refactor serde parsing for later reuse
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
cb04553d47
commit
8ebcd68a2c
@ -162,17 +162,12 @@ pub struct SerdeAttrib {
|
|||||||
pub flatten: bool,
|
pub flatten: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<&[syn::Attribute]> for SerdeAttrib {
|
impl SerdeAttrib {
|
||||||
type Error = syn::Error;
|
pub fn parse_attribute(&mut self, attrib: &syn::Attribute) -> Result<(), syn::Error> {
|
||||||
|
|
||||||
fn try_from(attributes: &[syn::Attribute]) -> Result<Self, syn::Error> {
|
|
||||||
use syn::{Meta, NestedMeta};
|
use syn::{Meta, NestedMeta};
|
||||||
|
|
||||||
let mut this: Self = Default::default();
|
|
||||||
|
|
||||||
for attrib in attributes {
|
|
||||||
if !attrib.path.is_ident("serde") {
|
if !attrib.path.is_ident("serde") {
|
||||||
continue;
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let args: AttrArgs = syn::parse2(attrib.tokens.clone())?;
|
let args: AttrArgs = syn::parse2(attrib.tokens.clone())?;
|
||||||
@ -182,20 +177,33 @@ impl TryFrom<&[syn::Attribute]> for SerdeAttrib {
|
|||||||
match var.lit {
|
match var.lit {
|
||||||
syn::Lit::Str(lit) => {
|
syn::Lit::Str(lit) => {
|
||||||
let rename = FieldName::from(&lit);
|
let rename = FieldName::from(&lit);
|
||||||
if this.rename.is_some() && this.rename.as_ref() != Some(&rename) {
|
if self.rename.is_some() && self.rename.as_ref() != Some(&rename) {
|
||||||
error!(lit => "multiple conflicting 'rename' attributes");
|
error!(lit => "multiple conflicting 'rename' attributes");
|
||||||
}
|
}
|
||||||
this.rename = Some(rename);
|
self.rename = Some(rename);
|
||||||
}
|
}
|
||||||
_ => error!(var.lit => "'rename' value must be a string literal"),
|
_ => error!(var.lit => "'rename' value must be a string literal"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NestedMeta::Meta(Meta::Path(path)) if path.is_ident("flatten") => {
|
NestedMeta::Meta(Meta::Path(path)) if path.is_ident("flatten") => {
|
||||||
this.flatten = true;
|
self.flatten = true;
|
||||||
}
|
}
|
||||||
_ => continue,
|
_ => continue,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<&[syn::Attribute]> for SerdeAttrib {
|
||||||
|
type Error = syn::Error;
|
||||||
|
|
||||||
|
fn try_from(attributes: &[syn::Attribute]) -> Result<Self, syn::Error> {
|
||||||
|
let mut this: Self = Default::default();
|
||||||
|
|
||||||
|
for attrib in attributes {
|
||||||
|
this.parse_attribute(attrib)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(this)
|
Ok(this)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user