Move RenameRule parse error message to a Display impl

This commit is contained in:
David Tolnay 2021-01-23 14:12:26 -08:00
parent 7db0982e58
commit 51d4563ed1
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 18 additions and 42 deletions

View File

@ -339,13 +339,7 @@ impl Container {
rename_all_ser_rule.set(&m.path, rename_rule);
rename_all_de_rule.set(&m.path, rename_rule);
}
Err(_err) => cx.error_spanned_by(
s,
format!(
"unknown rename rule for #[serde(rename_all = {:?})]",
s.value(),
),
),
Err(err) => cx.error_spanned_by(s, err),
}
}
}
@ -356,25 +350,13 @@ impl Container {
if let Some(ser) = ser {
match RenameRule::from_str(&ser.value()) {
Ok(rename_rule) => rename_all_ser_rule.set(&m.path, rename_rule),
Err(_err) => cx.error_spanned_by(
ser,
format!(
"unknown rename rule for #[serde(rename_all = {:?})]",
ser.value(),
),
),
Err(err) => cx.error_spanned_by(ser, err),
}
}
if let Some(de) = de {
match RenameRule::from_str(&de.value()) {
Ok(rename_rule) => rename_all_de_rule.set(&m.path, rename_rule),
Err(_err) => cx.error_spanned_by(
de,
format!(
"unknown rename rule for #[serde(rename_all = {:?})]",
de.value(),
),
),
Err(err) => cx.error_spanned_by(de, err),
}
}
}
@ -930,13 +912,7 @@ impl Variant {
rename_all_ser_rule.set(&m.path, rename_rule);
rename_all_de_rule.set(&m.path, rename_rule);
}
Err(_err) => cx.error_spanned_by(
s,
format!(
"unknown rename rule for #[serde(rename_all = {:?})]",
s.value()
),
),
Err(err) => cx.error_spanned_by(s, err),
}
}
}
@ -947,25 +923,13 @@ impl Variant {
if let Some(ser) = ser {
match RenameRule::from_str(&ser.value()) {
Ok(rename_rule) => rename_all_ser_rule.set(&m.path, rename_rule),
Err(_err) => cx.error_spanned_by(
ser,
format!(
"unknown rename rule for #[serde(rename_all = {:?})]",
ser.value(),
),
),
Err(err) => cx.error_spanned_by(ser, err),
}
}
if let Some(de) = de {
match RenameRule::from_str(&de.value()) {
Ok(rename_rule) => rename_all_de_rule.set(&m.path, rename_rule),
Err(_err) => cx.error_spanned_by(
de,
format!(
"unknown rename rule for #[serde(rename_all = {:?})]",
de.value(),
),
),
Err(err) => cx.error_spanned_by(de, err),
}
}
}

View File

@ -5,6 +5,8 @@
#[allow(deprecated, unused_imports)]
use std::ascii::AsciiExt;
use std::fmt::{self, Display};
use self::RenameRule::*;
/// The different possible ways to change case of fields in a struct, or variants in an enum.
@ -108,6 +110,16 @@ pub struct ParseError<'a> {
unknown: &'a str,
}
impl<'a> Display for ParseError<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"unknown rename rule for #[serde(rename_all = {:?})]",
self.unknown,
)
}
}
#[test]
fn rename_variants() {
for &(original, lower, upper, camel, snake, screaming, kebab, screaming_kebab) in &[