diff --git a/redb-derive/Cargo.toml b/redb-derive/Cargo.toml index 711c63ba..c01c64f5 100644 --- a/redb-derive/Cargo.toml +++ b/redb-derive/Cargo.toml @@ -1,11 +1,13 @@ [package] name = "redb-derive" version = "0.1.0" +license = "MIT OR Apache-2.0" edition = "2021" [lib] proc-macro = true [dependencies] +proc-macro2 = "1.0.66" quote = "1.0.33" syn = "2.0.29" diff --git a/redb-derive/src/lib.rs b/redb-derive/src/lib.rs index 1897b4f9..bf72e729 100644 --- a/redb-derive/src/lib.rs +++ b/redb-derive/src/lib.rs @@ -2,14 +2,21 @@ use proc_macro::TokenStream; use quote::quote; use syn::{parse_macro_input, DeriveInput}; -#[proc_macro_derive(RedbValue, attributes(fixed_width))] +#[proc_macro_derive(RedbValue, attributes(fixed_width, type_name))] pub fn redb_value(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); let name = input.ident; - let width = match &input.attrs.get(0) { - Some(_) => quote!(Some(std::mem::size_of::())), - None => quote!(None), - }; + + let mut width = quote!(None); + let mut type_name = quote!(stringify!(#name)); + for attr in input.attrs { + if attr.path().is_ident("fixed_width") { + width = quote!(Some(std::mem::size_of::())); + } + if attr.path().is_ident("type_name") { + type_name = attr.meta.require_list().unwrap().tokens.clone() + } + } let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); @@ -38,7 +45,7 @@ pub fn redb_value(input: TokenStream) -> TokenStream { } fn type_name() -> redb::TypeName { - redb::TypeName::new(stringify!($t)) + redb::TypeName::new(#type_name) } } };