From b6c1593616e598de446d05620f58a0bac81a8e38 Mon Sep 17 00:00:00 2001 From: Demur Rumed Date: Mon, 6 Nov 2023 13:43:05 +0000 Subject: [PATCH 1/4] derive: syn 2 --- derive/Cargo.toml | 2 +- derive/src/lib.rs | 38 +++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/derive/Cargo.toml b/derive/Cargo.toml index 58e1fbf57..e868790a0 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -18,6 +18,6 @@ edition = "2021" proc-macro = true [dependencies] -syn = "1.0" +syn = { version = "2.0", features = ["full"] } proc-macro2 = "1.0" quote = "1.0" diff --git a/derive/src/lib.rs b/derive/src/lib.rs index fbc3ef2e1..339f5505d 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -2,8 +2,8 @@ use proc_macro2::TokenStream; use quote::{format_ident, quote, quote_spanned, ToTokens}; use syn::spanned::Spanned; use syn::{ - parse_macro_input, parse_quote, Attribute, Data, DeriveInput, Fields, GenericParam, Generics, - Ident, Index, Lit, Meta, MetaNameValue, NestedMeta, + parse_macro_input, parse_quote, Attribute, Data, DeriveInput, Expr, ExprLit, ExprPath, + Fields, GenericParam, Generics, Ident, Index, Lit, Meta, MetaNameValue, }; @@ -81,33 +81,33 @@ struct Attributes { impl Attributes { fn parse(attrs: &[Attribute]) -> Self { let mut out = Self::default(); - for attr in attrs.iter().filter(|a| a.path.is_ident("visit")) { - let meta = attr.parse_meta().expect("visit attribute"); - match meta { - Meta::List(l) => { - for nested in &l.nested { - match nested { - NestedMeta::Meta(Meta::NameValue(v)) => out.parse_name_value(v), - _ => panic!("Expected #[visit(key = \"value\")]"), - } - } + for attr in attrs { + if let Meta::NameValue(ref namevalue) = attr.meta { + if namevalue.path.is_ident("visit") { + out.parse_name_value(namevalue); } - _ => panic!("Expected #[visit(...)]"), } } + if out.with.is_none() { + panic!("Expected #[visit(...)]"); + } out } /// Updates self with a name value attribute fn parse_name_value(&mut self, v: &MetaNameValue) { - if v.path.is_ident("with") { - match &v.lit { - Lit::Str(s) => self.with = Some(format_ident!("{}", s.value(), span = s.span())), - _ => panic!("Expected a string value, got {}", v.lit.to_token_stream()), + if let Expr::Assign(ref assign) = v.value { + if let Expr::Path(ExprPath { ref path, .. }) = *assign.left { + if path.is_ident("with") { + match *assign.right { + Expr::Lit(ExprLit { lit: Lit::Str(ref s), .. }) => self.with = Some(format_ident!("{}", s.value(), span = s.span())), + _ => panic!("Expected a string value, got {}", v.value.to_token_stream()), + } + return; + } } - return; } - panic!("Unrecognised kv attribute {}", v.path.to_token_stream()) + panic!("Unrecognised kv attribute {}", v.to_token_stream()) } /// Returns the pre and post visit token streams From aadae83a2378276eda474fb40fcf5859fe737b7d Mon Sep 17 00:00:00 2001 From: Demur Rumed Date: Sat, 18 Nov 2023 15:09:50 +0000 Subject: [PATCH 2/4] enable visit/visit-mut features --- derive/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/derive/Cargo.toml b/derive/Cargo.toml index e868790a0..2814738a7 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -18,6 +18,6 @@ edition = "2021" proc-macro = true [dependencies] -syn = { version = "2.0", features = ["full"] } +syn = { version = "2.0", features = ["full", "visit", "visit-mut"] } proc-macro2 = "1.0" quote = "1.0" From e19aa02759a0ca1ea64456013783ae9f6c2fe50b Mon Sep 17 00:00:00 2001 From: Demur Rumed Date: Tue, 21 Nov 2023 00:42:23 +0000 Subject: [PATCH 3/4] Take 2 --- derive/Cargo.toml | 2 +- derive/src/lib.rs | 54 ++++++++++++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/derive/Cargo.toml b/derive/Cargo.toml index 2814738a7..0ed9610f4 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -18,6 +18,6 @@ edition = "2021" proc-macro = true [dependencies] -syn = { version = "2.0", features = ["full", "visit", "visit-mut"] } +syn = { version = "2.0", features = ["parsing"] } proc-macro2 = "1.0" quote = "1.0" diff --git a/derive/src/lib.rs b/derive/src/lib.rs index 339f5505d..d19696aa4 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -2,8 +2,9 @@ use proc_macro2::TokenStream; use quote::{format_ident, quote, quote_spanned, ToTokens}; use syn::spanned::Spanned; use syn::{ - parse_macro_input, parse_quote, Attribute, Data, DeriveInput, Expr, ExprLit, ExprPath, - Fields, GenericParam, Generics, Ident, Index, Lit, Meta, MetaNameValue, + parse::{Parse, ParseStream}, + parse_macro_input, parse_quote, Attribute, Data, DeriveInput, + Fields, GenericParam, Generics, Ident, Index, LitStr, Meta, Token }; @@ -78,36 +79,41 @@ struct Attributes { with: Option, } +struct WithIdent { + with: Option, +} +impl Parse for WithIdent { + fn parse(input: ParseStream) -> Result { + let mut result = WithIdent { with: None }; + let ident = input.parse::()?; + if ident != "with" { + return Err(syn::Error::new(ident.span(), "Expected identifier to be `with`")); + } + input.parse::()?; + let s = input.parse::()?; + result.with = Some(format_ident!("{}", s.value(), span = s.span())); + Ok(result) + } +} + impl Attributes { fn parse(attrs: &[Attribute]) -> Self { let mut out = Self::default(); for attr in attrs { - if let Meta::NameValue(ref namevalue) = attr.meta { - if namevalue.path.is_ident("visit") { - out.parse_name_value(namevalue); - } - } - } - if out.with.is_none() { - panic!("Expected #[visit(...)]"); - } - out - } - - /// Updates self with a name value attribute - fn parse_name_value(&mut self, v: &MetaNameValue) { - if let Expr::Assign(ref assign) = v.value { - if let Expr::Path(ExprPath { ref path, .. }) = *assign.left { - if path.is_ident("with") { - match *assign.right { - Expr::Lit(ExprLit { lit: Lit::Str(ref s), .. }) => self.with = Some(format_ident!("{}", s.value(), span = s.span())), - _ => panic!("Expected a string value, got {}", v.value.to_token_stream()), + if let Meta::List(ref metalist) = attr.meta { + if metalist.path.is_ident("visit") { + match syn::parse2::(metalist.tokens.clone()) { + Ok(with_ident) => { + out.with = with_ident.with; + } + Err(e) => { + panic!("{}", e); + } } - return; } } } - panic!("Unrecognised kv attribute {}", v.to_token_stream()) + out } /// Returns the pre and post visit token streams From 7a4304881035c8d0687eb3edbfdb9e92747d9f2e Mon Sep 17 00:00:00 2001 From: Andrew Lamb Date: Mon, 27 Nov 2023 13:14:30 -0500 Subject: [PATCH 4/4] Minimize feature set used --- derive/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/derive/Cargo.toml b/derive/Cargo.toml index 788b06e44..43f75a5eb 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -18,6 +18,6 @@ edition = "2021" proc-macro = true [dependencies] -syn = { version = "2.0", features = ["parsing"] } +syn = { version = "2.0", default-features = false, features = ["printing", "parsing", "derive", "proc-macro"] } proc-macro2 = "1.0" quote = "1.0"