summaryrefslogtreecommitdiff
path: root/2018/src/lib.rs
blob: 72520fe60c9b827af7904e739116c967df99b3f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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);
    };
}