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

Update to syn v2 #145

Merged
merged 5 commits into from
Oct 25, 2023
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

-
- Update to `syn` v2

### Deprecated

Expand Down
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
36 changes: 17 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,24 @@ 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(..) => return Some(
Err(
Error::new_spanned(
&att.meta,
format!("Only `{ATTR_LABEL}({RESULT_KEY} = \"RES\")` (RES can be {OK_KEY:?} or {ERROR_KEY:?}) is supported"),
),
),
),
};

// Inside list, only 'result = ...' are allowed
Expand All @@ -120,11 +123,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 +156,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(feature = "prometheus-exporter")]

use autometrics::{autometrics, prometheus_exporter};

#[cfg(debug_assertions)]
Expand Down