From 138118249cc23190983e8a53f38019770d82bffb Mon Sep 17 00:00:00 2001 From: aumetra Date: Wed, 25 Oct 2023 10:06:13 +0200 Subject: [PATCH] Update to syn v2 (#145) * Update to syn v2 * Update autometrics/tests/init_to_zero_test.rs Co-authored-by: Mari * Restore old error message * Add `syn` v2 update to changelog --------- Co-authored-by: Mari --- CHANGELOG.md | 2 +- autometrics-macros/Cargo.toml | 2 +- autometrics-macros/src/lib.rs | 12 ++++----- autometrics-macros/src/result_labels.rs | 36 ++++++++++++------------- autometrics/tests/init_to_zero_test.rs | 2 ++ 5 files changed, 26 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 618c9c2..8c81ea7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- +- Update to `syn` v2 ### Deprecated diff --git a/autometrics-macros/Cargo.toml b/autometrics-macros/Cargo.toml index 8cdfab8..dd5c5df 100644 --- a/autometrics-macros/Cargo.toml +++ b/autometrics-macros/Cargo.toml @@ -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"] } diff --git a/autometrics-macros/src/lib.rs b/autometrics-macros/src/lib.rs index 5608901..ac1486d 100644 --- a/autometrics-macros/src/lib.rs +++ b/autometrics-macros/src/lib.rs @@ -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 }, @@ -321,17 +319,17 @@ fn instrument_impl_block(args: &AutometricsArgs, mut item: ItemImpl) -> Result { + 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 { diff --git a/autometrics-macros/src/result_labels.rs b/autometrics-macros/src/result_labels.rs index 5f924fa..3f93bab 100644 --- a/autometrics-macros/src/result_labels.rs +++ b/autometrics-macros/src/result_labels.rs @@ -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, @@ -94,8 +94,7 @@ fn conditional_label_clauses( fn extract_label_attribute(attrs: &[Attribute]) -> Result> { 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 { @@ -103,12 +102,16 @@ fn extract_label_attribute(attrs: &[Attribute]) -> Result> { } // 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::()) { + 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 @@ -120,11 +123,11 @@ fn extract_label_attribute(attrs: &[Attribute]) -> Result> { } // 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"), ))); } @@ -153,11 +156,6 @@ fn extract_label_attribute(attrs: &[Attribute]) -> Result> { ))) }, _ => None, - }, - Err(e) => Some(Err(Error::new_spanned( - att, - format!("could not parse the meta attribute: {e}"), - ))), - }) + }) .transpose() } diff --git a/autometrics/tests/init_to_zero_test.rs b/autometrics/tests/init_to_zero_test.rs index 9b51d5f..f5406b7 100644 --- a/autometrics/tests/init_to_zero_test.rs +++ b/autometrics/tests/init_to_zero_test.rs @@ -1,3 +1,5 @@ +#![cfg(feature = "prometheus-exporter")] + use autometrics::{autometrics, prometheus_exporter}; #[cfg(debug_assertions)]