From bc92479bf286f5b25c9f86de2828d62d3338b6b9 Mon Sep 17 00:00:00 2001 From: Collins Muriuki Date: Tue, 12 Dec 2023 10:54:23 +0300 Subject: [PATCH 1/2] chore: Refactor get function return type --- macros/src/lib.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/macros/src/lib.rs b/macros/src/lib.rs index f468668..74d49e7 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -163,11 +163,8 @@ fn get_function_return_type(output: &ReturnType) -> FunctionReturnType { .last() .map(|segment| segment.ident.to_string()); if let Some(s) = type_str { - if s == "Result" { - is_result = true; - } else if s == "Option" { - is_option = true - } + is_option = s == "Option"; + is_result = s == "Result"; } } FunctionReturnType { From ec294ea6eceb9b998a4282b44aa8aaf18e10faac Mon Sep 17 00:00:00 2001 From: Collins Muriuki Date: Wed, 20 Mar 2024 15:30:26 +0300 Subject: [PATCH 2/2] improve error message propagation with ttl_cache macro --- Cargo.toml | 6 +++--- macros/Cargo.toml | 3 ++- macros/src/lib.rs | 19 ++++++++----------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a30a6d0..a9a2674 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ description = "Simple rust caching tools" keywords = ["caching"] license = "MIT" readme = "./README.md" -version ="2.4.0-beta" +version ="2.4.1-beta" edition = "2021" [workspace.package] @@ -18,8 +18,8 @@ readme = "./README.md" license = "MIT" [dependencies] -simple_cache_macros = "2.4.0-beta" -simple_cache_core = "1.0.0-beta" +simple_cache_macros = {path = "./macros"} +simple_cache_core = {path = "./core"} futures = "0.3.28" [workspace] diff --git a/macros/Cargo.toml b/macros/Cargo.toml index e838f9b..874821d 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -7,7 +7,7 @@ keywords.workspace = true repository.workspace = true readme.workspace = true license.workspace = true -version = "2.4.0-beta" +version = "2.4.1-beta" [lib] proc-macro = true @@ -18,3 +18,4 @@ quote = "1.0.32" proc-macro2="1.0.66" syn = {version="2.0.28", features=["extra-traits", "full"]} futures = "0.3.28" +proc-macro-error = "1.0.4" diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 74d49e7..606e027 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -2,6 +2,7 @@ use darling::ast::NestedMeta; use darling::{Error, FromMeta}; use proc_macro::TokenStream; use proc_macro2::Span; +use proc_macro_error::proc_macro_error; use quote::quote; use syn::punctuated::Punctuated; use syn::token::Comma; @@ -24,6 +25,7 @@ struct FunctionReturnType<'a> { /// This proc macro is designed to cache function calls with a /// time-to-live (TTL) duration. #[proc_macro_attribute] +#[proc_macro_error] pub fn ttl_cache(attr: TokenStream, item: TokenStream) -> TokenStream { // Parse the anotated function signature and extract various properties let function = parse_macro_input!(item as ItemFn); @@ -66,21 +68,16 @@ pub fn ttl_cache(attr: TokenStream, item: TokenStream) -> TokenStream { let only_ok = only_ok.is_some(); let only_some = only_some.is_some(); let function_return_type = ty; - if only_ok || only_some { - assert_ne!( - only_some, only_ok, - "`only_some` and `only_ok` cannot both be set" - ); + if only_ok && only_some { + proc_macro_error::abort_call_site!("`only_some` and `only_ok` cannot both be set"); } - if only_ok { - assert_eq!( - only_ok, is_result, + if only_ok && !is_result { + proc_macro_error::abort_call_site!( "`only_ok` can only be applied if the function's return type is a `Result`" ); } - if only_some { - assert_eq!( - only_some, is_option, + if only_some && !is_option { + proc_macro_error::abort_call_site!( "`only_some` can only be applied if the function's return type is an `Option`" ); }