Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reset attribute #1

Merged
merged 2 commits into from
Jun 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use convert_case::{Case, Casing};
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use syn::ext::IdentExt;
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::{
Expand Down Expand Up @@ -201,11 +202,28 @@ fn generate_fields<'a>(
}
}

fn is_reset_attr(attr: &Attribute) -> bool {
if let Some(ident) = attr.path.get_ident() {
if ident.to_string() == "nestruct" {
if let Ok(arg) = attr.parse_args_with(Ident::parse_any) {
return arg.to_string() == "reset";
}
}
}
false
}

fn generate_structs(nest: bool, nestruct: &Nestruct, parent_attrs: &[Attribute]) -> TokenStream2 {
let mut tokens = Vec::new();
let fields: Vec<&NestableField> = nestruct.fields.iter().collect();
let mut attrs = Vec::from(parent_attrs);
attrs.extend(nestruct.attrs.iter().cloned());
for attr in nestruct.attrs.clone() {
if is_reset_attr(&attr) {
attrs.clear()
} else {
attrs.push(attr)
}
}
let (children, is_variant, fvs) = generate_fields(&fields, nest, quote! { pub });
for child in children {
tokens.push(generate_structs(nest, child, &attrs))
Expand Down