diff --git a/hitbox-derive/src/cacheable_macro.rs b/hitbox-derive/src/cacheable_macro.rs index 896c34d..59f494c 100644 --- a/hitbox-derive/src/cacheable_macro.rs +++ b/hitbox-derive/src/cacheable_macro.rs @@ -26,7 +26,7 @@ pub fn impl_macro(ast: &syn::DeriveInput) -> syn::Result { } }; - let cache_ttl_implement = match attrs.ttl { + let cache_ttl_implement = match attrs.cache_ttl { Some(cache_ttl) => quote! { fn cache_ttl(&self) -> u32 { #cache_ttl @@ -35,7 +35,7 @@ pub fn impl_macro(ast: &syn::DeriveInput) -> syn::Result { None => proc_macro2::TokenStream::new(), }; - let cache_stale_ttl_implement = match attrs.stale_ttl { + let cache_stale_ttl_implement = match attrs.cache_stale_ttl { Some(cache_stale_ttl) => quote! { fn cache_stale_ttl(&self) -> u32 { #cache_stale_ttl @@ -44,7 +44,7 @@ pub fn impl_macro(ast: &syn::DeriveInput) -> syn::Result { None => proc_macro2::TokenStream::new(), }; - let cache_version_implement = match attrs.version { + let cache_version_implement = match attrs.cache_version { Some(cache_version) => quote! { fn cache_version(&self) -> u32 { #cache_version diff --git a/hitbox-derive/src/container.rs b/hitbox-derive/src/container.rs index d77b873..0e74164 100644 --- a/hitbox-derive/src/container.rs +++ b/hitbox-derive/src/container.rs @@ -1,29 +1,27 @@ +use quote::ToTokens; + const CACHE_TTL: &str = "cache_ttl"; const CACHE_STALE_TTL: &str = "cache_stale_ttl"; const CACHE_VERSION: &str = "cache_version"; -use quote::ToTokens; - -fn get_lit_int<'a>(lit: &'a syn::Lit, attr_name: &'a str) -> Result<&'a syn::LitInt, syn::Error> { - if let syn::Lit::Int(lit) = lit { +fn parse_lit_to_u32(lit: &syn::Lit, attr_name: &str) -> syn::Result { + let lit = if let syn::Lit::Int(lit) = lit { Ok(lit) } else { Err(syn::Error::new_spanned( lit, format!("Expected hitbox {} attribute should be u32", attr_name), )) - } -} + }?; -fn parse_u32(lit: &syn::LitInt) -> syn::Result { lit.base10_parse::() .map_err(|e| syn::Error::new_spanned(lit, e)) } pub struct Container { - pub ttl: Option, - pub stale_ttl: Option, - pub version: Option, + pub cache_ttl: Option, + pub cache_stale_ttl: Option, + pub cache_version: Option, } impl Container { @@ -54,24 +52,21 @@ impl Container { match &meta_item { // Parse `#[hitbox(cache_ttl = 42)]` syn::NestedMeta::Meta(syn::Meta::NameValue(m)) if m.path.is_ident(CACHE_TTL) => { - let lit_ttl = get_lit_int(&m.lit, CACHE_TTL)?; - ttl = Some(parse_u32(lit_ttl)?) + ttl = Some(parse_lit_to_u32(&m.lit, CACHE_TTL)?); } // Parse `#[hitbox(cache_stale_ttl = 42)]` syn::NestedMeta::Meta(syn::Meta::NameValue(m)) if m.path.is_ident(CACHE_STALE_TTL) => { - let lit_stale_ttl = get_lit_int(&m.lit, CACHE_STALE_TTL)?; - stale_ttl = Some(parse_u32(lit_stale_ttl)?) + stale_ttl = Some(parse_lit_to_u32(&m.lit, CACHE_STALE_TTL)?); } // Parse `#[hitbox(cache_version = 42)]` syn::NestedMeta::Meta(syn::Meta::NameValue(m)) if m.path.is_ident(CACHE_VERSION) => { - let lit_version = get_lit_int(&m.lit, CACHE_VERSION)?; - version = Some(parse_u32(lit_version)?) + version = Some(parse_lit_to_u32(&m.lit, CACHE_VERSION)?); } // Throw error on unknown attribute @@ -94,9 +89,9 @@ impl Container { } Ok(Container { - ttl, - stale_ttl, - version, + cache_ttl: ttl, + cache_stale_ttl: stale_ttl, + cache_version: version, }) } }