diff --git a/serde_derive/src/internals/attr.rs b/serde_derive/src/internals/attr.rs index cea79663..a7757ec4 100644 --- a/serde_derive/src/internals/attr.rs +++ b/serde_derive/src/internals/attr.rs @@ -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), } } } diff --git a/serde_derive/src/internals/case.rs b/serde_derive/src/internals/case.rs index d9634e73..9ed8cd47 100644 --- a/serde_derive/src/internals/case.rs +++ b/serde_derive/src/internals/case.rs @@ -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 &[