summaryrefslogtreecommitdiff
path: root/2018/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to '2018/src/lib.rs')
-rw-r--r--2018/src/lib.rs31
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);
+ };
+}