@@ -42,13 +42,33 @@ impl Version {
4242 }
4343}
4444
45+ enum VersionReq {
46+ Exactly ( Version ) ,
47+ }
48+
49+ impl FromStr for VersionReq {
50+ type Err = <Version as FromStr >:: Err ;
51+
52+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
53+ Ok ( Self :: Exactly ( s. parse ( ) ?) )
54+ }
55+ }
56+
57+ impl VersionReq {
58+ fn matches ( & self , version : & Version ) -> bool {
59+ match self {
60+ VersionReq :: Exactly ( expected_version) => version == expected_version,
61+ }
62+ }
63+ }
64+
4565struct VersionFilter {
4666 version : Version ,
4767 error : Option < Error > ,
4868}
4969
5070impl VersionFilter {
51- fn extract_version ( & mut self , attrs : & mut Vec < syn:: Attribute > ) -> Option < Version > {
71+ fn extract_requirement ( & mut self , attrs : & mut Vec < syn:: Attribute > ) -> Option < VersionReq > {
5272 let mut opt_version = None ;
5373
5474 attrs. retain ( |attr| {
@@ -72,8 +92,8 @@ impl VersionFilter {
7292 opt_version
7393 }
7494
75- fn matches ( & self , found_version : & Version ) -> bool {
76- & self . version == found_version
95+ fn matches ( & self , requirement : & VersionReq ) -> bool {
96+ requirement . matches ( & self . version )
7797 }
7898
7999 fn filter_fields (
@@ -83,7 +103,7 @@ impl VersionFilter {
83103 fields
84104 . into_pairs ( )
85105 . filter_map (
86- |mut pair| match self . extract_version ( & mut pair. value_mut ( ) . attrs ) {
106+ |mut pair| match self . extract_requirement ( & mut pair. value_mut ( ) . attrs ) {
87107 Some ( version) => self . matches ( & version) . then_some ( pair) ,
88108 None => Some ( pair) ,
89109 } ,
@@ -107,7 +127,7 @@ impl Fold for VersionFilter {
107127 match stmt {
108128 syn:: Stmt :: Local ( syn:: Local { ref mut attrs, .. } )
109129 | syn:: Stmt :: Macro ( syn:: StmtMacro { ref mut attrs, .. } ) => {
110- if let Some ( version) = self . extract_version ( attrs) {
130+ if let Some ( version) = self . extract_requirement ( attrs) {
111131 if !self . matches ( & version) {
112132 stmt = parse_quote ! ( { } ; ) ;
113133 }
@@ -159,7 +179,7 @@ impl Fold for VersionFilter {
159179 | Expr :: Unsafe ( syn:: ExprUnsafe { ref mut attrs, .. } )
160180 | Expr :: While ( syn:: ExprWhile { ref mut attrs, .. } )
161181 | Expr :: Yield ( syn:: ExprYield { ref mut attrs, .. } ) => {
162- if let Some ( version) = self . extract_version ( attrs) {
182+ if let Some ( version) = self . extract_requirement ( attrs) {
163183 if !self . matches ( & version) {
164184 expr = parse_quote ! ( { } ) ;
165185 }
@@ -176,7 +196,7 @@ impl Fold for VersionFilter {
176196 . fields
177197 . into_pairs ( )
178198 . filter_map (
179- |mut pair| match self . extract_version ( & mut pair. value_mut ( ) . attrs ) {
199+ |mut pair| match self . extract_requirement ( & mut pair. value_mut ( ) . attrs ) {
180200 Some ( version) => self . matches ( & version) . then_some ( pair) ,
181201 None => Some ( pair) ,
182202 } ,
@@ -188,7 +208,7 @@ impl Fold for VersionFilter {
188208
189209 fn fold_expr_match ( & mut self , mut expr : syn:: ExprMatch ) -> syn:: ExprMatch {
190210 expr. arms
191- . retain_mut ( |arm| match self . extract_version ( & mut arm. attrs ) {
211+ . retain_mut ( |arm| match self . extract_requirement ( & mut arm. attrs ) {
192212 Some ( version) => self . matches ( & version) ,
193213 None => true ,
194214 } ) ;
@@ -213,7 +233,7 @@ impl Fold for VersionFilter {
213233 | Item :: Type ( syn:: ItemType { ref mut attrs, .. } )
214234 | Item :: Union ( syn:: ItemUnion { ref mut attrs, .. } )
215235 | Item :: Use ( syn:: ItemUse { ref mut attrs, .. } ) => {
216- if let Some ( version) = self . extract_version ( attrs) {
236+ if let Some ( version) = self . extract_requirement ( attrs) {
217237 if !self . matches ( & version) {
218238 item = parse_quote ! (
219239 use { } ;
0 commit comments