mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-05-01 11:18:01 +00:00
Remove use of ref keyword from serde_derive
This commit is contained in:
+135
-173
@@ -311,9 +311,9 @@ impl Container {
|
||||
.flat_map(|attr| get_serde_meta_items(cx, attr))
|
||||
.flatten()
|
||||
{
|
||||
match meta_item {
|
||||
match &meta_item {
|
||||
// Parse `#[serde(rename = "foo")]`
|
||||
Meta(NameValue(ref m)) if m.path == RENAME => {
|
||||
Meta(NameValue(m)) if m.path == RENAME => {
|
||||
if let Ok(s) = get_lit_str(cx, RENAME, &m.lit) {
|
||||
ser_name.set(&m.path, s.value());
|
||||
de_name.set(&m.path, s.value());
|
||||
@@ -321,7 +321,7 @@ impl Container {
|
||||
}
|
||||
|
||||
// Parse `#[serde(rename(serialize = "foo", deserialize = "bar"))]`
|
||||
Meta(List(ref m)) if m.path == RENAME => {
|
||||
Meta(List(m)) if m.path == RENAME => {
|
||||
if let Ok((ser, de)) = get_renames(cx, &m.nested) {
|
||||
ser_name.set_opt(&m.path, ser.map(syn::LitStr::value));
|
||||
de_name.set_opt(&m.path, de.map(syn::LitStr::value));
|
||||
@@ -329,7 +329,7 @@ impl Container {
|
||||
}
|
||||
|
||||
// Parse `#[serde(rename_all = "foo")]`
|
||||
Meta(NameValue(ref m)) if m.path == RENAME_ALL => {
|
||||
Meta(NameValue(m)) if m.path == RENAME_ALL => {
|
||||
if let Ok(s) = get_lit_str(cx, RENAME_ALL, &m.lit) {
|
||||
match RenameRule::from_str(&s.value()) {
|
||||
Ok(rename_rule) => {
|
||||
@@ -348,7 +348,7 @@ impl Container {
|
||||
}
|
||||
|
||||
// Parse `#[serde(rename_all(serialize = "foo", deserialize = "bar"))]`
|
||||
Meta(List(ref m)) if m.path == RENAME_ALL => {
|
||||
Meta(List(m)) if m.path == RENAME_ALL => {
|
||||
if let Ok((ser, de)) = get_renames(cx, &m.nested) {
|
||||
if let Some(ser) = ser {
|
||||
match RenameRule::from_str(&ser.value()) {
|
||||
@@ -378,18 +378,18 @@ impl Container {
|
||||
}
|
||||
|
||||
// Parse `#[serde(transparent)]`
|
||||
Meta(Path(ref word)) if word == TRANSPARENT => {
|
||||
Meta(Path(word)) if word == TRANSPARENT => {
|
||||
transparent.set_true(word);
|
||||
}
|
||||
|
||||
// Parse `#[serde(deny_unknown_fields)]`
|
||||
Meta(Path(ref word)) if word == DENY_UNKNOWN_FIELDS => {
|
||||
Meta(Path(word)) if word == DENY_UNKNOWN_FIELDS => {
|
||||
deny_unknown_fields.set_true(word);
|
||||
}
|
||||
|
||||
// Parse `#[serde(default)]`
|
||||
Meta(Path(ref word)) if word == DEFAULT => match item.data {
|
||||
syn::Data::Struct(syn::DataStruct { ref fields, .. }) => match *fields {
|
||||
Meta(Path(word)) if word == DEFAULT => match &item.data {
|
||||
syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {
|
||||
syn::Fields::Named(_) => {
|
||||
default.set(word, Default::Default);
|
||||
}
|
||||
@@ -398,24 +398,22 @@ impl Container {
|
||||
"#[serde(default)] can only be used on structs with named fields",
|
||||
),
|
||||
},
|
||||
syn::Data::Enum(syn::DataEnum { ref enum_token, .. }) => cx.error_spanned_by(
|
||||
syn::Data::Enum(syn::DataEnum { enum_token, .. }) => cx.error_spanned_by(
|
||||
enum_token,
|
||||
"#[serde(default)] can only be used on structs with named fields",
|
||||
),
|
||||
syn::Data::Union(syn::DataUnion {
|
||||
ref union_token, ..
|
||||
}) => cx.error_spanned_by(
|
||||
syn::Data::Union(syn::DataUnion { union_token, .. }) => cx.error_spanned_by(
|
||||
union_token,
|
||||
"#[serde(default)] can only be used on structs with named fields",
|
||||
),
|
||||
},
|
||||
|
||||
// Parse `#[serde(default = "...")]`
|
||||
Meta(NameValue(ref m)) if m.path == DEFAULT => {
|
||||
Meta(NameValue(m)) if m.path == DEFAULT => {
|
||||
if let Ok(path) = parse_lit_into_expr_path(cx, DEFAULT, &m.lit) {
|
||||
match item.data {
|
||||
syn::Data::Struct(syn::DataStruct { ref fields, .. }) => {
|
||||
match *fields {
|
||||
match &item.data {
|
||||
syn::Data::Struct(syn::DataStruct { fields, .. }) => {
|
||||
match fields {
|
||||
syn::Fields::Named(_) => {
|
||||
default.set(&m.path, Default::Path(path));
|
||||
}
|
||||
@@ -426,13 +424,13 @@ impl Container {
|
||||
),
|
||||
}
|
||||
}
|
||||
syn::Data::Enum(syn::DataEnum { ref enum_token, .. }) => cx
|
||||
syn::Data::Enum(syn::DataEnum { enum_token, .. }) => cx
|
||||
.error_spanned_by(
|
||||
enum_token,
|
||||
"#[serde(default = \"...\")] can only be used on structs with named fields",
|
||||
),
|
||||
syn::Data::Union(syn::DataUnion {
|
||||
ref union_token, ..
|
||||
union_token, ..
|
||||
}) => cx.error_spanned_by(
|
||||
union_token,
|
||||
"#[serde(default = \"...\")] can only be used on structs with named fields",
|
||||
@@ -442,7 +440,7 @@ impl Container {
|
||||
}
|
||||
|
||||
// Parse `#[serde(bound = "T: SomeBound")]`
|
||||
Meta(NameValue(ref m)) if m.path == BOUND => {
|
||||
Meta(NameValue(m)) if m.path == BOUND => {
|
||||
if let Ok(where_predicates) = parse_lit_into_where(cx, BOUND, BOUND, &m.lit) {
|
||||
ser_bound.set(&m.path, where_predicates.clone());
|
||||
de_bound.set(&m.path, where_predicates);
|
||||
@@ -450,7 +448,7 @@ impl Container {
|
||||
}
|
||||
|
||||
// Parse `#[serde(bound(serialize = "...", deserialize = "..."))]`
|
||||
Meta(List(ref m)) if m.path == BOUND => {
|
||||
Meta(List(m)) if m.path == BOUND => {
|
||||
if let Ok((ser, de)) = get_where_predicates(cx, &m.nested) {
|
||||
ser_bound.set_opt(&m.path, ser);
|
||||
de_bound.set_opt(&m.path, de);
|
||||
@@ -458,21 +456,17 @@ impl Container {
|
||||
}
|
||||
|
||||
// Parse `#[serde(untagged)]`
|
||||
Meta(Path(ref word)) if word == UNTAGGED => match item.data {
|
||||
Meta(Path(word)) if word == UNTAGGED => match item.data {
|
||||
syn::Data::Enum(_) => {
|
||||
untagged.set_true(word);
|
||||
}
|
||||
syn::Data::Struct(syn::DataStruct {
|
||||
ref struct_token, ..
|
||||
}) => {
|
||||
syn::Data::Struct(syn::DataStruct { struct_token, .. }) => {
|
||||
cx.error_spanned_by(
|
||||
struct_token,
|
||||
"#[serde(untagged)] can only be used on enums",
|
||||
);
|
||||
}
|
||||
syn::Data::Union(syn::DataUnion {
|
||||
ref union_token, ..
|
||||
}) => {
|
||||
syn::Data::Union(syn::DataUnion { union_token, .. }) => {
|
||||
cx.error_spanned_by(
|
||||
union_token,
|
||||
"#[serde(untagged)] can only be used on enums",
|
||||
@@ -481,28 +475,24 @@ impl Container {
|
||||
},
|
||||
|
||||
// Parse `#[serde(tag = "type")]`
|
||||
Meta(NameValue(ref m)) if m.path == TAG => {
|
||||
Meta(NameValue(m)) if m.path == TAG => {
|
||||
if let Ok(s) = get_lit_str(cx, TAG, &m.lit) {
|
||||
match item.data {
|
||||
match &item.data {
|
||||
syn::Data::Enum(_) => {
|
||||
internal_tag.set(&m.path, s.value());
|
||||
}
|
||||
syn::Data::Struct(syn::DataStruct { ref fields, .. }) => {
|
||||
match *fields {
|
||||
syn::Fields::Named(_) => {
|
||||
internal_tag.set(&m.path, s.value());
|
||||
}
|
||||
syn::Fields::Unnamed(_) | syn::Fields::Unit => {
|
||||
cx.error_spanned_by(
|
||||
syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {
|
||||
syn::Fields::Named(_) => {
|
||||
internal_tag.set(&m.path, s.value());
|
||||
}
|
||||
syn::Fields::Unnamed(_) | syn::Fields::Unit => {
|
||||
cx.error_spanned_by(
|
||||
fields,
|
||||
"#[serde(tag = \"...\")] can only be used on enums and structs with named fields",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
syn::Data::Union(syn::DataUnion {
|
||||
ref union_token, ..
|
||||
}) => {
|
||||
},
|
||||
syn::Data::Union(syn::DataUnion { union_token, .. }) => {
|
||||
cx.error_spanned_by(
|
||||
union_token,
|
||||
"#[serde(tag = \"...\")] can only be used on enums and structs with named fields",
|
||||
@@ -513,23 +503,19 @@ impl Container {
|
||||
}
|
||||
|
||||
// Parse `#[serde(content = "c")]`
|
||||
Meta(NameValue(ref m)) if m.path == CONTENT => {
|
||||
Meta(NameValue(m)) if m.path == CONTENT => {
|
||||
if let Ok(s) = get_lit_str(cx, CONTENT, &m.lit) {
|
||||
match item.data {
|
||||
match &item.data {
|
||||
syn::Data::Enum(_) => {
|
||||
content.set(&m.path, s.value());
|
||||
}
|
||||
syn::Data::Struct(syn::DataStruct {
|
||||
ref struct_token, ..
|
||||
}) => {
|
||||
syn::Data::Struct(syn::DataStruct { struct_token, .. }) => {
|
||||
cx.error_spanned_by(
|
||||
struct_token,
|
||||
"#[serde(content = \"...\")] can only be used on enums",
|
||||
);
|
||||
}
|
||||
syn::Data::Union(syn::DataUnion {
|
||||
ref union_token, ..
|
||||
}) => {
|
||||
syn::Data::Union(syn::DataUnion { union_token, .. }) => {
|
||||
cx.error_spanned_by(
|
||||
union_token,
|
||||
"#[serde(content = \"...\")] can only be used on enums",
|
||||
@@ -540,28 +526,28 @@ impl Container {
|
||||
}
|
||||
|
||||
// Parse `#[serde(from = "Type")]
|
||||
Meta(NameValue(ref m)) if m.path == FROM => {
|
||||
Meta(NameValue(m)) if m.path == FROM => {
|
||||
if let Ok(from_ty) = parse_lit_into_ty(cx, FROM, &m.lit) {
|
||||
type_from.set_opt(&m.path, Some(from_ty));
|
||||
}
|
||||
}
|
||||
|
||||
// Parse `#[serde(try_from = "Type")]
|
||||
Meta(NameValue(ref m)) if m.path == TRY_FROM => {
|
||||
Meta(NameValue(m)) if m.path == TRY_FROM => {
|
||||
if let Ok(try_from_ty) = parse_lit_into_ty(cx, TRY_FROM, &m.lit) {
|
||||
type_try_from.set_opt(&m.path, Some(try_from_ty));
|
||||
}
|
||||
}
|
||||
|
||||
// Parse `#[serde(into = "Type")]
|
||||
Meta(NameValue(ref m)) if m.path == INTO => {
|
||||
Meta(NameValue(m)) if m.path == INTO => {
|
||||
if let Ok(into_ty) = parse_lit_into_ty(cx, INTO, &m.lit) {
|
||||
type_into.set_opt(&m.path, Some(into_ty));
|
||||
}
|
||||
}
|
||||
|
||||
// Parse `#[serde(remote = "...")]`
|
||||
Meta(NameValue(ref m)) if m.path == REMOTE => {
|
||||
Meta(NameValue(m)) if m.path == REMOTE => {
|
||||
if let Ok(path) = parse_lit_into_path(cx, REMOTE, &m.lit) {
|
||||
if is_primitive_path(&path, "Self") {
|
||||
remote.set(&m.path, item.ident.clone().into());
|
||||
@@ -572,23 +558,23 @@ impl Container {
|
||||
}
|
||||
|
||||
// Parse `#[serde(field_identifier)]`
|
||||
Meta(Path(ref word)) if word == FIELD_IDENTIFIER => {
|
||||
Meta(Path(word)) if word == FIELD_IDENTIFIER => {
|
||||
field_identifier.set_true(word);
|
||||
}
|
||||
|
||||
// Parse `#[serde(variant_identifier)]`
|
||||
Meta(Path(ref word)) if word == VARIANT_IDENTIFIER => {
|
||||
Meta(Path(word)) if word == VARIANT_IDENTIFIER => {
|
||||
variant_identifier.set_true(word);
|
||||
}
|
||||
|
||||
// Parse `#[serde(crate = "foo")]`
|
||||
Meta(NameValue(ref m)) if m.path == CRATE => {
|
||||
Meta(NameValue(m)) if m.path == CRATE => {
|
||||
if let Ok(path) = parse_lit_into_path(cx, CRATE, &m.lit) {
|
||||
serde_path.set(&m.path, path)
|
||||
}
|
||||
}
|
||||
|
||||
Meta(ref meta_item) => {
|
||||
Meta(meta_item) => {
|
||||
let path = meta_item
|
||||
.path()
|
||||
.into_token_stream()
|
||||
@@ -600,7 +586,7 @@ impl Container {
|
||||
);
|
||||
}
|
||||
|
||||
Lit(ref lit) => {
|
||||
Lit(lit) => {
|
||||
cx.error_spanned_by(lit, "unexpected literal in serde container attribute");
|
||||
}
|
||||
}
|
||||
@@ -714,11 +700,11 @@ fn decide_tag(
|
||||
(Some(_), None, None) => TagType::None,
|
||||
(None, Some((_, tag)), None) => {
|
||||
// Check that there are no tuple variants.
|
||||
if let syn::Data::Enum(ref data) = item.data {
|
||||
if let syn::Data::Enum(data) = &item.data {
|
||||
for variant in &data.variants {
|
||||
match variant.fields {
|
||||
match &variant.fields {
|
||||
syn::Fields::Named(_) | syn::Fields::Unit => {}
|
||||
syn::Fields::Unnamed(ref fields) => {
|
||||
syn::Fields::Unnamed(fields) => {
|
||||
if fields.unnamed.len() != 1 {
|
||||
cx.error_spanned_by(
|
||||
variant,
|
||||
@@ -806,54 +792,30 @@ fn decide_identifier(
|
||||
);
|
||||
Identifier::No
|
||||
}
|
||||
(&syn::Data::Enum(_), Some(_), None) => Identifier::Field,
|
||||
(&syn::Data::Enum(_), None, Some(_)) => Identifier::Variant,
|
||||
(
|
||||
&syn::Data::Struct(syn::DataStruct {
|
||||
ref struct_token, ..
|
||||
}),
|
||||
Some(_),
|
||||
None,
|
||||
) => {
|
||||
(syn::Data::Enum(_), Some(_), None) => Identifier::Field,
|
||||
(syn::Data::Enum(_), None, Some(_)) => Identifier::Variant,
|
||||
(syn::Data::Struct(syn::DataStruct { struct_token, .. }), Some(_), None) => {
|
||||
cx.error_spanned_by(
|
||||
struct_token,
|
||||
"#[serde(field_identifier)] can only be used on an enum",
|
||||
);
|
||||
Identifier::No
|
||||
}
|
||||
(
|
||||
&syn::Data::Union(syn::DataUnion {
|
||||
ref union_token, ..
|
||||
}),
|
||||
Some(_),
|
||||
None,
|
||||
) => {
|
||||
(syn::Data::Union(syn::DataUnion { union_token, .. }), Some(_), None) => {
|
||||
cx.error_spanned_by(
|
||||
union_token,
|
||||
"#[serde(field_identifier)] can only be used on an enum",
|
||||
);
|
||||
Identifier::No
|
||||
}
|
||||
(
|
||||
&syn::Data::Struct(syn::DataStruct {
|
||||
ref struct_token, ..
|
||||
}),
|
||||
None,
|
||||
Some(_),
|
||||
) => {
|
||||
(syn::Data::Struct(syn::DataStruct { struct_token, .. }), None, Some(_)) => {
|
||||
cx.error_spanned_by(
|
||||
struct_token,
|
||||
"#[serde(variant_identifier)] can only be used on an enum",
|
||||
);
|
||||
Identifier::No
|
||||
}
|
||||
(
|
||||
&syn::Data::Union(syn::DataUnion {
|
||||
ref union_token, ..
|
||||
}),
|
||||
None,
|
||||
Some(_),
|
||||
) => {
|
||||
(syn::Data::Union(syn::DataUnion { union_token, .. }), None, Some(_)) => {
|
||||
cx.error_spanned_by(
|
||||
union_token,
|
||||
"#[serde(variant_identifier)] can only be used on an enum",
|
||||
@@ -899,9 +861,9 @@ impl Variant {
|
||||
.flat_map(|attr| get_serde_meta_items(cx, attr))
|
||||
.flatten()
|
||||
{
|
||||
match meta_item {
|
||||
match &meta_item {
|
||||
// Parse `#[serde(rename = "foo")]`
|
||||
Meta(NameValue(ref m)) if m.path == RENAME => {
|
||||
Meta(NameValue(m)) if m.path == RENAME => {
|
||||
if let Ok(s) = get_lit_str(cx, RENAME, &m.lit) {
|
||||
ser_name.set(&m.path, s.value());
|
||||
de_name.set_if_none(s.value());
|
||||
@@ -910,7 +872,7 @@ impl Variant {
|
||||
}
|
||||
|
||||
// Parse `#[serde(rename(serialize = "foo", deserialize = "bar"))]`
|
||||
Meta(List(ref m)) if m.path == RENAME => {
|
||||
Meta(List(m)) if m.path == RENAME => {
|
||||
if let Ok((ser, de)) = get_multiple_renames(cx, &m.nested) {
|
||||
ser_name.set_opt(&m.path, ser.map(syn::LitStr::value));
|
||||
for de_value in de {
|
||||
@@ -921,14 +883,14 @@ impl Variant {
|
||||
}
|
||||
|
||||
// Parse `#[serde(alias = "foo")]`
|
||||
Meta(NameValue(ref m)) if m.path == ALIAS => {
|
||||
Meta(NameValue(m)) if m.path == ALIAS => {
|
||||
if let Ok(s) = get_lit_str(cx, ALIAS, &m.lit) {
|
||||
de_aliases.insert(&m.path, s.value());
|
||||
}
|
||||
}
|
||||
|
||||
// Parse `#[serde(rename_all = "foo")]`
|
||||
Meta(NameValue(ref m)) if m.path == RENAME_ALL => {
|
||||
Meta(NameValue(m)) if m.path == RENAME_ALL => {
|
||||
if let Ok(s) = get_lit_str(cx, RENAME_ALL, &m.lit) {
|
||||
match RenameRule::from_str(&s.value()) {
|
||||
Ok(rename_rule) => {
|
||||
@@ -947,7 +909,7 @@ impl Variant {
|
||||
}
|
||||
|
||||
// Parse `#[serde(rename_all(serialize = "foo", deserialize = "bar"))]`
|
||||
Meta(List(ref m)) if m.path == RENAME_ALL => {
|
||||
Meta(List(m)) if m.path == RENAME_ALL => {
|
||||
if let Ok((ser, de)) = get_renames(cx, &m.nested) {
|
||||
if let Some(ser) = ser {
|
||||
match RenameRule::from_str(&ser.value()) {
|
||||
@@ -977,28 +939,28 @@ impl Variant {
|
||||
}
|
||||
|
||||
// Parse `#[serde(skip)]`
|
||||
Meta(Path(ref word)) if word == SKIP => {
|
||||
Meta(Path(word)) if word == SKIP => {
|
||||
skip_serializing.set_true(word);
|
||||
skip_deserializing.set_true(word);
|
||||
}
|
||||
|
||||
// Parse `#[serde(skip_deserializing)]`
|
||||
Meta(Path(ref word)) if word == SKIP_DESERIALIZING => {
|
||||
Meta(Path(word)) if word == SKIP_DESERIALIZING => {
|
||||
skip_deserializing.set_true(word);
|
||||
}
|
||||
|
||||
// Parse `#[serde(skip_serializing)]`
|
||||
Meta(Path(ref word)) if word == SKIP_SERIALIZING => {
|
||||
Meta(Path(word)) if word == SKIP_SERIALIZING => {
|
||||
skip_serializing.set_true(word);
|
||||
}
|
||||
|
||||
// Parse `#[serde(other)]`
|
||||
Meta(Path(ref word)) if word == OTHER => {
|
||||
Meta(Path(word)) if word == OTHER => {
|
||||
other.set_true(word);
|
||||
}
|
||||
|
||||
// Parse `#[serde(bound = "T: SomeBound")]`
|
||||
Meta(NameValue(ref m)) if m.path == BOUND => {
|
||||
Meta(NameValue(m)) if m.path == BOUND => {
|
||||
if let Ok(where_predicates) = parse_lit_into_where(cx, BOUND, BOUND, &m.lit) {
|
||||
ser_bound.set(&m.path, where_predicates.clone());
|
||||
de_bound.set(&m.path, where_predicates);
|
||||
@@ -1006,7 +968,7 @@ impl Variant {
|
||||
}
|
||||
|
||||
// Parse `#[serde(bound(serialize = "...", deserialize = "..."))]`
|
||||
Meta(List(ref m)) if m.path == BOUND => {
|
||||
Meta(List(m)) if m.path == BOUND => {
|
||||
if let Ok((ser, de)) = get_where_predicates(cx, &m.nested) {
|
||||
ser_bound.set_opt(&m.path, ser);
|
||||
de_bound.set_opt(&m.path, de);
|
||||
@@ -1014,7 +976,7 @@ impl Variant {
|
||||
}
|
||||
|
||||
// Parse `#[serde(with = "...")]`
|
||||
Meta(NameValue(ref m)) if m.path == WITH => {
|
||||
Meta(NameValue(m)) if m.path == WITH => {
|
||||
if let Ok(path) = parse_lit_into_expr_path(cx, WITH, &m.lit) {
|
||||
let mut ser_path = path.clone();
|
||||
ser_path
|
||||
@@ -1032,22 +994,22 @@ impl Variant {
|
||||
}
|
||||
|
||||
// Parse `#[serde(serialize_with = "...")]`
|
||||
Meta(NameValue(ref m)) if m.path == SERIALIZE_WITH => {
|
||||
Meta(NameValue(m)) if m.path == SERIALIZE_WITH => {
|
||||
if let Ok(path) = parse_lit_into_expr_path(cx, SERIALIZE_WITH, &m.lit) {
|
||||
serialize_with.set(&m.path, path);
|
||||
}
|
||||
}
|
||||
|
||||
// Parse `#[serde(deserialize_with = "...")]`
|
||||
Meta(NameValue(ref m)) if m.path == DESERIALIZE_WITH => {
|
||||
Meta(NameValue(m)) if m.path == DESERIALIZE_WITH => {
|
||||
if let Ok(path) = parse_lit_into_expr_path(cx, DESERIALIZE_WITH, &m.lit) {
|
||||
deserialize_with.set(&m.path, path);
|
||||
}
|
||||
}
|
||||
|
||||
// Defer `#[serde(borrow)]` and `#[serde(borrow = "'a + 'b")]`
|
||||
Meta(ref m) if m.path() == BORROW => match variant.fields {
|
||||
syn::Fields::Unnamed(ref fields) if fields.unnamed.len() == 1 => {
|
||||
Meta(m) if m.path() == BORROW => match &variant.fields {
|
||||
syn::Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {
|
||||
borrow.set(m.path(), m.clone());
|
||||
}
|
||||
_ => {
|
||||
@@ -1058,7 +1020,7 @@ impl Variant {
|
||||
}
|
||||
},
|
||||
|
||||
Meta(ref meta_item) => {
|
||||
Meta(meta_item) => {
|
||||
let path = meta_item
|
||||
.path()
|
||||
.into_token_stream()
|
||||
@@ -1070,7 +1032,7 @@ impl Variant {
|
||||
);
|
||||
}
|
||||
|
||||
Lit(ref lit) => {
|
||||
Lit(lit) => {
|
||||
cx.error_spanned_by(lit, "unexpected literal in serde variant attribute");
|
||||
}
|
||||
}
|
||||
@@ -1172,7 +1134,7 @@ pub enum Default {
|
||||
|
||||
impl Default {
|
||||
pub fn is_none(&self) -> bool {
|
||||
match *self {
|
||||
match self {
|
||||
Default::None => true,
|
||||
Default::Default | Default::Path(_) => false,
|
||||
}
|
||||
@@ -1203,8 +1165,8 @@ impl Field {
|
||||
let mut getter = Attr::none(cx, GETTER);
|
||||
let mut flatten = BoolAttr::none(cx, FLATTEN);
|
||||
|
||||
let ident = match field.ident {
|
||||
Some(ref ident) => unraw(ident),
|
||||
let ident = match &field.ident {
|
||||
Some(ident) => unraw(ident),
|
||||
None => index.to_string(),
|
||||
};
|
||||
|
||||
@@ -1219,9 +1181,9 @@ impl Field {
|
||||
.flatten()
|
||||
.chain(variant_borrow)
|
||||
{
|
||||
match meta_item {
|
||||
match &meta_item {
|
||||
// Parse `#[serde(rename = "foo")]`
|
||||
Meta(NameValue(ref m)) if m.path == RENAME => {
|
||||
Meta(NameValue(m)) if m.path == RENAME => {
|
||||
if let Ok(s) = get_lit_str(cx, RENAME, &m.lit) {
|
||||
ser_name.set(&m.path, s.value());
|
||||
de_name.set_if_none(s.value());
|
||||
@@ -1230,7 +1192,7 @@ impl Field {
|
||||
}
|
||||
|
||||
// Parse `#[serde(rename(serialize = "foo", deserialize = "bar"))]`
|
||||
Meta(List(ref m)) if m.path == RENAME => {
|
||||
Meta(List(m)) if m.path == RENAME => {
|
||||
if let Ok((ser, de)) = get_multiple_renames(cx, &m.nested) {
|
||||
ser_name.set_opt(&m.path, ser.map(syn::LitStr::value));
|
||||
for de_value in de {
|
||||
@@ -1241,63 +1203,63 @@ impl Field {
|
||||
}
|
||||
|
||||
// Parse `#[serde(alias = "foo")]`
|
||||
Meta(NameValue(ref m)) if m.path == ALIAS => {
|
||||
Meta(NameValue(m)) if m.path == ALIAS => {
|
||||
if let Ok(s) = get_lit_str(cx, ALIAS, &m.lit) {
|
||||
de_aliases.insert(&m.path, s.value());
|
||||
}
|
||||
}
|
||||
|
||||
// Parse `#[serde(default)]`
|
||||
Meta(Path(ref word)) if word == DEFAULT => {
|
||||
Meta(Path(word)) if word == DEFAULT => {
|
||||
default.set(word, Default::Default);
|
||||
}
|
||||
|
||||
// Parse `#[serde(default = "...")]`
|
||||
Meta(NameValue(ref m)) if m.path == DEFAULT => {
|
||||
Meta(NameValue(m)) if m.path == DEFAULT => {
|
||||
if let Ok(path) = parse_lit_into_expr_path(cx, DEFAULT, &m.lit) {
|
||||
default.set(&m.path, Default::Path(path));
|
||||
}
|
||||
}
|
||||
|
||||
// Parse `#[serde(skip_serializing)]`
|
||||
Meta(Path(ref word)) if word == SKIP_SERIALIZING => {
|
||||
Meta(Path(word)) if word == SKIP_SERIALIZING => {
|
||||
skip_serializing.set_true(word);
|
||||
}
|
||||
|
||||
// Parse `#[serde(skip_deserializing)]`
|
||||
Meta(Path(ref word)) if word == SKIP_DESERIALIZING => {
|
||||
Meta(Path(word)) if word == SKIP_DESERIALIZING => {
|
||||
skip_deserializing.set_true(word);
|
||||
}
|
||||
|
||||
// Parse `#[serde(skip)]`
|
||||
Meta(Path(ref word)) if word == SKIP => {
|
||||
Meta(Path(word)) if word == SKIP => {
|
||||
skip_serializing.set_true(word);
|
||||
skip_deserializing.set_true(word);
|
||||
}
|
||||
|
||||
// Parse `#[serde(skip_serializing_if = "...")]`
|
||||
Meta(NameValue(ref m)) if m.path == SKIP_SERIALIZING_IF => {
|
||||
Meta(NameValue(m)) if m.path == SKIP_SERIALIZING_IF => {
|
||||
if let Ok(path) = parse_lit_into_expr_path(cx, SKIP_SERIALIZING_IF, &m.lit) {
|
||||
skip_serializing_if.set(&m.path, path);
|
||||
}
|
||||
}
|
||||
|
||||
// Parse `#[serde(serialize_with = "...")]`
|
||||
Meta(NameValue(ref m)) if m.path == SERIALIZE_WITH => {
|
||||
Meta(NameValue(m)) if m.path == SERIALIZE_WITH => {
|
||||
if let Ok(path) = parse_lit_into_expr_path(cx, SERIALIZE_WITH, &m.lit) {
|
||||
serialize_with.set(&m.path, path);
|
||||
}
|
||||
}
|
||||
|
||||
// Parse `#[serde(deserialize_with = "...")]`
|
||||
Meta(NameValue(ref m)) if m.path == DESERIALIZE_WITH => {
|
||||
Meta(NameValue(m)) if m.path == DESERIALIZE_WITH => {
|
||||
if let Ok(path) = parse_lit_into_expr_path(cx, DESERIALIZE_WITH, &m.lit) {
|
||||
deserialize_with.set(&m.path, path);
|
||||
}
|
||||
}
|
||||
|
||||
// Parse `#[serde(with = "...")]`
|
||||
Meta(NameValue(ref m)) if m.path == WITH => {
|
||||
Meta(NameValue(m)) if m.path == WITH => {
|
||||
if let Ok(path) = parse_lit_into_expr_path(cx, WITH, &m.lit) {
|
||||
let mut ser_path = path.clone();
|
||||
ser_path
|
||||
@@ -1315,7 +1277,7 @@ impl Field {
|
||||
}
|
||||
|
||||
// Parse `#[serde(bound = "T: SomeBound")]`
|
||||
Meta(NameValue(ref m)) if m.path == BOUND => {
|
||||
Meta(NameValue(m)) if m.path == BOUND => {
|
||||
if let Ok(where_predicates) = parse_lit_into_where(cx, BOUND, BOUND, &m.lit) {
|
||||
ser_bound.set(&m.path, where_predicates.clone());
|
||||
de_bound.set(&m.path, where_predicates);
|
||||
@@ -1323,7 +1285,7 @@ impl Field {
|
||||
}
|
||||
|
||||
// Parse `#[serde(bound(serialize = "...", deserialize = "..."))]`
|
||||
Meta(List(ref m)) if m.path == BOUND => {
|
||||
Meta(List(m)) if m.path == BOUND => {
|
||||
if let Ok((ser, de)) = get_where_predicates(cx, &m.nested) {
|
||||
ser_bound.set_opt(&m.path, ser);
|
||||
de_bound.set_opt(&m.path, de);
|
||||
@@ -1331,14 +1293,14 @@ impl Field {
|
||||
}
|
||||
|
||||
// Parse `#[serde(borrow)]`
|
||||
Meta(Path(ref word)) if word == BORROW => {
|
||||
Meta(Path(word)) if word == BORROW => {
|
||||
if let Ok(borrowable) = borrowable_lifetimes(cx, &ident, field) {
|
||||
borrowed_lifetimes.set(word, borrowable);
|
||||
}
|
||||
}
|
||||
|
||||
// Parse `#[serde(borrow = "'a + 'b")]`
|
||||
Meta(NameValue(ref m)) if m.path == BORROW => {
|
||||
Meta(NameValue(m)) if m.path == BORROW => {
|
||||
if let Ok(lifetimes) = parse_lit_into_lifetimes(cx, BORROW, &m.lit) {
|
||||
if let Ok(borrowable) = borrowable_lifetimes(cx, &ident, field) {
|
||||
for lifetime in &lifetimes {
|
||||
@@ -1358,18 +1320,18 @@ impl Field {
|
||||
}
|
||||
|
||||
// Parse `#[serde(getter = "...")]`
|
||||
Meta(NameValue(ref m)) if m.path == GETTER => {
|
||||
Meta(NameValue(m)) if m.path == GETTER => {
|
||||
if let Ok(path) = parse_lit_into_expr_path(cx, GETTER, &m.lit) {
|
||||
getter.set(&m.path, path);
|
||||
}
|
||||
}
|
||||
|
||||
// Parse `#[serde(flatten)]`
|
||||
Meta(Path(ref word)) if word == FLATTEN => {
|
||||
Meta(Path(word)) if word == FLATTEN => {
|
||||
flatten.set_true(word);
|
||||
}
|
||||
|
||||
Meta(ref meta_item) => {
|
||||
Meta(meta_item) => {
|
||||
let path = meta_item
|
||||
.path()
|
||||
.into_token_stream()
|
||||
@@ -1381,7 +1343,7 @@ impl Field {
|
||||
);
|
||||
}
|
||||
|
||||
Lit(ref lit) => {
|
||||
Lit(lit) => {
|
||||
cx.error_spanned_by(lit, "unexpected literal in serde field attribute");
|
||||
}
|
||||
}
|
||||
@@ -1551,14 +1513,14 @@ where
|
||||
let mut de_meta = VecAttr::none(cx, attr_name);
|
||||
|
||||
for meta in metas {
|
||||
match *meta {
|
||||
Meta(NameValue(ref meta)) if meta.path == SERIALIZE => {
|
||||
match meta {
|
||||
Meta(NameValue(meta)) if meta.path == SERIALIZE => {
|
||||
if let Ok(v) = f(cx, attr_name, SERIALIZE, &meta.lit) {
|
||||
ser_meta.insert(&meta.path, v);
|
||||
}
|
||||
}
|
||||
|
||||
Meta(NameValue(ref meta)) if meta.path == DESERIALIZE => {
|
||||
Meta(NameValue(meta)) if meta.path == DESERIALIZE => {
|
||||
if let Ok(v) = f(cx, attr_name, DESERIALIZE, &meta.lit) {
|
||||
de_meta.insert(&meta.path, v);
|
||||
}
|
||||
@@ -1610,7 +1572,7 @@ pub fn get_serde_meta_items(cx: &Ctxt, attr: &syn::Attribute) -> Result<Vec<syn:
|
||||
}
|
||||
|
||||
match attr.parse_meta() {
|
||||
Ok(List(ref meta)) => Ok(meta.nested.iter().cloned().collect()),
|
||||
Ok(List(meta)) => Ok(meta.nested.into_iter().collect()),
|
||||
Ok(other) => {
|
||||
cx.error_spanned_by(other, "expected #[serde(...)]");
|
||||
Err(())
|
||||
@@ -1632,7 +1594,7 @@ fn get_lit_str2<'a>(
|
||||
meta_item_name: Symbol,
|
||||
lit: &'a syn::Lit,
|
||||
) -> Result<&'a syn::LitStr, ()> {
|
||||
if let syn::Lit::Str(ref lit) = *lit {
|
||||
if let syn::Lit::Str(lit) = lit {
|
||||
Ok(lit)
|
||||
} else {
|
||||
cx.error_spanned_by(
|
||||
@@ -1762,8 +1724,8 @@ fn is_implicitly_borrowed_reference(ty: &syn::Type) -> bool {
|
||||
// cow: Cow<'a, str>,
|
||||
// }
|
||||
fn is_cow(ty: &syn::Type, elem: fn(&syn::Type) -> bool) -> bool {
|
||||
let path = match *ty {
|
||||
syn::Type::Path(ref ty) => &ty.path,
|
||||
let path = match ty {
|
||||
syn::Type::Path(ty) => &ty.path,
|
||||
_ => {
|
||||
return false;
|
||||
}
|
||||
@@ -1774,8 +1736,8 @@ fn is_cow(ty: &syn::Type, elem: fn(&syn::Type) -> bool) -> bool {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
let args = match seg.arguments {
|
||||
syn::PathArguments::AngleBracketed(ref bracketed) => &bracketed.args,
|
||||
let args = match &seg.arguments {
|
||||
syn::PathArguments::AngleBracketed(bracketed) => &bracketed.args,
|
||||
_ => {
|
||||
return false;
|
||||
}
|
||||
@@ -1783,14 +1745,14 @@ fn is_cow(ty: &syn::Type, elem: fn(&syn::Type) -> bool) -> bool {
|
||||
seg.ident == "Cow"
|
||||
&& args.len() == 2
|
||||
&& match (&args[0], &args[1]) {
|
||||
(&syn::GenericArgument::Lifetime(_), &syn::GenericArgument::Type(ref arg)) => elem(arg),
|
||||
(syn::GenericArgument::Lifetime(_), syn::GenericArgument::Type(arg)) => elem(arg),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_option(ty: &syn::Type, elem: fn(&syn::Type) -> bool) -> bool {
|
||||
let path = match *ty {
|
||||
syn::Type::Path(ref ty) => &ty.path,
|
||||
let path = match ty {
|
||||
syn::Type::Path(ty) => &ty.path,
|
||||
_ => {
|
||||
return false;
|
||||
}
|
||||
@@ -1801,16 +1763,16 @@ fn is_option(ty: &syn::Type, elem: fn(&syn::Type) -> bool) -> bool {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
let args = match seg.arguments {
|
||||
syn::PathArguments::AngleBracketed(ref bracketed) => &bracketed.args,
|
||||
let args = match &seg.arguments {
|
||||
syn::PathArguments::AngleBracketed(bracketed) => &bracketed.args,
|
||||
_ => {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
seg.ident == "Option"
|
||||
&& args.len() == 1
|
||||
&& match args[0] {
|
||||
syn::GenericArgument::Type(ref arg) => elem(arg),
|
||||
&& match &args[0] {
|
||||
syn::GenericArgument::Type(arg) => elem(arg),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -1836,8 +1798,8 @@ fn is_option(ty: &syn::Type, elem: fn(&syn::Type) -> bool) -> bool {
|
||||
// r: &'a str,
|
||||
// }
|
||||
fn is_reference(ty: &syn::Type, elem: fn(&syn::Type) -> bool) -> bool {
|
||||
match *ty {
|
||||
syn::Type::Reference(ref ty) => ty.mutability.is_none() && elem(&ty.elem),
|
||||
match ty {
|
||||
syn::Type::Reference(ty) => ty.mutability.is_none() && elem(&ty.elem),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -1847,15 +1809,15 @@ fn is_str(ty: &syn::Type) -> bool {
|
||||
}
|
||||
|
||||
fn is_slice_u8(ty: &syn::Type) -> bool {
|
||||
match *ty {
|
||||
syn::Type::Slice(ref ty) => is_primitive_type(&ty.elem, "u8"),
|
||||
match ty {
|
||||
syn::Type::Slice(ty) => is_primitive_type(&ty.elem, "u8"),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_primitive_type(ty: &syn::Type, primitive: &str) -> bool {
|
||||
match *ty {
|
||||
syn::Type::Path(ref ty) => ty.qself.is_none() && is_primitive_path(&ty.path, primitive),
|
||||
match ty {
|
||||
syn::Type::Path(ty) => ty.qself.is_none() && is_primitive_path(&ty.path, primitive),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -1893,40 +1855,40 @@ fn borrowable_lifetimes(
|
||||
}
|
||||
|
||||
fn collect_lifetimes(ty: &syn::Type, out: &mut BTreeSet<syn::Lifetime>) {
|
||||
match *ty {
|
||||
syn::Type::Slice(ref ty) => {
|
||||
match ty {
|
||||
syn::Type::Slice(ty) => {
|
||||
collect_lifetimes(&ty.elem, out);
|
||||
}
|
||||
syn::Type::Array(ref ty) => {
|
||||
syn::Type::Array(ty) => {
|
||||
collect_lifetimes(&ty.elem, out);
|
||||
}
|
||||
syn::Type::Ptr(ref ty) => {
|
||||
syn::Type::Ptr(ty) => {
|
||||
collect_lifetimes(&ty.elem, out);
|
||||
}
|
||||
syn::Type::Reference(ref ty) => {
|
||||
syn::Type::Reference(ty) => {
|
||||
out.extend(ty.lifetime.iter().cloned());
|
||||
collect_lifetimes(&ty.elem, out);
|
||||
}
|
||||
syn::Type::Tuple(ref ty) => {
|
||||
syn::Type::Tuple(ty) => {
|
||||
for elem in &ty.elems {
|
||||
collect_lifetimes(elem, out);
|
||||
}
|
||||
}
|
||||
syn::Type::Path(ref ty) => {
|
||||
if let Some(ref qself) = ty.qself {
|
||||
syn::Type::Path(ty) => {
|
||||
if let Some(qself) = &ty.qself {
|
||||
collect_lifetimes(&qself.ty, out);
|
||||
}
|
||||
for seg in &ty.path.segments {
|
||||
if let syn::PathArguments::AngleBracketed(ref bracketed) = seg.arguments {
|
||||
if let syn::PathArguments::AngleBracketed(bracketed) = &seg.arguments {
|
||||
for arg in &bracketed.args {
|
||||
match *arg {
|
||||
syn::GenericArgument::Lifetime(ref lifetime) => {
|
||||
match arg {
|
||||
syn::GenericArgument::Lifetime(lifetime) => {
|
||||
out.insert(lifetime.clone());
|
||||
}
|
||||
syn::GenericArgument::Type(ref ty) => {
|
||||
syn::GenericArgument::Type(ty) => {
|
||||
collect_lifetimes(ty, out);
|
||||
}
|
||||
syn::GenericArgument::Binding(ref binding) => {
|
||||
syn::GenericArgument::Binding(binding) => {
|
||||
collect_lifetimes(&binding.ty, out);
|
||||
}
|
||||
syn::GenericArgument::Constraint(_)
|
||||
@@ -1936,10 +1898,10 @@ fn collect_lifetimes(ty: &syn::Type, out: &mut BTreeSet<syn::Lifetime>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
syn::Type::Paren(ref ty) => {
|
||||
syn::Type::Paren(ty) => {
|
||||
collect_lifetimes(&ty.elem, out);
|
||||
}
|
||||
syn::Type::Group(ref ty) => {
|
||||
syn::Type::Group(ty) => {
|
||||
collect_lifetimes(&ty.elem, out);
|
||||
}
|
||||
syn::Type::BareFn(_)
|
||||
@@ -1974,7 +1936,7 @@ fn respan_token_stream(stream: TokenStream, span: Span) -> TokenStream {
|
||||
}
|
||||
|
||||
fn respan_token_tree(mut token: TokenTree, span: Span) -> TokenTree {
|
||||
if let TokenTree::Group(ref mut g) = token {
|
||||
if let TokenTree::Group(g) = &mut token {
|
||||
*g = Group::new(g.delimiter(), respan_token_stream(g.stream().clone(), span));
|
||||
}
|
||||
token.set_span(span);
|
||||
|
||||
Reference in New Issue
Block a user