Something went wrong. Try again.
YAML-based live configuration of MicroFreak datyoma.tngl.io/mfctl
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475use proc_macro::TokenStream;use quote::quote;use syn::{Data, DeriveInput, Fields, parse_macro_input, parse_quote};
/// Add the shared author-model derives and implicit nested validation.////// Generates Clone, Copy, Debug, PartialEq, Deserialize, Serialize, JsonSchema,/// and Validate. Use `#[author(no_copy)]` for types containing owned data to/// omit Copy while retaining the other derives and implicit validation./// Fields without Garde annotations receive `#[garde(dive)]`; explicit rules/// are preserved as written. Combine `dive` with `custom` when both are needed./// Default, Serde policy, and schema annotations remain explicit.////// ```compile_fail/// use mfctl_derive::author;/// #[author]/// union Unsupported { value: u8 }/// ```////// ```compile_fail/// use mfctl_derive::author;/// #[author(options)]/// struct Unsupported;/// ```#[proc_macro_attribute]pub fn author(args: TokenStream, input: TokenStream) -> TokenStream { let no_copy = parse_macro_input!(args with parse_no_copy); let mut item = parse_macro_input!(input as DeriveInput); match &mut item.data { Data::Struct(data) => inject(&mut data.fields), Data::Enum(data) => { for variant in &mut data.variants { inject(&mut variant.fields); } } Data::Union(_) => { return syn::Error::new_spanned(&item.ident, "author supports only structs and enums") .into_compile_error() .into(); } } let copy = (!no_copy).then(|| quote!(Copy,)); item.attrs.insert( 0, parse_quote!(#[derive( Clone, #copy Debug, PartialEq, ::serde::Deserialize, ::serde::Serialize, ::schemars::JsonSchema, ::garde::Validate, )]), ); quote!(#item).into()}
fn parse_no_copy(input: syn::parse::ParseStream) -> syn::Result<bool> { if input.is_empty() { return Ok(false); } let option: syn::Ident = input.parse()?; if option != "no_copy" { return Err(syn::Error::new_spanned(option, "expected `no_copy`")); } Ok(true)}
fn inject(fields: &mut Fields) { for field in fields { if !field.attrs.iter().any(|attr| attr.path().is_ident("garde")) { field.attrs.push(parse_quote!(#[garde(dive)])); } }}
#[cfg(test)]mod tests;