@@ -25,6 +25,12 @@ impl Error {
25
25
}
26
26
}
27
27
28
+ impl From < syn:: Error > for Error {
29
+ fn from ( e : syn:: Error ) -> Error {
30
+ Error ( e. to_compile_error ( ) )
31
+ }
32
+ }
33
+
28
34
decl_derive ! ( [ Fail , attributes( fail, cause) ] => fail_derive) ;
29
35
30
36
fn fail_derive ( s : synstructure:: Structure ) -> TokenStream {
@@ -120,7 +126,7 @@ fn display_body(s: &synstructure::Structure) -> Result<Option<quote::__rt::Token
120
126
}
121
127
122
128
let format_string = match msg. nested [ 0 ] {
123
- syn:: NestedMeta :: Meta ( syn:: Meta :: NameValue ( ref nv) ) if nv. ident == "display" => {
129
+ syn:: NestedMeta :: Meta ( syn:: Meta :: NameValue ( ref nv) ) if nv. path . is_ident ( "display" ) => {
124
130
nv. lit . clone ( )
125
131
}
126
132
_ => {
@@ -131,12 +137,12 @@ fn display_body(s: &synstructure::Structure) -> Result<Option<quote::__rt::Token
131
137
}
132
138
} ;
133
139
let args = msg. nested . iter ( ) . skip ( 1 ) . map ( |arg| match * arg {
134
- syn:: NestedMeta :: Literal ( syn:: Lit :: Int ( ref i) ) => {
135
- let bi = & v. bindings ( ) [ i. value ( ) as usize ] ;
140
+ syn:: NestedMeta :: Lit ( syn:: Lit :: Int ( ref i) ) => {
141
+ let bi = & v. bindings ( ) [ i. base10_parse :: < usize > ( ) ? ] ;
136
142
Ok ( quote ! ( #bi) )
137
143
}
138
- syn:: NestedMeta :: Meta ( syn:: Meta :: Word ( ref id ) ) => {
139
- let id_s = id . to_string ( ) ;
144
+ syn:: NestedMeta :: Meta ( syn:: Meta :: Path ( ref path ) ) => {
145
+ let id_s = path . get_ident ( ) . map ( syn :: Ident :: to_string) . unwrap_or ( "" . to_string ( ) ) ;
140
146
if id_s. starts_with ( "_" ) {
141
147
if let Ok ( idx) = id_s[ 1 ..] . parse :: < usize > ( ) {
142
148
let bi = match v. bindings ( ) . get ( idx) {
@@ -160,15 +166,16 @@ fn display_body(s: &synstructure::Structure) -> Result<Option<quote::__rt::Token
160
166
}
161
167
}
162
168
for bi in v. bindings ( ) {
163
- if bi. ast ( ) . ident . as_ref ( ) == Some ( id) {
169
+ let id = bi. ast ( ) . ident . as_ref ( ) ;
170
+ if id. is_some ( ) && path. is_ident ( id. unwrap ( ) ) {
164
171
return Ok ( quote ! ( #bi) ) ;
165
172
}
166
173
}
167
174
return Err ( Error :: new (
168
175
arg. span ( ) ,
169
176
& format ! (
170
- "Couldn't find field `{}` in `{}::{}`" ,
171
- id ,
177
+ "Couldn't find field `{:? }` in `{}::{}`" ,
178
+ path ,
172
179
s. ast( ) . ident,
173
180
v. ast( ) . ident
174
181
)
@@ -192,8 +199,8 @@ fn display_body(s: &synstructure::Structure) -> Result<Option<quote::__rt::Token
192
199
fn find_error_msg ( attrs : & [ syn:: Attribute ] ) -> Result < Option < syn:: MetaList > , Error > {
193
200
let mut error_msg = None ;
194
201
for attr in attrs {
195
- if let Some ( meta) = attr. interpret_meta ( ) {
196
- if meta. name ( ) == "fail" {
202
+ if let Ok ( meta) = attr. parse_meta ( ) {
203
+ if meta. path ( ) . is_ident ( "fail" ) {
197
204
if error_msg. is_some ( ) {
198
205
return Err ( Error :: new (
199
206
meta. span ( ) ,
@@ -223,7 +230,7 @@ fn is_backtrace(bi: &&synstructure::BindingInfo) -> bool {
223
230
segments : ref path, ..
224
231
} ,
225
232
} ) => path. last ( ) . map_or ( false , |s| {
226
- s. value ( ) . ident == "Backtrace" && s. value ( ) . arguments . is_empty ( )
233
+ s. ident == "Backtrace" && s. arguments . is_empty ( )
227
234
} ) ,
228
235
_ => false ,
229
236
}
@@ -232,18 +239,18 @@ fn is_backtrace(bi: &&synstructure::BindingInfo) -> bool {
232
239
fn is_cause ( bi : & & synstructure:: BindingInfo ) -> bool {
233
240
let mut found_cause = false ;
234
241
for attr in & bi. ast ( ) . attrs {
235
- if let Some ( meta) = attr. interpret_meta ( ) {
236
- if meta. name ( ) == "cause" {
242
+ if let Ok ( meta) = attr. parse_meta ( ) {
243
+ if meta. path ( ) . is_ident ( "cause" ) {
237
244
if found_cause {
238
245
panic ! ( "Cannot have two `cause` attributes" ) ;
239
246
}
240
247
found_cause = true ;
241
248
}
242
- if meta. name ( ) == "fail" {
249
+ if meta. path ( ) . is_ident ( "fail" ) {
243
250
if let syn:: Meta :: List ( ref list) = meta {
244
251
if let Some ( ref pair) = list. nested . first ( ) {
245
- if let & & syn:: NestedMeta :: Meta ( syn:: Meta :: Word ( ref word ) ) = pair. value ( ) {
246
- if word == "cause" {
252
+ if let & & syn:: NestedMeta :: Meta ( syn:: Meta :: Path ( ref path ) ) = pair {
253
+ if path . is_ident ( "cause" ) {
247
254
if found_cause {
248
255
panic ! ( "Cannot have two `cause` attributes" ) ;
249
256
}
0 commit comments