@@ -11,7 +11,7 @@ use std::collections::HashSet;
11
11
/// - attributes slice
12
12
/// - manually feeding values into the underlying contexts
13
13
///
14
- /// Query this context to know if you need skip a block.
14
+ /// Query this context to know if you need to skip a block.
15
15
#[ derive( Default , Clone ) ]
16
16
pub ( crate ) struct SkipContext {
17
17
pub ( crate ) macros : SkipNameContext ,
@@ -20,8 +20,8 @@ pub(crate) struct SkipContext {
20
20
21
21
impl SkipContext {
22
22
pub ( crate ) fn update_with_attrs ( & mut self , attrs : & [ ast:: Attribute ] ) {
23
- self . macros . append ( get_skip_names ( "macros" , attrs) ) ;
24
- self . attributes . append ( get_skip_names ( "attributes" , attrs) ) ;
23
+ self . macros . extend ( get_skip_names ( "macros" , attrs) ) ;
24
+ self . attributes . extend ( get_skip_names ( "attributes" , attrs) ) ;
25
25
}
26
26
27
27
pub ( crate ) fn update ( & mut self , other : SkipContext ) {
@@ -34,28 +34,52 @@ impl SkipContext {
34
34
/// Track which names to skip.
35
35
///
36
36
/// Query this context with a string to know whether to skip it.
37
- #[ derive( Default , Clone ) ]
38
- pub ( crate ) struct SkipNameContext {
39
- all : bool ,
40
- values : HashSet < String > ,
37
+ #[ derive( Clone ) ]
38
+ pub ( crate ) enum SkipNameContext {
39
+ All ,
40
+ Values ( HashSet < String > ) ,
41
41
}
42
42
43
- impl SkipNameContext {
44
- pub ( crate ) fn append ( & mut self , values : Vec < String > ) {
45
- self . values . extend ( values) ;
43
+ impl Default for SkipNameContext {
44
+ fn default ( ) -> Self {
45
+ Self :: Values ( Default :: default ( ) )
46
+ }
47
+ }
48
+
49
+ impl Extend < String > for SkipNameContext {
50
+ fn extend < T : IntoIterator < Item = String > > ( & mut self , iter : T ) {
51
+ match self {
52
+ Self :: All => { }
53
+ Self :: Values ( values) => values. extend ( iter) ,
54
+ }
46
55
}
56
+ }
47
57
58
+ impl SkipNameContext {
48
59
pub ( crate ) fn update ( & mut self , other : Self ) {
49
- self . all = self . all || other. all ;
50
- self . values . extend ( other. values ) ;
60
+ match ( self , other) {
61
+ // If we're already skipping everything, nothing more can be added
62
+ ( Self :: All , _) => { }
63
+ // If we want to skip all, set it
64
+ ( this, Self :: All ) => {
65
+ * this = Self :: All ;
66
+ }
67
+ // If we have some new values to skip, add them
68
+ ( Self :: Values ( existing_values) , Self :: Values ( new_values) ) => {
69
+ existing_values. extend ( new_values)
70
+ }
71
+ }
51
72
}
52
73
53
74
pub ( crate ) fn skip ( & self , name : & str ) -> bool {
54
- self . all || self . values . contains ( name)
75
+ match self {
76
+ Self :: All => true ,
77
+ Self :: Values ( values) => values. contains ( name) ,
78
+ }
55
79
}
56
80
57
- pub ( crate ) fn set_all ( & mut self , all : bool ) {
58
- self . all = all ;
81
+ pub ( crate ) fn skip_all ( & mut self ) {
82
+ * self = Self :: All ;
59
83
}
60
84
}
61
85
0 commit comments