From cfd9b4f2ad1a09bedf7f764f84448a61faab54a3 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Tue, 19 Apr 2022 20:26:02 +0200 Subject: Refile for merging repos --- 2018/src/lib.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2018/src/lib.rs (limited to '2018/src/lib.rs') 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, std::io::Error> { + let file = File::open(file)?; + let file_reader = BufReader::new(file); + file_reader.lines() + .collect::, _>>() + .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) -> Vec { + 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); + }; +} -- cgit v1.2.3