From a604a6dbf0779c001d145297eb1fb1a1164882f9 Mon Sep 17 00:00:00 2001 From: a14m Date: Tue, 6 Apr 2021 20:35:13 +0200 Subject: Refactor populate to work with strings and generate vec --- CLI/rust/src/game_of_life/generators/input.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'CLI') diff --git a/CLI/rust/src/game_of_life/generators/input.rs b/CLI/rust/src/game_of_life/generators/input.rs index 4f205cb..9584cee 100644 --- a/CLI/rust/src/game_of_life/generators/input.rs +++ b/CLI/rust/src/game_of_life/generators/input.rs @@ -78,16 +78,7 @@ fn generate_file_data(opts: Opts) -> Result { )) } }; - let data: Vec> = file - .split("\n") - .map(|s| { - s.to_string() - .chars() - .map(|c| c.is_ascii_alphanumeric()) - .collect() - }) - .collect(); - Ok(populate(opts, data)) + Ok(populate(opts, file)) } #[cfg(test)] @@ -148,8 +139,18 @@ fn generate_url_data(_opts: Opts) -> Result { /// Return a universe struct with populated data from the data param /// /// The data param is reshaped to fit with the opts width/height (extended or shrinked) -fn populate(opts: Opts, data: Vec>) -> Universe { +fn populate(opts: Opts, raw_data: String) -> Universe { let (width, height) = (opts.width as usize, opts.height as usize); + let data: Vec> = raw_data + .split("\n") + .map(|s| { + s.to_string() + .chars() + .map(|c| c.is_ascii_alphanumeric()) + .collect() + }) + .collect(); + let mut current = vec![vec![false; width]; height]; for i in 0..height { @@ -188,7 +189,7 @@ mod populate { #[test] fn expand_data() { - let data = vec![vec![true, false, true]; 3]; + let data = String::from("x.x\nO.0\nO.X"); let opts = opts(5, 5); let universe = populate(opts.clone(), data); @@ -210,7 +211,7 @@ mod populate { #[test] fn shrink_data() { - let data = vec![vec![true, false, true]; 3]; + let data = String::from("x.x\nO.0\nO.X"); let opts = opts(2, 2); let universe = populate(opts.clone(), data); -- cgit v1.2.3