Skip to content

Commit

Permalink
fix: Improve error propagation (#7)
Browse files Browse the repository at this point in the history
* chore: Refactor get function return type

* improve error message propagation with ttl_cache macro
  • Loading branch information
c12i authored Mar 20, 2024
1 parent 4079cdb commit 88fc6fc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
19 changes: 8 additions & 11 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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`"
);
}
Expand Down

0 comments on commit 88fc6fc

Please sign in to comment.