From 4a34780eb4ff5f12ea85dc1b0b03c68c764aec2c Mon Sep 17 00:00:00 2001 From: Arijit Basu Date: Wed, 26 Oct 2022 17:43:05 +0530 Subject: [PATCH] Improve errors messages `-m` and `-M` --- src/cli.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index e4e4dd7..3e0aa9b 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -178,7 +178,7 @@ fn fmt_msg_in(args: Vec) -> Result { let mut msg = "".to_string(); let mut last_char = None; - for ch in format.chars() { + for (col, ch) in format.chars().enumerate() { match (ch, last_char) { ('%', Some('%')) => { msg.push(ch); @@ -188,17 +188,26 @@ fn fmt_msg_in(args: Vec) -> Result { last_char = Some(ch); } ('q', Some('%')) => { - let arg = args.next().context("not enough arguments")?; + let arg = args.next().context(format!( + "argument missing for the placeholder at col {}", + col + ))?; msg.push_str(&json::to_string(&arg)?); last_char = None; } ('s', Some('%')) => { - let arg = args.next().context("not enough arguments")?; + let arg = args.next().context(format!( + "argument missing for the placeholder at col {}", + col + ))?; msg.push_str(&arg); last_char = None; } (ch, Some('%')) => { - bail!(format!("invalid placeholder: %{}, use %s, %q or %%", ch)); + bail!(format!( + "invalid placeholder (%{}) at col {}, use one of %s %q %%", + ch, col + )); } (ch, _) => { msg.push(ch); @@ -212,7 +221,7 @@ fn fmt_msg_in(args: Vec) -> Result { } if args.count() != 0 { - bail!("too many arguments") + bail!("too many arguments, not enough placeholders") } // Since we'll mostly by passing json using `-m`, and json is faster than yaml