parse field type

This commit is contained in:
Quaternions 2024-02-07 04:05:00 -08:00
parent e2c681b511
commit 1f710976cc

View File

@ -9,17 +9,32 @@ pub fn id_derive(input: TokenStream) -> TokenStream {
// Parse the input tokens into a syntax tree // Parse the input tokens into a syntax tree
let ast = parse_macro_input!(input as DeriveInput); let ast = parse_macro_input!(input as DeriveInput);
let name = &ast.ident; let name = &ast.ident;
let ty = match &ast.data {
syn::Data::Struct(s) => {
if let syn::Fields::Unnamed(fields) = &s.fields {
if fields.unnamed.len() != 1 {
return syn::Error::new_spanned(&ast, "Expected exactly one field.").to_compile_error().into();
}
&fields.unnamed[0].ty
} else {
return syn::Error::new_spanned(&ast, "Expected a tuple struct.").to_compile_error().into();
}
}
_ => {
return syn::Error::new_spanned(&ast, "Expected a struct.").to_compile_error().into();
}
};
// Generate the code for the implementation // Generate the code for the implementation
let expanded = quote! { let expanded = quote! {
impl #name { impl #name {
#[inline] #[inline]
pub const fn new(id: u32) -> Self { pub const fn new(id: #ty) -> Self {
Self(id) Self(id)
} }
#[inline] #[inline]
pub const fn get(self) -> u32 { pub const fn get(self) -> #ty {
self.0 self.0
} }
} }