diff options
Diffstat (limited to 'src/util.rs')
-rw-r--r-- | src/util.rs | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..fdc3bed --- /dev/null +++ b/src/util.rs @@ -0,0 +1,17 @@ +use anyhow::Context as _; +use std::io::BufRead 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 +} |