Formatting
This commit is contained in:
16
src/attr.rs
16
src/attr.rs
@@ -30,7 +30,10 @@ pub(crate) fn tag_type(attrs: &[Attribute], enumeration: &DataEnum) -> Result<Ta
|
||||
} else if value.path.is_ident("content") {
|
||||
if let Lit::Str(s) = &value.lit {
|
||||
if content.is_some() {
|
||||
return Err(Error::new_spanned(meta, "duplicate content attribute"));
|
||||
return Err(Error::new_spanned(
|
||||
meta,
|
||||
"duplicate content attribute",
|
||||
));
|
||||
}
|
||||
content = Some(s.value());
|
||||
continue;
|
||||
@@ -60,14 +63,19 @@ pub(crate) fn tag_type(attrs: &[Attribute], enumeration: &DataEnum) -> Result<Ta
|
||||
(Some(tag), None) => {
|
||||
for fields in enumeration.variants.iter().map(|v| &v.fields) {
|
||||
if let Fields::Unnamed(_) = fields {
|
||||
return Err(Error::new_spanned(fields,
|
||||
"enums containing tuple variants cannot be internally tagged"));
|
||||
return Err(Error::new_spanned(
|
||||
fields,
|
||||
"enums containing tuple variants cannot be internally tagged",
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(TagType::Internal(tag))
|
||||
}
|
||||
(Some(tag), Some(content)) => Ok(TagType::Adjacent { tag, content }),
|
||||
_ => Err(Error::new_spanned(&attrs[0], "Invalid enum representation."))
|
||||
_ => Err(Error::new_spanned(
|
||||
&attrs[0],
|
||||
"Invalid enum representation.",
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
40
src/de.rs
40
src/de.rs
@@ -12,7 +12,9 @@ pub fn derive(input: &DeriveInput, enumeration: &DataEnum) -> Result<TokenStream
|
||||
let tag_type = attr::tag_type(&input.attrs, &enumeration)?;
|
||||
match &tag_type {
|
||||
TagType::External => deserialize_external(input, enumeration),
|
||||
TagType::Adjacent { tag, content } => deserialize_adjacent(input, enumeration, tag, content),
|
||||
TagType::Adjacent { tag, content } => {
|
||||
deserialize_adjacent(input, enumeration, tag, content)
|
||||
}
|
||||
TagType::Internal(tag) => deserialize_internal(input, enumeration, tag),
|
||||
_ => Err(Error::new(
|
||||
Span::call_site(),
|
||||
@@ -33,13 +35,13 @@ struct EnumVariants {
|
||||
impl EnumVariants {
|
||||
fn new(ident: &Ident, enumeration: &DataEnum) -> Result<EnumVariants> {
|
||||
let (unit_variants, struct_variants): (Vec<_>, Vec<_>) =
|
||||
enumeration.variants.iter().partition(|v| {
|
||||
if let Fields::Unit = &v.fields {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
});
|
||||
enumeration.variants.iter().partition(|v| {
|
||||
if let Fields::Unit = &v.fields {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
});
|
||||
let struct_variant_names = struct_variants
|
||||
.iter()
|
||||
.cloned()
|
||||
@@ -51,9 +53,9 @@ impl EnumVariants {
|
||||
Ident::new(
|
||||
&format!("__{}_{}_Struct", ident, variant.ident),
|
||||
Span::call_site(),
|
||||
)
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
.collect::<Vec<_>>();
|
||||
let structs = struct_variants
|
||||
.iter()
|
||||
.zip(struct_names.iter())
|
||||
@@ -63,7 +65,10 @@ impl EnumVariants {
|
||||
.iter()
|
||||
.map(|variant| variant.ident.clone())
|
||||
.collect::<Vec<_>>();
|
||||
let unit_variant_idents = unit_variants.iter().map(|v| v.ident.clone()).collect::<Vec<_>>();
|
||||
let unit_variant_idents = unit_variants
|
||||
.iter()
|
||||
.map(|v| v.ident.clone())
|
||||
.collect::<Vec<_>>();
|
||||
let unit_variant_names = unit_variants
|
||||
.iter()
|
||||
.cloned()
|
||||
@@ -80,7 +85,11 @@ impl EnumVariants {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deserialize_internal(input: &DeriveInput, enumeration: &DataEnum, tag: &str) -> Result<TokenStream> {
|
||||
pub fn deserialize_internal(
|
||||
input: &DeriveInput,
|
||||
enumeration: &DataEnum,
|
||||
tag: &str,
|
||||
) -> Result<TokenStream> {
|
||||
let ident = &input.ident;
|
||||
let EnumVariants {
|
||||
struct_variant_names,
|
||||
@@ -171,7 +180,12 @@ pub fn deserialize_internal(input: &DeriveInput, enumeration: &DataEnum, tag: &s
|
||||
})
|
||||
}
|
||||
|
||||
pub fn deserialize_adjacent(input: &DeriveInput, enumeration: &DataEnum, tag: &str, content: &str) -> Result<TokenStream> {
|
||||
pub fn deserialize_adjacent(
|
||||
input: &DeriveInput,
|
||||
enumeration: &DataEnum,
|
||||
tag: &str,
|
||||
content: &str,
|
||||
) -> Result<TokenStream> {
|
||||
let ident = &input.ident;
|
||||
let EnumVariants {
|
||||
struct_variant_names,
|
||||
|
||||
Reference in New Issue
Block a user