Ref: Move check outside to make it more sound
This commit is contained in:
13
src/main.rs
13
src/main.rs
@@ -67,16 +67,17 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_playfield(field: &String, field_size: usize) -> Playfield {
|
fn parse_playfield(field: &String, field_size: usize) -> Playfield {
|
||||||
let playfield = &mut Playfield::new(field_size);
|
let mut playfield = Playfield::new(field_size);
|
||||||
|
|
||||||
let chars = field.split(" ").collect::<Vec<&str>>();
|
let chars = field.split(" ").collect::<Vec<&str>>();
|
||||||
|
|
||||||
if chars.len() != field_size * field_size {
|
if !is_right_field_size(&chars, field_size) {
|
||||||
panic!(
|
panic!(
|
||||||
"Input must have dimension of size {size}x{size}",
|
"Input must have dimension of size {size}x{size}",
|
||||||
size = field_size
|
size = field_size
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
for row in 0..field_size {
|
for row in 0..field_size {
|
||||||
for col in 0..field_size {
|
for col in 0..field_size {
|
||||||
let field = &mut playfield.fields[row][col];
|
let field = &mut playfield.fields[row][col];
|
||||||
@@ -86,11 +87,15 @@ fn parse_playfield(field: &String, field_size: usize) -> Playfield {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
field.value = Some(char_field.to_string().parse::<u32>().unwrap());
|
field.value = Some(char_field.parse().unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return playfield.clone();
|
return playfield;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_right_field_size(field: &Vec<&str>, field_size: usize) -> bool {
|
||||||
|
return field.len() == field_size * field_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
Reference in New Issue
Block a user