Skip to content

Commit

Permalink
added support for public structs
Browse files Browse the repository at this point in the history
  • Loading branch information
perpetualcacophony authored and ModProg committed Jun 22, 2024
1 parent 0cda963 commit 5b968fd
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use proc_macro2::{Literal, Span, TokenStream};
use proc_macro_utils::{TokenParser, TokenStream2Ext};
use quote::{format_ident, ToTokens};
use quote_use::quote_use as quote;
use syn::spanned::Spanned;
use syn::{spanned::Spanned, Visibility};
use syn::{DataStruct, DeriveInput, Field, Fields, Generics, Ident, LitStr, Type};

const ATTRIBUTE_IDENT: &str = "attribute";
Expand Down Expand Up @@ -743,7 +743,12 @@ fn parse_comma() -> TokenStream {
}
}

fn partial_attribute(partial: &Ident, fields: &[AttrField], generics: &Generics) -> Result {
fn partial_attribute(
partial: &Ident,
vis: &Visibility,
fields: &[AttrField],
generics: &Generics,
) -> Result {
let Some(first_field) = fields.first() else {
return Ok(quote!(#[derive(Default)] struct #partial #generics {}));
};
Expand All @@ -755,7 +760,7 @@ fn partial_attribute(partial: &Ident, fields: &[AttrField], generics: &Generics)
};
Ok(quote! {
#[derive(Default)]
struct #partial #generics #fields
#vis struct #partial #generics #fields
})
}

Expand All @@ -771,6 +776,7 @@ pub fn attribute_derive(input: DeriveInput) -> Result {
pub fn from_attr_derive(
DeriveInput {
attrs,
vis,
ident,
generics,
data,
Expand Down Expand Up @@ -814,7 +820,7 @@ pub fn from_attr_derive(

let conflicts = conflicts.to_tokens(struct_error)?;

let partial_struct = partial_attribute(partial_ident, &fields, &generics)?;
let partial_struct = partial_attribute(partial_ident, &vis, &fields, &generics)?;

let error_invalid_name = struct_error.unknown_field_error(&fields)?;

Expand Down

0 comments on commit 5b968fd

Please sign in to comment.