Add error type to use for RenameRule parsing

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

View File

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

View File

@ -34,7 +34,7 @@ pub enum RenameRule {
}
impl RenameRule {
pub fn from_str(rename_all_str: &str) -> Result<Self, ()> {
pub fn from_str(rename_all_str: &str) -> Result<Self, ParseError> {
match rename_all_str {
"lowercase" => Ok(LowerCase),
"UPPERCASE" => Ok(UPPERCASE),
@ -44,7 +44,7 @@ impl RenameRule {
"SCREAMING_SNAKE_CASE" => Ok(ScreamingSnakeCase),
"kebab-case" => Ok(KebabCase),
"SCREAMING-KEBAB-CASE" => Ok(ScreamingKebabCase),
_ => Err(()),
unknown => Err(ParseError { unknown }),
}
}
@ -104,6 +104,10 @@ impl RenameRule {
}
}
pub struct ParseError<'a> {
unknown: &'a str,
}
#[test]
fn rename_variants() {
for &(original, lower, upper, camel, snake, screaming, kebab, screaming_kebab) in &[