@@ -2,8 +2,8 @@ use proc_macro2::TokenStream;
2
2
use quote:: { format_ident, quote, quote_spanned, ToTokens } ;
3
3
use syn:: spanned:: Spanned ;
4
4
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 , ExprLit , ExprPath ,
6
+ Fields , GenericParam , Generics , Ident , Index , Lit , Meta , MetaNameValue ,
7
7
} ;
8
8
9
9
@@ -81,33 +81,33 @@ struct Attributes {
81
81
impl Attributes {
82
82
fn parse ( attrs : & [ Attribute ] ) -> Self {
83
83
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) ;
94
88
}
95
- _ => panic ! ( "Expected #[visit(...)]" ) ,
96
89
}
97
90
}
91
+ if out. with . is_none ( ) {
92
+ panic ! ( "Expected #[visit(...)]" ) ;
93
+ }
98
94
out
99
95
}
100
96
101
97
/// Updates self with a name value attribute
102
98
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 ( ref assign) = v. value {
100
+ if let Expr :: Path ( ExprPath { ref path, .. } ) = * assign. left {
101
+ if path. is_ident ( "with" ) {
102
+ match * assign. 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
+ }
107
108
}
108
- return ;
109
109
}
110
- panic ! ( "Unrecognised kv attribute {}" , v. path . to_token_stream( ) )
110
+ panic ! ( "Unrecognised kv attribute {}" , v. to_token_stream( ) )
111
111
}
112
112
113
113
/// Returns the pre and post visit token streams
0 commit comments