diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2022-04-19 20:26:02 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2022-04-19 20:26:02 +0200 |
commit | cfd9b4f2ad1a09bedf7f764f84448a61faab54a3 (patch) | |
tree | bdabffda69f447aa9e6816d14aa6125ac5b767bf /2018/src/lib.rs | |
parent | 5edb4153cde1171c5aff85de16a9ace526b0b293 (diff) |
Refile for merging repos
Diffstat (limited to '2018/src/lib.rs')
-rw-r--r-- | 2018/src/lib.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/2018/src/lib.rs b/2018/src/lib.rs new file mode 100644 index 0000000..72520fe --- /dev/null +++ b/2018/src/lib.rs @@ -0,0 +1,31 @@ +use std::path::PathBuf; +use std::io::BufReader; +use std::io::prelude::*; +use std::fs::File; + +/// Reads a specified file into a vector of strings, one line of the +/// file per string. Fails if any part of reading the file fails. +pub fn read_file(file: &PathBuf) -> Result<Vec<String>, std::io::Error> { + let file = File::open(file)?; + let file_reader = BufReader::new(file); + file_reader.lines() + .collect::<Result<Vec<_>, _>>() + .map(preprocess_file_lines) +} + +/// Removes any empty lines and makes sure that lines don't have +/// problematic whitespace. +pub fn preprocess_file_lines(lines: Vec<String>) -> Vec<String> { + lines.iter() + .filter(|line| line.len() > 0) + .map(|line| line.trim_right().to_string()) + .collect() +} + + +#[macro_export] +macro_rules! debug { + ( $x:expr ) => { + println!("{} = {:?}", stringify!($x), $x); + }; +} |