Improve errors messages `-m` and `-M`

pull/522/head
Arijit Basu 2 years ago
parent b05e702536
commit 4a34780eb4
No known key found for this signature in database
GPG Key ID: 0F8EF5258DC38077

@ -178,7 +178,7 @@ fn fmt_msg_in(args: Vec<String>) -> Result<String> {
let mut msg = "".to_string(); let mut msg = "".to_string();
let mut last_char = None; let mut last_char = None;
for ch in format.chars() { for (col, ch) in format.chars().enumerate() {
match (ch, last_char) { match (ch, last_char) {
('%', Some('%')) => { ('%', Some('%')) => {
msg.push(ch); msg.push(ch);
@ -188,17 +188,26 @@ fn fmt_msg_in(args: Vec<String>) -> Result<String> {
last_char = Some(ch); last_char = Some(ch);
} }
('q', Some('%')) => { ('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)?); msg.push_str(&json::to_string(&arg)?);
last_char = None; last_char = None;
} }
('s', Some('%')) => { ('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); msg.push_str(&arg);
last_char = None; last_char = None;
} }
(ch, Some('%')) => { (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, _) => { (ch, _) => {
msg.push(ch); msg.push(ch);
@ -212,7 +221,7 @@ fn fmt_msg_in(args: Vec<String>) -> Result<String> {
} }
if args.count() != 0 { 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 // Since we'll mostly by passing json using `-m`, and json is faster than yaml

Loading…
Cancel
Save