diff options
author | Jesse Luehrs <doy@tozt.net> | 2022-12-11 21:59:21 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2022-12-11 22:16:30 -0500 |
commit | e2d219b331a878bbb3c9dcef9ea4e218b2e3ee06 (patch) | |
tree | 93e418011c45cab8d4070d3d33b377a9364f4a27 /src/parse.rs | |
parent | 179467096141b7e8f67d63b89fd21e779a564fe6 (diff) | |
download | advent-of-code-e2d219b331a878bbb3c9dcef9ea4e218b2e3ee06.tar.gz advent-of-code-e2d219b331a878bbb3c9dcef9ea4e218b2e3ee06.zip |
refactor
Diffstat (limited to 'src/parse.rs')
-rw-r--r-- | src/parse.rs | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/src/parse.rs b/src/parse.rs index 3222bf0..7e6cab7 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -1,14 +1,26 @@ use crate::prelude::*; -pub fn data(year: u16, day: u16) -> Result<File> { +pub fn data(year: u16, day: u8) -> Result<File> { File::open(format!("data/{}/{}.txt", year, day)).map_err(|e| anyhow!(e)) } -pub fn lines<R: std::io::Read>(fh: R) -> impl Iterator<Item = String> { +pub fn raw_lines<R>(fh: R) -> impl Iterator<Item = String> +where + R: std::io::Read, +{ let fh = std::io::BufReader::new(fh); fh.lines().map(|res| res.unwrap()) } +pub fn lines<R, T>(fh: R) -> impl Iterator<Item = T> +where + R: std::io::Read, + T: std::str::FromStr, + <T as std::str::FromStr>::Err: std::fmt::Debug, +{ + raw_lines(fh).map(|s| s.trim().parse().unwrap()) +} + pub struct Chunk<'a, I: Iterator<Item = String>> { it: &'a mut I, } @@ -35,17 +47,16 @@ where Chunk { it } } -pub fn split<R: std::io::Read>( - fh: R, - sep: u8, -) -> impl Iterator<Item = String> { +pub fn split<R, T>(fh: R, sep: u8) -> impl Iterator<Item = T> +where + R: std::io::Read, + T: std::str::FromStr, + <T as std::str::FromStr>::Err: std::fmt::Debug, +{ let fh = std::io::BufReader::new(fh); fh.split(sep) .map(|res| String::from_utf8(res.unwrap()).unwrap()) -} - -pub fn ints(iter: impl Iterator<Item = String>) -> impl Iterator<Item = i64> { - iter.map(|s| s.trim().parse().unwrap()) + .map(|s| s.trim().parse().unwrap()) } pub fn bytes<R: std::io::Read>(fh: R) -> impl Iterator<Item = u8> { |