@@ -29,11 +29,10 @@ use proc_macro_error::{abort, abort_call_site};
29
29
use syn:: {
30
30
parenthesized,
31
31
parse:: { Parse , ParseStream } ,
32
- spanned:: Spanned ,
33
- Expr , ItemFn ,
32
+ Expr ,
34
33
} ;
35
34
36
- use crate :: trace:: validate:: Ast ;
35
+ // use crate::trace::validate::Ast;
37
36
38
37
// - `<Macro>.name: syn::LitStr,` // String`
39
38
// - `<Macro>.enter_on_poll: syn::LitBool,` // boolean
@@ -112,8 +111,8 @@ enum Scope {
112
111
) ]
113
112
pub struct Trace {
114
113
// Anything that implements `syn::parse::Parse` is supported.
115
- name : syn:: LitStr ,
116
- scope : Scope , // Scope::Local, Scope::Thread, etc.
114
+ name : Option < syn:: LitStr > ,
115
+ scope : Option < Scope > , // Scope::Local, Scope::Thread, etc.
117
116
118
117
// Fields wrapped in `Option` are and default to `None` if
119
118
// not specified in the attribute.
@@ -154,17 +153,27 @@ impl Parse for Scope {
154
153
//
155
154
// The `Models` container is built based on the attribute parameters
156
155
// held in the `Trace` type.
156
+ //
157
+ // The inputs are:
158
+ // - `meta`: A `syn::Attribute` encapsulated in `TraceAttr`.
159
+ // - `items`: A `proc_macr2::TokenStream`.
157
160
use syn:: visit:: Visit ;
158
161
pub fn analyze (
159
162
meta : crate :: trace:: validate:: TraceAttr ,
160
163
items : proc_macro2:: TokenStream ,
161
164
) -> Models < Model > {
162
165
let mut models = Models :: < Model > :: new ( ) ;
166
+ //println!("Meta {:#?}", meta);
167
+ // `from_attributes(..)` takes a slice reference.
163
168
// `meta.attrs` is a `Vec<>`, which implements `AsRef`, so is coerced to `&[]`
164
169
let attrs: & [ syn:: Attribute ] = & meta. attrs ;
170
+ // let attribute = Trace::from_attributes(attrs).unwrap();
171
+ //println!("Attrs {:#?}", attrs);
165
172
let attribute = match Trace :: from_attributes ( attrs) {
166
- Ok ( attrs) => attrs,
167
- Err ( err) => return err. into_compile_error ( ) . into ( ) ,
173
+ Ok ( trace) => trace,
174
+ Err ( err) => {
175
+ return err. into_compile_error ( ) . into ( ) ;
176
+ }
168
177
} ;
169
178
170
179
models. push ( Model :: Attribute ( attribute) ) ;
@@ -180,21 +189,10 @@ pub fn analyze(
180
189
models. push ( Model :: Item ( syn:: Item :: Fn ( ( * f) . clone ( ) ) ) ) ;
181
190
}
182
191
183
- // Move this check for duplicate `#[trace]` attributes to validate
184
- //
185
- // for index in (0..attrs.len()).rev() {
186
- // if let Some(ident) = attrs[index].path.get_ident() {
187
- // if ident.to_string().as_str() == "precondition" {
188
- // let attr = attrs.remove(index);
189
- // let _span = attr.tokens.span();
190
- // }
191
- // }
192
- // }
193
-
194
192
models
195
193
}
196
194
197
- // `Models` a Vec-newtype
195
+ // `Models` are a Vec-newtype
198
196
//
199
197
// A wrapper that allows us to implement the [`From`] trait.
200
198
// The [`From`] trait provides these conveniences (`match` branch):
@@ -216,6 +214,7 @@ impl<T: std::fmt::Debug> Models<T> {
216
214
Models ( Vec :: < T > :: new ( ) )
217
215
}
218
216
217
+ #[ allow( dead_code) ]
219
218
pub fn with_capacity ( capacity : usize ) -> Models < T > {
220
219
Models ( Vec :: < T > :: with_capacity ( capacity) )
221
220
}
@@ -248,7 +247,7 @@ impl<T: std::fmt::Debug> std::ops::DerefMut for Models<T> {
248
247
249
248
#[ derive( Debug ) ]
250
249
struct Item {
251
- item : syn:: Item ,
250
+ // item: syn::Item,
252
251
}
253
252
254
253
#[ derive( Clone , Debug , PartialEq , thiserror:: Error ) ]
@@ -290,7 +289,7 @@ impl<'ast> syn::visit::Visit<'ast> for FnVisitor<'ast> {
290
289
// Err(err) => return err.into_compile_error().into(),
291
290
//
292
291
impl std:: convert:: From < proc_macro2:: TokenStream > for Model {
293
- fn from ( inner : proc_macro2:: TokenStream ) -> Model {
292
+ fn from ( _inner : proc_macro2:: TokenStream ) -> Model {
294
293
let attribute = Default :: default ( ) ;
295
294
Model :: Attribute ( attribute)
296
295
}
@@ -302,7 +301,7 @@ impl std::convert::From<proc_macro2::TokenStream> for Model {
302
301
//
303
302
//
304
303
impl std:: convert:: From < proc_macro2:: TokenStream > for Models < Model > {
305
- fn from ( inner : proc_macro2:: TokenStream ) -> Models < Model > {
304
+ fn from ( _inner : proc_macro2:: TokenStream ) -> Models < Model > {
306
305
let attribute = Default :: default ( ) ;
307
306
let mut models = Models :: < Model > :: new ( ) ;
308
307
models. push ( Model :: Attribute ( attribute) ) ;
@@ -314,15 +313,15 @@ impl Default for Trace {
314
313
fn default ( ) -> Self {
315
314
// let scope = proc_macro2::Ident::new("Local", proc_macro2::Span::call_site());
316
315
// Some(syn::LitBool::new(false, proc_macro2::Span::call_site()));
317
- let name = syn:: LitStr :: new ( "root" , proc_macro2:: Span :: call_site ( ) ) ;
318
- let scope = Scope :: Local ;
316
+ let name = Some ( syn:: LitStr :: new ( "root" , proc_macro2:: Span :: call_site ( ) ) ) ;
317
+ let scope = Some ( Scope :: Local ) ;
319
318
let enter_on_poll = Some ( syn:: LitBool :: new ( true , proc_macro2:: Span :: call_site ( ) ) ) ;
320
319
let recorder = Some ( proc_macro2:: Ident :: new (
321
320
"span" ,
322
321
proc_macro2:: Span :: call_site ( ) ,
323
322
) ) ;
324
323
let recurse = None ;
325
- let root= Some ( syn:: LitBool :: new ( false , proc_macro2:: Span :: call_site ( ) ) ) ;
324
+ let root = Some ( syn:: LitBool :: new ( false , proc_macro2:: Span :: call_site ( ) ) ) ;
326
325
let variables = None ;
327
326
let parent = Some ( syn:: LitStr :: new ( "root" , proc_macro2:: Span :: call_site ( ) ) ) ;
328
327
let async_trait = None ;
@@ -412,8 +411,9 @@ mod tests {
412
411
#[ test]
413
412
fn with_traces ( ) {
414
413
let models = analyze (
415
- parse_quote ! ( # [ trace ] ) ,
414
+ parse_quote ! ( ) ,
416
415
parse_quote ! (
416
+ #[ trace]
417
417
fn f( x: bool ) { }
418
418
) ,
419
419
) ;
@@ -454,30 +454,37 @@ mod tests {
454
454
}
455
455
456
456
#[ test]
457
- #[ should_panic]
458
- fn error_without_trace ( ) {
457
+ fn with_no_trace ( ) {
459
458
analyze (
460
459
parse_quote ! ( ) ,
461
460
parse_quote ! (
462
- #[ without_trace]
463
- fn f( x: bool ) { }
461
+ fn f( x: bool ) -> bool {
462
+ x
463
+ }
464
464
) ,
465
465
) ;
466
466
}
467
467
468
+ // There is no filtering/validation in the `analyze` function.
469
+ // All such checks are done in `validate` function.
468
470
#[ test]
469
- fn preserve_non_dsl_attributes ( ) {
471
+ fn others_with_traces ( ) {
470
472
let models = analyze (
471
- parse_quote ! ( #[ trace] ) ,
473
+ parse_quote ! ( #[ trace( ) ] ) ,
472
474
parse_quote ! (
473
475
#[ a]
474
476
#[ trace]
475
477
#[ b]
476
- fn f( x: bool ) { }
478
+ fn f( x: bool ) -> bool {
479
+ x
480
+ }
477
481
) ,
478
482
) ;
479
-
480
- let expected: & [ Attribute ] = & [ parse_quote ! ( #[ a] ) , parse_quote ! ( #[ b] ) ] ;
483
+ let expected: & [ Attribute ] = & [
484
+ parse_quote ! ( #[ a] ) ,
485
+ parse_quote ! ( #[ trace] ) ,
486
+ parse_quote ! ( #[ b] ) ,
487
+ ] ;
481
488
let model = models. get ( 1 ) . unwrap ( ) ;
482
489
let item = if let Model :: Item ( item) = model {
483
490
item
@@ -491,4 +498,17 @@ mod tests {
491
498
} ;
492
499
assert_eq ! ( expected, actual. attrs) ;
493
500
}
501
+
502
+ #[ test]
503
+ fn others_with_no_trace ( ) {
504
+ let models = analyze (
505
+ parse_quote ! ( ) ,
506
+ parse_quote ! (
507
+ #[ a]
508
+ #[ b]
509
+ fn f( x: bool ) { }
510
+ ) ,
511
+ ) ;
512
+ assert_eq ! ( models. len( ) , 1 ) ;
513
+ }
494
514
}
0 commit comments