]> git.scottworley.com Git - tablify/blobdiff - src/lib.rs
Show line number in command-related error messages
[tablify] / src / lib.rs
index af582d45272207432a0d5de47e6f01be1dcf9938..09e2e7ad11801dd754e20d98ce0eb37a8459bec7 100644 (file)
@@ -10,13 +10,21 @@ struct Config {
     static_columns: Vec<String>,
 }
 impl Config {
     static_columns: Vec<String>,
 }
 impl Config {
-    fn apply_command(&mut self, cmd: &str) -> Result<(), std::io::Error> {
+    fn apply_command(&mut self, line_num: usize, cmd: &str) -> Result<(), std::io::Error> {
         if let Some(threshold) = cmd.strip_prefix("col_threshold ") {
         if let Some(threshold) = cmd.strip_prefix("col_threshold ") {
-            self.column_threshold = threshold
-                .parse()
-                .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e))?;
+            self.column_threshold = threshold.parse().map_err(|e| {
+                std::io::Error::new(
+                    std::io::ErrorKind::InvalidInput,
+                    format!("line {line_num}: col_threshold must be numeric: {e}"),
+                )
+            })?;
         } else if let Some(col) = cmd.strip_prefix("col ") {
             self.static_columns.push(col.to_owned());
         } else if let Some(col) = cmd.strip_prefix("col ") {
             self.static_columns.push(col.to_owned());
+        } else {
+            return Err(std::io::Error::new(
+                std::io::ErrorKind::InvalidInput,
+                format!("line {line_num}: Unknown command: {cmd}"),
+            ));
         }
         Ok(())
     }
         }
         Ok(())
     }
@@ -158,7 +166,7 @@ impl<'cfg, Input: Iterator<Item = Result<String, std::io::Error>>> Iterator
                 Some((_, Err(e))) => return Some(Err(e)),
                 Some((n, Ok(line))) => match InputLine::from(line.as_ref()) {
                     InputLine::Command(cmd) => {
                 Some((_, Err(e))) => return Some(Err(e)),
                 Some((n, Ok(line))) => match InputLine::from(line.as_ref()) {
                     InputLine::Command(cmd) => {
-                        if let Err(e) = self.config.apply_command(cmd) {
+                        if let Err(e) = self.config.apply_command(n + 1, cmd) {
                             return Some(Err(e));
                         }
                     }
                             return Some(Err(e));
                         }
                     }
@@ -169,7 +177,7 @@ impl<'cfg, Input: Iterator<Item = Result<String, std::io::Error>>> Iterator
                     InputLine::Entry(col, instance) => match &mut self.row {
                         None => {
                             return Some(Err(std::io::Error::other(format!(
                     InputLine::Entry(col, instance) => match &mut self.row {
                         None => {
                             return Some(Err(std::io::Error::other(format!(
-                                "{}: Entry with no header",
+                                "line {}: Entry with no header",
                                 n + 1
                             ))))
                         }
                                 n + 1
                             ))))
                         }
@@ -498,11 +506,11 @@ mod tests {
 
         let bad = read_rows(&b" foo"[..]);
         assert!(bad.is_err());
 
         let bad = read_rows(&b" foo"[..]);
         assert!(bad.is_err());
-        assert!(format!("{bad:?}").contains("1: Entry with no header"));
+        assert!(format!("{bad:?}").contains("line 1: Entry with no header"));
 
         let bad2 = read_rows(&b"foo\n\n bar"[..]);
         assert!(bad2.is_err());
 
         let bad2 = read_rows(&b"foo\n\n bar"[..]);
         assert!(bad2.is_err());
-        assert!(format!("{bad2:?}").contains("3: Entry with no header"));
+        assert!(format!("{bad2:?}").contains("line 3: Entry with no header"));
     }
 
     #[test]
     }
 
     #[test]
@@ -518,9 +526,13 @@ mod tests {
             vec!["foo".to_owned()]
         );
 
             vec!["foo".to_owned()]
         );
 
+        let bad_command = read_config(&b"!no such command"[..]);
+        assert!(bad_command.is_err());
+        assert!(format!("{bad_command:?}").contains("line 1: Unknown command"));
+
         let bad_num = read_config(&b"!col_threshold foo"[..]);
         assert!(bad_num.is_err());
         let bad_num = read_config(&b"!col_threshold foo"[..]);
         assert!(bad_num.is_err());
-        assert!(format!("{bad_num:?}").contains("Parse"));
+        assert!(format!("{bad_num:?}").contains("line 1: col_threshold must be numeric"));
     }
 
     #[test]
     }
 
     #[test]