Skip to content

Commit

Permalink
Merge pull request #108 from nguarracino/main
Browse files Browse the repository at this point in the history
Return an error if serde flatten attribute is found
  • Loading branch information
snowsignal authored Apr 27, 2023
2 parents cf70742 + 28485cf commit aa7cbdc
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub enum ParseError {
SerdeTagRequired { enum_ident: String },
#[error("serde content attribute needs to be specified for algebraic enum {enum_ident}. e.g. #[serde(tag = \"type\", content = \"content\")]")]
SerdeContentRequired { enum_ident: String },
#[error("the serde flatten attribute is not currently supported")]
SerdeFlattenNotAllowed,
}

/// Parse the given Rust source string into `ParsedData`.
Expand Down Expand Up @@ -168,6 +170,10 @@ fn parse_struct(s: &ItemStruct) -> Result<RustItem, ParseError> {
RustType::try_from(&f.ty)?
};

if serde_flatten(&f.attrs) {
return Err(ParseError::SerdeFlattenNotAllowed);
}

let has_default = serde_default(&f.attrs);
let decorators = get_field_decorators(&f.attrs);

Expand Down Expand Up @@ -645,14 +651,12 @@ fn serde_rename_all(attrs: &[syn::Attribute]) -> Option<String> {
.and_then(literal_as_string)
}

fn serde_default(attrs: &[syn::Attribute]) -> bool {
let default = Ident::new("default", Span::call_site());

fn serde_attr(attrs: &[syn::Attribute], ident: &Ident) -> bool {
attrs.iter().any(|attr| {
get_serde_meta_items(attr).iter().any(|arg| match arg {
NestedMeta::Meta(Meta::Path(path)) => {
if let Some(ident) = path.get_ident() {
*ident == default
if let Some(this_ident) = path.get_ident() {
*this_ident == *ident
} else {
false
}
Expand All @@ -662,6 +666,14 @@ fn serde_default(attrs: &[syn::Attribute]) -> bool {
})
}

fn serde_default(attrs: &[syn::Attribute]) -> bool {
serde_attr(attrs, &Ident::new("default", Span::call_site()))
}

fn serde_flatten(attrs: &[syn::Attribute]) -> bool {
serde_attr(attrs, &Ident::new("flatten", Span::call_site()))
}

// TODO: for now, this is a workaround until we can integrate serde_derive_internal
// into our parser.
/// Returns all arguments passed into `#[serde(...)]` attributes
Expand Down

0 comments on commit aa7cbdc

Please sign in to comment.