Impl: Check for wrong input characters

This commit is contained in:
Jonas Zeunert
2024-02-18 17:30:41 +01:00
parent 34c8d01a23
commit 14ba619bc4

View File

@@ -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;