From f105c5bc0dbbe15d36acd0d596b0e6fa7d22b1d0 Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Thu, 3 Oct 2024 14:17:31 -0700 Subject: [PATCH 1/1] Show line number in command-related error messages --- src/lib.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e20d23d..09e2e7a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,17 +10,20 @@ struct Config { static_columns: Vec, } 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 ") { - 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 { return Err(std::io::Error::new( std::io::ErrorKind::InvalidInput, - format!("Unknown command: {cmd}"), + format!("line {line_num}: Unknown command: {cmd}"), )); } Ok(()) @@ -163,7 +166,7 @@ impl<'cfg, Input: Iterator>> Iterator 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)); } } @@ -525,11 +528,11 @@ mod tests { let bad_command = read_config(&b"!no such command"[..]); assert!(bad_command.is_err()); - assert!(format!("{bad_command:?}").contains("Unknown command")); + assert!(format!("{bad_command:?}").contains("line 1: Unknown command")); 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] -- 2.44.1