]> git.scottworley.com Git - tablify/commitdiff
Line numbers in error messages
authorScott Worley <scottworley@scottworley.com>
Mon, 19 Aug 2024 17:49:44 +0000 (10:49 -0700)
committerScott Worley <scottworley@scottworley.com>
Mon, 19 Aug 2024 17:49:44 +0000 (10:49 -0700)
src/lib.rs

index 72a3add0ef2f9edccc34cc6f39b9ce8df9f79b56..56a2b6ddec94a404a23d582bbf3b16fcc06bb5bc 100644 (file)
@@ -10,13 +10,16 @@ struct RowInput {
 }
 
 struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> {
 }
 
 struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> {
-    input: Input,
+    input: std::iter::Enumerate<Input>,
     row: Option<RowInput>,
 }
 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> {
     #[cfg(test)]
     fn new(input: Input) -> Self {
     row: Option<RowInput>,
 }
 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> {
     #[cfg(test)]
     fn new(input: Input) -> Self {
-        Self { input, row: None }
+        Self {
+            input: input.enumerate(),
+            row: None,
+        }
     }
 }
 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> {
     }
 }
 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> {
@@ -26,19 +29,24 @@ impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader
             match self
                 .input
                 .next()
             match self
                 .input
                 .next()
-                .map(|r| r.map(|line| String::from(line.trim_end())))
+                .map(|(n, r)| (n, r.map(|line| String::from(line.trim_end()))))
             {
                 None => return Ok(std::mem::take(&mut self.row)).transpose(),
             {
                 None => return Ok(std::mem::take(&mut self.row)).transpose(),
-                Some(Err(e)) => return Some(Err(e)),
-                Some(Ok(line)) if line.is_empty() && self.row.is_some() => {
+                Some((_, Err(e))) => return Some(Err(e)),
+                Some((_, Ok(line))) if line.is_empty() && self.row.is_some() => {
                     return Ok(std::mem::take(&mut self.row)).transpose()
                 }
                     return Ok(std::mem::take(&mut self.row)).transpose()
                 }
-                Some(Ok(line)) if line.is_empty() => {}
-                Some(Ok(line)) if line.starts_with(' ') => match &mut self.row {
-                    None => return Some(Err(std::io::Error::other("Entry with no header"))),
+                Some((_, Ok(line))) if line.is_empty() => {}
+                Some((n, Ok(line))) if line.starts_with(' ') => match &mut self.row {
+                    None => {
+                        return Some(Err(std::io::Error::other(format!(
+                            "{}: Entry with no header",
+                            n + 1
+                        ))))
+                    }
                     Some(ref mut row) => row.entries.push(String::from(line.trim())),
                 },
                     Some(ref mut row) => row.entries.push(String::from(line.trim())),
                 },
-                Some(Ok(line)) => {
+                Some((_, Ok(line))) => {
                     let prev = std::mem::take(&mut self.row);
                     self.row = Some(RowInput {
                         label: line,
                     let prev = std::mem::take(&mut self.row);
                     self.row = Some(RowInput {
                         label: line,
@@ -153,10 +161,10 @@ mod tests {
 
         let bad = read_rows(&b" foo"[..]).next().unwrap();
         assert!(bad.is_err());
 
         let bad = read_rows(&b" foo"[..]).next().unwrap();
         assert!(bad.is_err());
-        assert!(format!("{bad:?}").contains("Entry with no header"));
+        assert!(format!("{bad:?}").contains("1: Entry with no header"));
 
         let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap();
         assert!(bad2.is_err());
 
         let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap();
         assert!(bad2.is_err());
-        assert!(format!("{bad2:?}").contains("Entry with no header"));
+        assert!(format!("{bad2:?}").contains("3: Entry with no header"));
     }
 }
     }
 }