diff --git a/src/file.rs b/src/file.rs index 8056f6c..133fe4c 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,5 +1,7 @@ use std::fs; +use crate::parse::string_to_numbers; + pub fn read_to_lines(_filename: &str) -> Result, String> { let _contents = fs::read_to_string(_filename); if let Err(v) = _contents { @@ -22,4 +24,19 @@ pub fn read_to_string(_filename: &str) -> Result { }, Err(v) => Err(v) } +} + +pub fn read_to_matrix(_filename: &str) -> Result>, String> { + let _lines = read_to_lines(_filename); + + if let Err(v) = _lines { + return Err(v); + } + + Ok(_lines + .unwrap() + .iter() + .map(|x| string_to_numbers(x.to_string())) + .collect() + ) } \ No newline at end of file diff --git a/src/parse.rs b/src/parse.rs index f5648fb..d246b5b 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -2,7 +2,20 @@ pub fn string_to_usize_vec(input: String) -> Vec { input.chars().map(|x| x.to_digit(10).unwrap() as usize).collect() } +pub fn string_to_numbers(input: String) -> Vec { + input + .split_whitespace() + .map(|x| x.parse::().unwrap()) + .collect() +} + #[test] fn test_string_to_usize_vec() { assert_eq!(vec![1, 2, 3, 4], string_to_usize_vec(String::from("1234"))); -} \ No newline at end of file +} + +#[test] +fn test_string_to_numbers() { + assert_eq!(vec![1, 2, -3, -4], string_to_numbers(String::from("1 2 -3 -4"))); +} +