Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Archisman-Mridha committed Jan 15, 2024
1 parent bbba7c1 commit 3c6dfd3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion autometrics-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn autometrics(
let item = parse_macro_input!(item as Item);

let result = match item {
Item::Function(item) => instrument_function(&args, item, None),
Item::Function(item) => instrument_function(&args, item, args.struct_name.as_deref()),
Item::Impl(item) => instrument_impl_block(&args, item, &async_trait),
};

Expand Down
12 changes: 11 additions & 1 deletion autometrics-macros/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use syn::parse::{Parse, ParseStream};
use syn::{Expr, ItemFn, ItemImpl, Result, Token};
use syn::{Expr, ItemFn, ItemImpl, LitStr, Result, Token};

mod kw {
syn::custom_keyword!(track_concurrency);
Expand All @@ -8,10 +8,12 @@ mod kw {
syn::custom_keyword!(latency);
syn::custom_keyword!(ok_if);
syn::custom_keyword!(error_if);
syn::custom_keyword!(struct_name);
}

/// Autometrics can be applied to individual functions or to
/// (all of the methods within) impl blocks.
#[derive(Debug)]
pub(crate) enum Item {
Function(ItemFn),
Impl(ItemImpl),
Expand All @@ -32,6 +34,9 @@ pub(crate) struct AutometricsArgs {
pub ok_if: Option<Expr>,
pub error_if: Option<Expr>,
pub objective: Option<Expr>,

// Fix for https://github.com/autometrics-dev/autometrics-rs/issues/139.
pub struct_name: Option<String>,
}

impl Parse for AutometricsArgs {
Expand Down Expand Up @@ -67,6 +72,11 @@ impl Parse for AutometricsArgs {
return Err(input.error("expected only a single `objective` argument"));
}
args.objective = Some(input.parse()?);
} else if lookahead.peek(kw::struct_name) {
let _ = input.parse::<kw::struct_name>()?;
let _ = input.parse::<Token![=]>()?;
let struct_name = input.parse::<LitStr>()?.value();
args.struct_name = Some(struct_name);
} else if lookahead.peek(Token![,]) {
let _ = input.parse::<Token![,]>()?;
} else {
Expand Down

0 comments on commit 3c6dfd3

Please sign in to comment.