Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Update to syn v2
  • Loading branch information
aumetra committed Oct 19, 2023
commit 1f5c48051eb780f04a16b4bf9b59d26dbef96c4a
2 changes: 1 addition & 1 deletion autometrics-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ proc-macro = true
percent-encoding = "2.2"
proc-macro2 = "1"
quote = "1"
syn = { version = "1", features = ["full"] }
syn = { version = "2", features = ["full"] }
12 changes: 5 additions & 7 deletions autometrics-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ fn instrument_function(

for args in &brackets.args {
ts.push(match args {
GenericArgument::Type(ty)
if matches!(ty, Type::ImplTrait(_)) =>
{
GenericArgument::Type(Type::ImplTrait(_)) => {
quote! { _ }
}
generic_arg => quote! { #generic_arg },
Expand Down Expand Up @@ -321,17 +319,17 @@ fn instrument_impl_block(args: &AutometricsArgs, mut item: ItemImpl) -> Result<T
.items
.into_iter()
.map(|item| match item {
ImplItem::Method(mut method) => {
ImplItem::Fn(mut method) => {
// Skip any methods that have the #[skip_autometrics] attribute
if method
.attrs
.iter()
.any(|attr| attr.path.is_ident("skip_autometrics"))
.any(|attr| attr.path().is_ident("skip_autometrics"))
{
method
.attrs
.retain(|attr| !attr.path.is_ident("skip_autometrics"));
return ImplItem::Method(method);
.retain(|attr| !attr.path().is_ident("skip_autometrics"));
return ImplItem::Fn(method);
}

let item_fn = ItemFn {
Expand Down
29 changes: 10 additions & 19 deletions autometrics-macros/src/result_labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use proc_macro2::TokenStream;
use quote::quote;
use syn::{
punctuated::Punctuated, token::Comma, Attribute, Data, DataEnum, DeriveInput, Error, Ident,
Lit, LitStr, Result, Variant,
punctuated::Punctuated, token::Comma, Attribute, Data, DataEnum, DeriveInput, Error, Expr,
ExprLit, Ident, Lit, LitStr, Result, Variant,
};

// These labels must match autometrics::ERROR_KEY and autometrics::OK_KEY,
Expand Down Expand Up @@ -94,21 +94,17 @@ fn conditional_label_clauses(
fn extract_label_attribute(attrs: &[Attribute]) -> Result<Option<LitStr>> {
attrs
.iter()
.find_map(|att| match att.parse_meta() {
Ok(meta) => match &meta {
.find_map(|att| match &att.meta {
syn::Meta::List(list) => {
// Ignore attribute if it's not `label(...)`
if list.path.segments.len() != 1 || list.path.segments[0].ident != ATTR_LABEL {
return None;
}

// Only lists are allowed
let pair = match list.nested.first() {
Some(syn::NestedMeta::Meta(syn::Meta::NameValue(pair))) => pair,
_ => return Some(Err(Error::new_spanned(
meta,
format!("Only `{ATTR_LABEL}({RESULT_KEY} = \"RES\")` (RES can be {OK_KEY:?} or {ERROR_KEY:?}) is supported"),
))),
let pair = match att.meta.require_list().and_then(|list| list.parse_args::<syn::MetaNameValue>()) {
Ok(pair) => pair,
Err(err) => return Some(Err(Error::new_spanned(&att.meta, err))),
};

// Inside list, only 'result = ...' are allowed
Expand All @@ -120,11 +116,11 @@ fn extract_label_attribute(attrs: &[Attribute]) -> Result<Option<LitStr>> {
}

// Inside 'result = val', 'val' must be a string literal
let lit_str = match pair.lit {
Lit::Str(ref lit_str) => lit_str,
let lit_str = match pair.value {
Expr::Lit(ExprLit { lit: Lit::Str(ref lit_str), .. }) => lit_str,
_ => {
return Some(Err(Error::new_spanned(
&pair.lit,
&pair.value,
format!("Only {OK_KEY:?} or {ERROR_KEY:?}, as string literals, are accepted as result values"),
)));
}
Expand Down Expand Up @@ -153,11 +149,6 @@ fn extract_label_attribute(attrs: &[Attribute]) -> Result<Option<LitStr>> {
)))
},
_ => None,
},
Err(e) => Some(Err(Error::new_spanned(
att,
format!("could not parse the meta attribute: {e}"),
))),
})
})
.transpose()
}
2 changes: 2 additions & 0 deletions autometrics/tests/init_to_zero_test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(featurue = "prometheus-exporter")]

use autometrics::{autometrics, prometheus_exporter};

#[cfg(debug_assertions)]
Expand Down