mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-22 16:08:05 +00:00
Format with rustfmt-nightly 0.3.4
This commit is contained in:
@@ -59,19 +59,15 @@ impl<'a> Container<'a> {
|
||||
};
|
||||
|
||||
match body {
|
||||
Body::Enum(ref mut variants) => {
|
||||
for ref mut variant in variants {
|
||||
variant.attrs.rename_by_rule(attrs.rename_all());
|
||||
for ref mut field in &mut variant.fields {
|
||||
field.attrs.rename_by_rule(variant.attrs.rename_all());
|
||||
}
|
||||
Body::Enum(ref mut variants) => for ref mut variant in variants {
|
||||
variant.attrs.rename_by_rule(attrs.rename_all());
|
||||
for ref mut field in &mut variant.fields {
|
||||
field.attrs.rename_by_rule(variant.attrs.rename_all());
|
||||
}
|
||||
}
|
||||
Body::Struct(_, ref mut fields) => {
|
||||
for field in fields {
|
||||
field.attrs.rename_by_rule(attrs.rename_all());
|
||||
}
|
||||
}
|
||||
},
|
||||
Body::Struct(_, ref mut fields) => for field in fields {
|
||||
field.attrs.rename_by_rule(attrs.rename_all());
|
||||
},
|
||||
}
|
||||
|
||||
let item = Container {
|
||||
@@ -128,16 +124,18 @@ fn struct_from_ast<'a>(
|
||||
container_default: &attr::Default,
|
||||
) -> (Style, Vec<Field<'a>>) {
|
||||
match *data {
|
||||
syn::VariantData::Struct(ref fields) => {
|
||||
(Style::Struct, fields_from_ast(cx, fields, attrs, container_default))
|
||||
}
|
||||
syn::VariantData::Struct(ref fields) => (
|
||||
Style::Struct,
|
||||
fields_from_ast(cx, fields, attrs, container_default),
|
||||
),
|
||||
syn::VariantData::Tuple(ref fields) if fields.len() == 1 => (
|
||||
Style::Newtype,
|
||||
fields_from_ast(cx, fields, attrs, container_default),
|
||||
),
|
||||
syn::VariantData::Tuple(ref fields) => {
|
||||
(Style::Tuple, fields_from_ast(cx, fields, attrs, container_default))
|
||||
}
|
||||
syn::VariantData::Tuple(ref fields) => (
|
||||
Style::Tuple,
|
||||
fields_from_ast(cx, fields, attrs, container_default),
|
||||
),
|
||||
syn::VariantData::Unit => (Style::Unit, Vec::new()),
|
||||
}
|
||||
}
|
||||
@@ -151,14 +149,10 @@ fn fields_from_ast<'a>(
|
||||
fields
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(
|
||||
|(i, field)| {
|
||||
Field {
|
||||
ident: field.ident.clone(),
|
||||
attrs: attr::Field::from_ast(cx, i, field, attrs, container_default),
|
||||
ty: &field.ty,
|
||||
}
|
||||
},
|
||||
)
|
||||
.map(|(i, field)| Field {
|
||||
ident: field.ident.clone(),
|
||||
attrs: attr::Field::from_ast(cx, i, field, attrs, container_default),
|
||||
ty: &field.ty,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
+107
-118
@@ -216,11 +216,11 @@ impl Container {
|
||||
if let Ok(s) = get_string_from_lit(cx, name.as_ref(), name.as_ref(), lit) {
|
||||
match RenameRule::from_str(&s) {
|
||||
Ok(rename_rule) => rename_all.set(rename_rule),
|
||||
Err(()) => {
|
||||
cx.error(format!("unknown rename rule for #[serde(rename_all \
|
||||
= {:?})]",
|
||||
s))
|
||||
}
|
||||
Err(()) => cx.error(format!(
|
||||
"unknown rename rule for #[serde(rename_all \
|
||||
= {:?})]",
|
||||
s
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -231,19 +231,15 @@ impl Container {
|
||||
}
|
||||
|
||||
// Parse `#[serde(default)]`
|
||||
MetaItem(Word(ref name)) if name == "default" => {
|
||||
match item.body {
|
||||
syn::Body::Struct(syn::VariantData::Struct(_)) => {
|
||||
default.set(Default::Default);
|
||||
}
|
||||
_ => {
|
||||
cx.error(
|
||||
"#[serde(default)] can only be used on structs \
|
||||
with named fields",
|
||||
)
|
||||
}
|
||||
MetaItem(Word(ref name)) if name == "default" => match item.body {
|
||||
syn::Body::Struct(syn::VariantData::Struct(_)) => {
|
||||
default.set(Default::Default);
|
||||
}
|
||||
}
|
||||
_ => cx.error(
|
||||
"#[serde(default)] can only be used on structs \
|
||||
with named fields",
|
||||
),
|
||||
},
|
||||
|
||||
// Parse `#[serde(default = "...")]`
|
||||
MetaItem(NameValue(ref name, ref lit)) if name == "default" => {
|
||||
@@ -252,12 +248,10 @@ impl Container {
|
||||
syn::Body::Struct(syn::VariantData::Struct(_)) => {
|
||||
default.set(Default::Path(path));
|
||||
}
|
||||
_ => {
|
||||
cx.error(
|
||||
"#[serde(default = \"...\")] can only be used \
|
||||
on structs with named fields",
|
||||
)
|
||||
}
|
||||
_ => cx.error(
|
||||
"#[serde(default = \"...\")] can only be used \
|
||||
on structs with named fields",
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -265,7 +259,8 @@ impl Container {
|
||||
// Parse `#[serde(bound = "D: Serialize")]`
|
||||
MetaItem(NameValue(ref name, ref lit)) if name == "bound" => {
|
||||
if let Ok(where_predicates) =
|
||||
parse_lit_into_where(cx, name.as_ref(), name.as_ref(), lit) {
|
||||
parse_lit_into_where(cx, name.as_ref(), name.as_ref(), lit)
|
||||
{
|
||||
ser_bound.set(where_predicates.clone());
|
||||
de_bound.set(where_predicates);
|
||||
}
|
||||
@@ -280,16 +275,14 @@ impl Container {
|
||||
}
|
||||
|
||||
// Parse `#[serde(untagged)]`
|
||||
MetaItem(Word(ref name)) if name == "untagged" => {
|
||||
match item.body {
|
||||
syn::Body::Enum(_) => {
|
||||
untagged.set_true();
|
||||
}
|
||||
syn::Body::Struct(_) => {
|
||||
cx.error("#[serde(untagged)] can only be used on enums")
|
||||
}
|
||||
MetaItem(Word(ref name)) if name == "untagged" => match item.body {
|
||||
syn::Body::Enum(_) => {
|
||||
untagged.set_true();
|
||||
}
|
||||
}
|
||||
syn::Body::Struct(_) => {
|
||||
cx.error("#[serde(untagged)] can only be used on enums")
|
||||
}
|
||||
},
|
||||
|
||||
// Parse `#[serde(tag = "type")]`
|
||||
MetaItem(NameValue(ref name, ref lit)) if name == "tag" => {
|
||||
@@ -312,12 +305,10 @@ impl Container {
|
||||
syn::Body::Enum(_) => {
|
||||
content.set(s);
|
||||
}
|
||||
syn::Body::Struct(_) => {
|
||||
cx.error(
|
||||
"#[serde(content = \"...\")] can only be used on \
|
||||
enums",
|
||||
)
|
||||
}
|
||||
syn::Body::Struct(_) => cx.error(
|
||||
"#[serde(content = \"...\")] can only be used on \
|
||||
enums",
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -354,8 +345,10 @@ impl Container {
|
||||
}
|
||||
|
||||
MetaItem(ref meta_item) => {
|
||||
cx.error(format!("unknown serde container attribute `{}`",
|
||||
meta_item.name()));
|
||||
cx.error(format!(
|
||||
"unknown serde container attribute `{}`",
|
||||
meta_item.name()
|
||||
));
|
||||
}
|
||||
|
||||
Literal(_) => {
|
||||
@@ -443,13 +436,12 @@ fn decide_tag(
|
||||
if let syn::Body::Enum(ref variants) = item.body {
|
||||
for variant in variants {
|
||||
match variant.data {
|
||||
syn::VariantData::Struct(_) |
|
||||
syn::VariantData::Unit => {}
|
||||
syn::VariantData::Struct(_) | syn::VariantData::Unit => {}
|
||||
syn::VariantData::Tuple(ref fields) => {
|
||||
if fields.len() != 1 {
|
||||
cx.error(
|
||||
"#[serde(tag = \"...\")] cannot be used with tuple \
|
||||
variants",
|
||||
variants",
|
||||
);
|
||||
break;
|
||||
}
|
||||
@@ -464,21 +456,19 @@ fn decide_tag(
|
||||
EnumTag::External // doesn't matter, will error
|
||||
}
|
||||
(false, None, Some(_)) => {
|
||||
cx.error("#[serde(tag = \"...\", content = \"...\")] must be used together",);
|
||||
cx.error("#[serde(tag = \"...\", content = \"...\")] must be used together");
|
||||
EnumTag::External
|
||||
}
|
||||
(true, None, Some(_)) => {
|
||||
cx.error("untagged enum cannot have #[serde(content = \"...\")]");
|
||||
EnumTag::External
|
||||
}
|
||||
(false, Some(tag), Some(content)) => {
|
||||
EnumTag::Adjacent {
|
||||
tag: tag,
|
||||
content: content,
|
||||
}
|
||||
}
|
||||
(false, Some(tag), Some(content)) => EnumTag::Adjacent {
|
||||
tag: tag,
|
||||
content: content,
|
||||
},
|
||||
(true, Some(_), Some(_)) => {
|
||||
cx.error("untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]",);
|
||||
cx.error("untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]");
|
||||
EnumTag::External
|
||||
}
|
||||
}
|
||||
@@ -493,7 +483,7 @@ fn decide_identifier(
|
||||
match (&item.body, field_identifier.get(), variant_identifier.get()) {
|
||||
(_, false, false) => Identifier::No,
|
||||
(_, true, true) => {
|
||||
cx.error("`field_identifier` and `variant_identifier` cannot both be set",);
|
||||
cx.error("`field_identifier` and `variant_identifier` cannot both be set");
|
||||
Identifier::No
|
||||
}
|
||||
(&syn::Body::Struct(_), true, false) => {
|
||||
@@ -560,11 +550,11 @@ impl Variant {
|
||||
if let Ok(s) = get_string_from_lit(cx, name.as_ref(), name.as_ref(), lit) {
|
||||
match RenameRule::from_str(&s) {
|
||||
Ok(rename_rule) => rename_all.set(rename_rule),
|
||||
Err(()) => {
|
||||
cx.error(format!("unknown rename rule for #[serde(rename_all \
|
||||
= {:?})]",
|
||||
s))
|
||||
}
|
||||
Err(()) => cx.error(format!(
|
||||
"unknown rename rule for #[serde(rename_all \
|
||||
= {:?})]",
|
||||
s
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -611,19 +601,20 @@ impl Variant {
|
||||
}
|
||||
|
||||
// Defer `#[serde(borrow)]` and `#[serde(borrow = "'a + 'b")]`
|
||||
MetaItem(ref mi) if mi.name() == "borrow" => {
|
||||
match variant.data {
|
||||
syn::VariantData::Tuple(ref fields) if fields.len() == 1 => {
|
||||
borrow.set(mi.clone());
|
||||
}
|
||||
_ => {
|
||||
cx.error("#[serde(borrow)] may only be used on newtype variants");
|
||||
}
|
||||
MetaItem(ref mi) if mi.name() == "borrow" => match variant.data {
|
||||
syn::VariantData::Tuple(ref fields) if fields.len() == 1 => {
|
||||
borrow.set(mi.clone());
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
cx.error("#[serde(borrow)] may only be used on newtype variants");
|
||||
}
|
||||
},
|
||||
|
||||
MetaItem(ref meta_item) => {
|
||||
cx.error(format!("unknown serde variant attribute `{}`", meta_item.name()));
|
||||
cx.error(format!(
|
||||
"unknown serde variant attribute `{}`",
|
||||
meta_item.name()
|
||||
));
|
||||
}
|
||||
|
||||
Literal(_) => {
|
||||
@@ -754,7 +745,12 @@ impl Field {
|
||||
.as_ref()
|
||||
.map(|borrow| vec![MetaItem(borrow.clone())]);
|
||||
|
||||
for meta_items in field.attrs.iter().filter_map(get_serde_meta_items).chain(variant_borrow) {
|
||||
for meta_items in field
|
||||
.attrs
|
||||
.iter()
|
||||
.filter_map(get_serde_meta_items)
|
||||
.chain(variant_borrow)
|
||||
{
|
||||
for meta_item in meta_items {
|
||||
match meta_item {
|
||||
// Parse `#[serde(rename = "foo")]`
|
||||
@@ -799,7 +795,7 @@ impl Field {
|
||||
MetaItem(Word(ref name)) if name == "skip" => {
|
||||
skip_serializing.set_true();
|
||||
skip_deserializing.set_true();
|
||||
},
|
||||
}
|
||||
|
||||
// Parse `#[serde(skip_serializing_if = "...")]`
|
||||
MetaItem(NameValue(ref name, ref lit)) if name == "skip_serializing_if" => {
|
||||
@@ -837,7 +833,8 @@ impl Field {
|
||||
// Parse `#[serde(bound = "D: Serialize")]`
|
||||
MetaItem(NameValue(ref name, ref lit)) if name == "bound" => {
|
||||
if let Ok(where_predicates) =
|
||||
parse_lit_into_where(cx, name.as_ref(), name.as_ref(), lit) {
|
||||
parse_lit_into_where(cx, name.as_ref(), name.as_ref(), lit)
|
||||
{
|
||||
ser_bound.set(where_predicates.clone());
|
||||
de_bound.set(where_predicates);
|
||||
}
|
||||
@@ -864,13 +861,10 @@ impl Field {
|
||||
if let Ok(borrowable) = borrowable_lifetimes(cx, &ident, &field.ty) {
|
||||
for lifetime in &lifetimes {
|
||||
if !borrowable.contains(lifetime) {
|
||||
cx.error(
|
||||
format!(
|
||||
"field `{}` does not have lifetime {}",
|
||||
ident,
|
||||
lifetime.ident
|
||||
),
|
||||
);
|
||||
cx.error(format!(
|
||||
"field `{}` does not have lifetime {}",
|
||||
ident, lifetime.ident
|
||||
));
|
||||
}
|
||||
}
|
||||
borrowed_lifetimes.set(lifetimes);
|
||||
@@ -886,7 +880,10 @@ impl Field {
|
||||
}
|
||||
|
||||
MetaItem(ref meta_item) => {
|
||||
cx.error(format!("unknown serde field attribute `{}`", meta_item.name()),);
|
||||
cx.error(format!(
|
||||
"unknown serde field attribute `{}`",
|
||||
meta_item.name()
|
||||
));
|
||||
}
|
||||
|
||||
Literal(_) => {
|
||||
@@ -1034,13 +1031,11 @@ where
|
||||
}
|
||||
|
||||
_ => {
|
||||
cx.error(
|
||||
format!(
|
||||
"malformed {0} attribute, expected `{0}(serialize = ..., \
|
||||
deserialize = ...)`",
|
||||
attr_name
|
||||
),
|
||||
);
|
||||
cx.error(format!(
|
||||
"malformed {0} attribute, expected `{0}(serialize = ..., \
|
||||
deserialize = ...)`",
|
||||
attr_name
|
||||
));
|
||||
return Err(());
|
||||
}
|
||||
}
|
||||
@@ -1076,13 +1071,10 @@ fn get_string_from_lit(
|
||||
if let syn::Lit::Str(ref s, _) = *lit {
|
||||
Ok(s.clone())
|
||||
} else {
|
||||
cx.error(
|
||||
format!(
|
||||
"expected serde {} attribute to be a string: `{} = \"...\"`",
|
||||
attr_name,
|
||||
meta_item_name
|
||||
),
|
||||
);
|
||||
cx.error(format!(
|
||||
"expected serde {} attribute to be a string: `{} = \"...\"`",
|
||||
attr_name, meta_item_name
|
||||
));
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
@@ -1113,11 +1105,12 @@ fn parse_lit_into_where(
|
||||
fn parse_lit_into_ty(cx: &Ctxt, attr_name: &str, lit: &syn::Lit) -> Result<syn::Ty, ()> {
|
||||
let string = try!(get_string_from_lit(cx, attr_name, attr_name, lit));
|
||||
|
||||
syn::parse_type(&string).map_err(
|
||||
|_| {
|
||||
cx.error(format!("failed to parse type: {} = {:?}", attr_name, string),)
|
||||
},
|
||||
)
|
||||
syn::parse_type(&string).map_err(|_| {
|
||||
cx.error(format!(
|
||||
"failed to parse type: {} = {:?}",
|
||||
attr_name, string
|
||||
))
|
||||
})
|
||||
}
|
||||
|
||||
// Parses a string literal like "'a + 'b + 'c" containing a nonempty list of
|
||||
@@ -1148,7 +1141,7 @@ fn parse_lit_into_lifetimes(
|
||||
return Ok(set);
|
||||
}
|
||||
}
|
||||
Err(cx.error(format!("failed to parse borrowed lifetimes: {:?}", string)),)
|
||||
Err(cx.error(format!("failed to parse borrowed lifetimes: {:?}", string)))
|
||||
}
|
||||
|
||||
// Whether the type looks like it might be `std::borrow::Cow<T>` where elem="T".
|
||||
@@ -1192,8 +1185,8 @@ fn is_cow(ty: &syn::Ty, elem: &str) -> bool {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
seg.ident == "Cow" && params.lifetimes.len() == 1 &&
|
||||
params.types == vec![syn::parse_type(elem).unwrap()] && params.bindings.is_empty()
|
||||
seg.ident == "Cow" && params.lifetimes.len() == 1
|
||||
&& params.types == vec![syn::parse_type(elem).unwrap()] && params.bindings.is_empty()
|
||||
}
|
||||
|
||||
// Whether the type looks like it might be `&T` where elem="T". This can have
|
||||
@@ -1219,8 +1212,8 @@ fn is_cow(ty: &syn::Ty, elem: &str) -> bool {
|
||||
fn is_rptr(ty: &syn::Ty, elem: &str) -> bool {
|
||||
match *ty {
|
||||
syn::Ty::Rptr(Some(_), ref mut_ty) => {
|
||||
mut_ty.mutability == syn::Mutability::Immutable &&
|
||||
mut_ty.ty == syn::parse_type(elem).unwrap()
|
||||
mut_ty.mutability == syn::Mutability::Immutable
|
||||
&& mut_ty.ty == syn::parse_type(elem).unwrap()
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
@@ -1241,7 +1234,7 @@ fn borrowable_lifetimes(
|
||||
let mut lifetimes = BTreeSet::new();
|
||||
collect_lifetimes(ty, &mut lifetimes);
|
||||
if lifetimes.is_empty() {
|
||||
Err(cx.error(format!("field `{}` has no lifetimes to borrow", name)),)
|
||||
Err(cx.error(format!("field `{}` has no lifetimes to borrow", name)))
|
||||
} else {
|
||||
Ok(lifetimes)
|
||||
}
|
||||
@@ -1249,9 +1242,7 @@ fn borrowable_lifetimes(
|
||||
|
||||
fn collect_lifetimes(ty: &syn::Ty, out: &mut BTreeSet<syn::Lifetime>) {
|
||||
match *ty {
|
||||
syn::Ty::Slice(ref elem) |
|
||||
syn::Ty::Array(ref elem, _) |
|
||||
syn::Ty::Paren(ref elem) => {
|
||||
syn::Ty::Slice(ref elem) | syn::Ty::Array(ref elem, _) | syn::Ty::Paren(ref elem) => {
|
||||
collect_lifetimes(elem, out);
|
||||
}
|
||||
syn::Ty::Ptr(ref elem) => {
|
||||
@@ -1261,11 +1252,9 @@ fn collect_lifetimes(ty: &syn::Ty, out: &mut BTreeSet<syn::Lifetime>) {
|
||||
out.extend(lifetime.iter().cloned());
|
||||
collect_lifetimes(&elem.ty, out);
|
||||
}
|
||||
syn::Ty::Tup(ref elems) => {
|
||||
for elem in elems {
|
||||
collect_lifetimes(elem, out);
|
||||
}
|
||||
}
|
||||
syn::Ty::Tup(ref elems) => for elem in elems {
|
||||
collect_lifetimes(elem, out);
|
||||
},
|
||||
syn::Ty::Path(ref qself, ref path) => {
|
||||
if let Some(ref qself) = *qself {
|
||||
collect_lifetimes(&qself.ty, out);
|
||||
@@ -1282,11 +1271,11 @@ fn collect_lifetimes(ty: &syn::Ty, out: &mut BTreeSet<syn::Lifetime>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
syn::Ty::BareFn(_) |
|
||||
syn::Ty::Never |
|
||||
syn::Ty::TraitObject(_) |
|
||||
syn::Ty::ImplTrait(_) |
|
||||
syn::Ty::Infer |
|
||||
syn::Ty::Mac(_) => {}
|
||||
syn::Ty::BareFn(_)
|
||||
| syn::Ty::Never
|
||||
| syn::Ty::TraitObject(_)
|
||||
| syn::Ty::ImplTrait(_)
|
||||
| syn::Ty::Infer
|
||||
| syn::Ty::Mac(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ pub enum RenameRule {
|
||||
/// Rename direct children to "kebab-case" style.
|
||||
KebabCase,
|
||||
/// Rename direct children to "SCREAMING-KEBAB-CASE" style.
|
||||
ScreamingKebabCase
|
||||
ScreamingKebabCase,
|
||||
}
|
||||
|
||||
impl RenameRule {
|
||||
@@ -52,7 +52,9 @@ impl RenameRule {
|
||||
}
|
||||
ScreamingSnakeCase => SnakeCase.apply_to_variant(variant).to_ascii_uppercase(),
|
||||
KebabCase => SnakeCase.apply_to_variant(variant).replace('_', "-"),
|
||||
ScreamingKebabCase => ScreamingSnakeCase.apply_to_variant(variant).replace('_', "-")
|
||||
ScreamingKebabCase => ScreamingSnakeCase
|
||||
.apply_to_variant(variant)
|
||||
.replace('_', "-"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +82,7 @@ impl RenameRule {
|
||||
}
|
||||
ScreamingSnakeCase => field.to_ascii_uppercase(),
|
||||
KebabCase => field.replace('_', "-"),
|
||||
ScreamingKebabCase => ScreamingSnakeCase.apply_to_field(field).replace('_', "-")
|
||||
ScreamingKebabCase => ScreamingSnakeCase.apply_to_field(field).replace('_', "-"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -104,13 +106,28 @@ impl FromStr for RenameRule {
|
||||
|
||||
#[test]
|
||||
fn rename_variants() {
|
||||
for &(original, lower, camel, snake, screaming, kebab, screaming_kebab) in
|
||||
&[
|
||||
("Outcome", "outcome", "outcome", "outcome", "OUTCOME", "outcome", "OUTCOME"),
|
||||
("VeryTasty", "verytasty", "veryTasty", "very_tasty", "VERY_TASTY", "very-tasty", "VERY-TASTY"),
|
||||
("A", "a", "a", "a", "A", "a", "A"),
|
||||
("Z42", "z42", "z42", "z42", "Z42", "z42", "Z42"),
|
||||
] {
|
||||
for &(original, lower, camel, snake, screaming, kebab, screaming_kebab) in &[
|
||||
(
|
||||
"Outcome",
|
||||
"outcome",
|
||||
"outcome",
|
||||
"outcome",
|
||||
"OUTCOME",
|
||||
"outcome",
|
||||
"OUTCOME",
|
||||
),
|
||||
(
|
||||
"VeryTasty",
|
||||
"verytasty",
|
||||
"veryTasty",
|
||||
"very_tasty",
|
||||
"VERY_TASTY",
|
||||
"very-tasty",
|
||||
"VERY-TASTY",
|
||||
),
|
||||
("A", "a", "a", "a", "A", "a", "A"),
|
||||
("Z42", "z42", "z42", "z42", "Z42", "z42", "Z42"),
|
||||
] {
|
||||
assert_eq!(None.apply_to_variant(original), original);
|
||||
assert_eq!(LowerCase.apply_to_variant(original), lower);
|
||||
assert_eq!(PascalCase.apply_to_variant(original), original);
|
||||
@@ -118,19 +135,35 @@ fn rename_variants() {
|
||||
assert_eq!(SnakeCase.apply_to_variant(original), snake);
|
||||
assert_eq!(ScreamingSnakeCase.apply_to_variant(original), screaming);
|
||||
assert_eq!(KebabCase.apply_to_variant(original), kebab);
|
||||
assert_eq!(ScreamingKebabCase.apply_to_variant(original), screaming_kebab);
|
||||
assert_eq!(
|
||||
ScreamingKebabCase.apply_to_variant(original),
|
||||
screaming_kebab
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rename_fields() {
|
||||
for &(original, pascal, camel, screaming, kebab, screaming_kebab) in
|
||||
&[
|
||||
("outcome", "Outcome", "outcome", "OUTCOME", "outcome", "OUTCOME"),
|
||||
("very_tasty", "VeryTasty", "veryTasty", "VERY_TASTY", "very-tasty", "VERY-TASTY"),
|
||||
("a", "A", "a", "A", "a", "A"),
|
||||
("z42", "Z42", "z42", "Z42", "z42", "Z42"),
|
||||
] {
|
||||
for &(original, pascal, camel, screaming, kebab, screaming_kebab) in &[
|
||||
(
|
||||
"outcome",
|
||||
"Outcome",
|
||||
"outcome",
|
||||
"OUTCOME",
|
||||
"outcome",
|
||||
"OUTCOME",
|
||||
),
|
||||
(
|
||||
"very_tasty",
|
||||
"VeryTasty",
|
||||
"veryTasty",
|
||||
"VERY_TASTY",
|
||||
"very-tasty",
|
||||
"VERY-TASTY",
|
||||
),
|
||||
("a", "A", "a", "A", "a", "A"),
|
||||
("z42", "Z42", "z42", "Z42", "z42", "Z42"),
|
||||
] {
|
||||
assert_eq!(None.apply_to_field(original), original);
|
||||
assert_eq!(PascalCase.apply_to_field(original), pascal);
|
||||
assert_eq!(CamelCase.apply_to_field(original), camel);
|
||||
|
||||
@@ -31,7 +31,7 @@ fn check_getter(cx: &Ctxt, cont: &Container) {
|
||||
if cont.body.has_getter() && cont.attrs.remote().is_none() {
|
||||
cx.error(
|
||||
"#[serde(getter = \"...\")] can only be used in structs \
|
||||
that have #[serde(remote = \"...\")]",
|
||||
that have #[serde(remote = \"...\")]",
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -53,10 +53,13 @@ fn check_identifier(cx: &Ctxt, cont: &Container) {
|
||||
};
|
||||
|
||||
for (i, variant) in variants.iter().enumerate() {
|
||||
match (variant.style, cont.attrs.identifier(), variant.attrs.other()) {
|
||||
match (
|
||||
variant.style,
|
||||
cont.attrs.identifier(),
|
||||
variant.attrs.other(),
|
||||
) {
|
||||
// The `other` attribute may only be used in a field_identifier.
|
||||
(_, Identifier::Variant, true) |
|
||||
(_, Identifier::No, true) => {
|
||||
(_, Identifier::Variant, true) | (_, Identifier::No, true) => {
|
||||
cx.error("#[serde(other)] may only be used inside a field_identifier");
|
||||
}
|
||||
|
||||
@@ -109,42 +112,58 @@ fn check_variant_skip_attrs(cx: &Ctxt, cont: &Container) {
|
||||
for variant in variants.iter() {
|
||||
if variant.attrs.serialize_with().is_some() {
|
||||
if variant.attrs.skip_serializing() {
|
||||
cx.error(format!("variant `{}` cannot have both #[serde(serialize_with)] and \
|
||||
#[serde(skip_serializing)]", variant.ident));
|
||||
cx.error(format!(
|
||||
"variant `{}` cannot have both #[serde(serialize_with)] and \
|
||||
#[serde(skip_serializing)]",
|
||||
variant.ident
|
||||
));
|
||||
}
|
||||
|
||||
for (i, field) in variant.fields.iter().enumerate() {
|
||||
let ident = field.ident.as_ref().map_or_else(|| format!("{}", i),
|
||||
|ident| format!("`{}`", ident));
|
||||
let ident = field
|
||||
.ident
|
||||
.as_ref()
|
||||
.map_or_else(|| format!("{}", i), |ident| format!("`{}`", ident));
|
||||
|
||||
if field.attrs.skip_serializing() {
|
||||
cx.error(format!("variant `{}` cannot have both #[serde(serialize_with)] and \
|
||||
a field {} marked with #[serde(skip_serializing)]",
|
||||
variant.ident, ident));
|
||||
cx.error(format!(
|
||||
"variant `{}` cannot have both #[serde(serialize_with)] and \
|
||||
a field {} marked with #[serde(skip_serializing)]",
|
||||
variant.ident, ident
|
||||
));
|
||||
}
|
||||
|
||||
if field.attrs.skip_serializing_if().is_some() {
|
||||
cx.error(format!("variant `{}` cannot have both #[serde(serialize_with)] and \
|
||||
a field {} marked with #[serde(skip_serializing_if)]",
|
||||
variant.ident, ident));
|
||||
cx.error(format!(
|
||||
"variant `{}` cannot have both #[serde(serialize_with)] and \
|
||||
a field {} marked with #[serde(skip_serializing_if)]",
|
||||
variant.ident, ident
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if variant.attrs.deserialize_with().is_some() {
|
||||
if variant.attrs.skip_deserializing() {
|
||||
cx.error(format!("variant `{}` cannot have both #[serde(deserialize_with)] and \
|
||||
#[serde(skip_deserializing)]", variant.ident));
|
||||
cx.error(format!(
|
||||
"variant `{}` cannot have both #[serde(deserialize_with)] and \
|
||||
#[serde(skip_deserializing)]",
|
||||
variant.ident
|
||||
));
|
||||
}
|
||||
|
||||
for (i, field) in variant.fields.iter().enumerate() {
|
||||
if field.attrs.skip_deserializing() {
|
||||
let ident = field.ident.as_ref().map_or_else(|| format!("{}", i),
|
||||
|ident| format!("`{}`", ident));
|
||||
let ident = field
|
||||
.ident
|
||||
.as_ref()
|
||||
.map_or_else(|| format!("{}", i), |ident| format!("`{}`", ident));
|
||||
|
||||
cx.error(format!("variant `{}` cannot have both #[serde(deserialize_with)] \
|
||||
and a field {} marked with #[serde(skip_deserializing)]",
|
||||
variant.ident, ident));
|
||||
cx.error(format!(
|
||||
"variant `{}` cannot have both #[serde(deserialize_with)] \
|
||||
and a field {} marked with #[serde(skip_deserializing)]",
|
||||
variant.ident, ident
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,9 @@ pub struct Ctxt {
|
||||
|
||||
impl Ctxt {
|
||||
pub fn new() -> Self {
|
||||
Ctxt { errors: RefCell::new(Some(Vec::new())) }
|
||||
Ctxt {
|
||||
errors: RefCell::new(Some(Vec::new())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn error<T: Display>(&self, msg: T) {
|
||||
|
||||
Reference in New Issue
Block a user