From 14ba619bc4eede4b69644bf53fdea253fa416a87 Mon Sep 17 00:00:00 2001 From: Jonas Zeunert Date: Sun, 18 Feb 2024 17:30:41 +0100 Subject: [PATCH] Impl: Check for wrong input characters --- src/main.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6d8c46a..817fe66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,5 @@ use clap::Parser; -use regex::Regex; - #[derive(Parser)] #[command(version, about, long_about = None)] struct Cli { @@ -87,7 +85,12 @@ fn parse_playfield(field: &String, field_size: usize) -> Playfield { continue; } - field.value = Some(char_field.parse().unwrap()); + field.value = Some(char_field.parse().unwrap_or_else(|char_field| { + panic!( + "Input must contain only digits followed by space. Disallowed character: {}", + char_field + ) + })); } } @@ -132,7 +135,7 @@ mod tests { } #[test] - #[should_panic(expected = "Input must have dimension of size 2x2")] + #[should_panic(expected = "Input must have dimension of size 3x3")] fn test_parse_field_too_short_input() { let input = "1 2 3"; let field_size = 3;