commit a4f69cc2c251c9498537e15ff1903d52be5ee523 Author: Patrick MARIE Date: Sun Jan 17 17:20:54 2021 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..aa02585 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "mkz_aoc" +version = "0.1.0" +authors = ["Patrick MARIE "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/file.rs b/src/file.rs new file mode 100644 index 0000000..8056f6c --- /dev/null +++ b/src/file.rs @@ -0,0 +1,25 @@ +use std::fs; + +pub fn read_to_lines(_filename: &str) -> Result, String> { + let _contents = fs::read_to_string(_filename); + if let Err(v) = _contents { + return Err(format!("Could not read file: {}", v)); + } + + let lines = _contents.unwrap().lines().map(|x| x.to_string()).collect::>(); + + Ok(lines) +} + +pub fn read_to_string(_filename: &str) -> Result { + let _lines = read_to_lines(_filename); + + match _lines { + Ok(lines) => if lines.len() >= 1 { + Ok(lines[0].clone()) + } else { + Err(String::from("Missing content")) + }, + Err(v) => Err(v) + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..99e3888 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,2 @@ +pub mod file; +pub mod parse; \ No newline at end of file diff --git a/src/parse.rs b/src/parse.rs new file mode 100644 index 0000000..f5648fb --- /dev/null +++ b/src/parse.rs @@ -0,0 +1,8 @@ +pub fn string_to_usize_vec(input: String) -> Vec { + input.chars().map(|x| x.to_digit(10).unwrap() as usize).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