summaryrefslogtreecommitdiffstats
path: root/src/parse.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-12-01 04:21:57 -0500
committerJesse Luehrs <doy@tozt.net>2022-12-01 04:47:05 -0500
commit18cab4c9209996aeaa29a88f8ac98e91dcfdede9 (patch)
treedbf156a4a8951facc32fefaaa83a4ea6ff38db24 /src/parse.rs
parent3cdefe9564d7d13ce0c643c1b4433f21f2d2e839 (diff)
downloadadvent-of-code-18cab4c9209996aeaa29a88f8ac98e91dcfdede9.tar.gz
advent-of-code-18cab4c9209996aeaa29a88f8ac98e91dcfdede9.zip
simplify
Diffstat (limited to 'src/parse.rs')
-rw-r--r--src/parse.rs37
1 files changed, 33 insertions, 4 deletions
diff --git a/src/parse.rs b/src/parse.rs
index 3674a25..3222bf0 100644
--- a/src/parse.rs
+++ b/src/parse.rs
@@ -4,12 +4,41 @@ pub fn data(year: u16, day: u16) -> Result<File> {
File::open(format!("data/{}/{}.txt", year, day)).map_err(|e| anyhow!(e))
}
-pub fn lines(fh: File) -> impl Iterator<Item = String> {
+pub fn lines<R: std::io::Read>(fh: R) -> impl Iterator<Item = String> {
let fh = std::io::BufReader::new(fh);
fh.lines().map(|res| res.unwrap())
}
-pub fn split(fh: File, sep: u8) -> impl Iterator<Item = String> {
+pub struct Chunk<'a, I: Iterator<Item = String>> {
+ it: &'a mut I,
+}
+
+impl<'a, I: Iterator<Item = String>> Iterator for Chunk<'a, I> {
+ type Item = String;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ if let Some(line) = self.it.next() {
+ if line.is_empty() {
+ return None;
+ } else {
+ return Some(line);
+ }
+ }
+ None
+ }
+}
+
+pub fn chunk<I>(it: &mut I) -> Chunk<'_, I>
+where
+ I: Iterator<Item = String>,
+{
+ Chunk { it }
+}
+
+pub fn split<R: std::io::Read>(
+ fh: R,
+ sep: u8,
+) -> impl Iterator<Item = String> {
let fh = std::io::BufReader::new(fh);
fh.split(sep)
.map(|res| String::from_utf8(res.unwrap()).unwrap())
@@ -19,11 +48,11 @@ pub fn ints(iter: impl Iterator<Item = String>) -> impl Iterator<Item = i64> {
iter.map(|s| s.trim().parse().unwrap())
}
-pub fn bytes(fh: File) -> impl Iterator<Item = u8> {
+pub fn bytes<R: std::io::Read>(fh: R) -> impl Iterator<Item = u8> {
fh.bytes().map(|res| res.unwrap())
}
-pub fn string(fh: File) -> String {
+pub fn string<R: std::io::Read>(fh: R) -> String {
let bytes: Vec<_> = bytes(fh).collect();
String::from_utf8(bytes).unwrap()
}