Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve error propagation #7

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
26 changes: 10 additions & 16 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 Expand Up @@ -163,11 +160,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 {
Expand Down
Loading