Skip to content

Commit 1ddbc6c

Browse files
committed
derive: syn 2
1 parent 4cdaa40 commit 1ddbc6c

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ edition = "2021"
1818
proc-macro = true
1919

2020
[dependencies]
21-
syn = "1.0"
21+
syn = "2.0"
2222
proc-macro2 = "1.0"
2323
quote = "1.0"

derive/src/lib.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use proc_macro2::TokenStream;
22
use quote::{format_ident, quote, quote_spanned, ToTokens};
33
use syn::spanned::Spanned;
44
use syn::{
5-
parse_macro_input, parse_quote, Attribute, Data, DeriveInput, Fields, GenericParam, Generics,
6-
Ident, Index, Lit, Meta, MetaNameValue, NestedMeta,
5+
parse_macro_input, parse_quote, Attribute, Data, DeriveInput, Expr, ExprAssign, ExprLit, ExprPath,
6+
Fields, GenericParam, Generics, Ident, Index, Lit, Meta, MetaNameValue,
77
};
88

99

@@ -81,33 +81,33 @@ struct Attributes {
8181
impl Attributes {
8282
fn parse(attrs: &[Attribute]) -> Self {
8383
let mut out = Self::default();
84-
for attr in attrs.iter().filter(|a| a.path.is_ident("visit")) {
85-
let meta = attr.parse_meta().expect("visit attribute");
86-
match meta {
87-
Meta::List(l) => {
88-
for nested in &l.nested {
89-
match nested {
90-
NestedMeta::Meta(Meta::NameValue(v)) => out.parse_name_value(v),
91-
_ => panic!("Expected #[visit(key = \"value\")]"),
92-
}
93-
}
84+
for attr in attrs {
85+
if let Meta::NameValue(ref namevalue) = attr.meta {
86+
if namevalue.path.is_ident("visit") {
87+
out.parse_name_value(namevalue);
9488
}
95-
_ => panic!("Expected #[visit(...)]"),
9689
}
9790
}
91+
if out.with.is_none() {
92+
panic!("Expected #[visit(...)]");
93+
}
9894
out
9995
}
10096

10197
/// Updates self with a name value attribute
10298
fn parse_name_value(&mut self, v: &MetaNameValue) {
103-
if v.path.is_ident("with") {
104-
match &v.lit {
105-
Lit::Str(s) => self.with = Some(format_ident!("{}", s.value(), span = s.span())),
106-
_ => panic!("Expected a string value, got {}", v.lit.to_token_stream()),
99+
if let Expr::Assign(ExprAssign { ref left, ref right , ..}) = v.value {
100+
if let Expr::Path(ExprPath { ref path, .. }) = **left {
101+
if path.is_ident("with") {
102+
match **right {
103+
Expr::Lit(ExprLit { lit: Lit::Str(ref s), .. }) => self.with = Some(format_ident!("{}", s.value(), span = s.span())),
104+
_ => panic!("Expected a string value, got {}", v.value.to_token_stream()),
105+
}
106+
return;
107+
}
107108
}
108-
return;
109109
}
110-
panic!("Unrecognised kv attribute {}", v.path.to_token_stream())
110+
panic!("Unrecognised kv attribute {}", v.to_token_stream())
111111
}
112112

113113
/// Returns the pre and post visit token streams

0 commit comments

Comments
 (0)