summaryrefslogtreecommitdiffstats
path: root/src/util.rs
blob: 94f97969b61d55eb7caa9ac7f0a3cabd92a8d68c (plain) (blame)
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
use anyhow::Context as _;
use std::io::{BufRead as _, Read as _};

pub fn read_ints(filename: &str) -> anyhow::Result<Vec<i32>> {
    let f = std::fs::File::open(filename)
        .with_context(|| format!("couldn't find data file {}", filename))?;
    let f = std::io::BufReader::new(f);
    let ints: anyhow::Result<Vec<i32>> = f
        .lines()
        .map(|l| {
            l.context("failed to read a line")?
                .parse()
                .context("failed to parse line into an integer")
        })
        .collect();
    ints
}

pub fn read_file_str(filename: &str) -> anyhow::Result<String> {
    let mut f = std::fs::File::open(filename)
        .with_context(|| format!("couldn't find data file {}", filename))?;
    let mut s = String::new();
    f.read_to_string(&mut s)
        .context("failed to read map contents")?;
    Ok(s)
}