1
1
use general_filters:: { FunctionInImpl , FunctionInLines , FunctionWithParameterRust } ;
2
2
use std:: {
3
3
collections:: { hash_map, HashMap } ,
4
- fmt,
4
+ fmt:: { self , Display } ,
5
5
hash:: Hash ,
6
6
} ;
7
7
@@ -185,13 +185,14 @@ impl<Supports> InstantiatedFilter<Supports> {
185
185
}
186
186
}
187
187
188
+ #[ derive( Debug , PartialEq , Eq ) ]
188
189
pub struct All ;
189
190
impl From < All > for SupportedLanguages {
190
191
fn from ( _: All ) -> Self {
191
192
Self :: All
192
193
}
193
- // add code here
194
194
}
195
+ #[ derive( Debug , PartialEq , Eq ) ]
195
196
pub struct Language ( String ) ;
196
197
impl From < Language > for SupportedLanguages {
197
198
fn from ( value : Language ) -> Self {
@@ -205,12 +206,13 @@ macro_rules! default_filters {
205
206
HashMap :: from( [ $( ( $filter. filter_info( ) . filter_name( ) . to_string( ) , & $filter as & ' static dyn Filter ) ) ,* ] )
206
207
} ;
207
208
}
209
+ #[ derive( Debug , PartialEq , Eq ) ]
208
210
pub struct Many < T : Info < Supported = Language > > {
209
211
pub name : String ,
210
212
pub filters : HashMap < String , T > ,
211
213
}
212
214
213
- trait Info {
215
+ pub trait Info {
214
216
type Supported ;
215
217
fn filter_name ( & self ) -> String ;
216
218
}
@@ -220,6 +222,7 @@ pub type FilterType<'a> =
220
222
SingleOrMany < & ' a dyn Filter < Supports = All > , & ' a dyn Filter < Supports = Language > > ;
221
223
pub type InstantiatedFilterType =
222
224
SingleOrMany < InstantiatedFilter < All > , InstantiatedFilter < Language > > ;
225
+ #[ derive( Debug , PartialEq , Eq ) ]
223
226
pub enum SingleOrMany < A : Info < Supported = All > , M : Info < Supported = Language > > {
224
227
All ( A ) ,
225
228
Many ( Many < M > ) ,
@@ -228,21 +231,43 @@ impl<A: Info<Supported = All>, M: Info<Supported = Language>> SingleOrMany<A, M>
228
231
#[ must_use]
229
232
pub fn filter_name ( & self ) -> String {
230
233
match self {
231
- SingleOrMany :: All ( f) => f. filter_name ( ) ,
232
- SingleOrMany :: Many ( many) => many. name . clone ( ) ,
234
+ Self :: All ( f) => f. filter_name ( ) ,
235
+ Self :: Many ( many) => many. name . clone ( ) ,
233
236
}
234
237
}
235
238
236
239
#[ must_use]
237
240
pub fn supports ( & self ) -> SupportedLanguages {
238
241
match self {
239
- SingleOrMany :: All ( _) => SupportedLanguages :: All ,
240
- SingleOrMany :: Many ( many) => {
242
+ Self :: All ( _) => SupportedLanguages :: All ,
243
+ Self :: Many ( many) => {
241
244
SupportedLanguages :: Many ( many. filters . keys ( ) . cloned ( ) . collect ( ) )
242
245
}
243
246
}
244
247
}
245
248
}
249
+ impl < A : Info < Supported = All > , M : Info < Supported = Language > > Display for SingleOrMany < A , M > {
250
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
251
+ write ! ( f, "{}" , self . filter_name( ) )
252
+ }
253
+ }
254
+ impl < ' a > FilterType < ' a > {
255
+ pub fn to_filter ( & self , s : & str ) -> Result < InstantiatedFilterType , String > {
256
+ match self {
257
+ SingleOrMany :: All ( a) => a. to_filter ( s) . map ( SingleOrMany :: All ) ,
258
+ SingleOrMany :: Many ( Many { name, filters } ) => filters
259
+ . iter ( )
260
+ . map ( |( name, f) | f. to_filter ( s) . map ( |f| ( name. clone ( ) , f) ) )
261
+ . collect :: < Result < HashMap < _ , _ > , _ > > ( )
262
+ . map ( |filters| {
263
+ SingleOrMany :: Many ( Many {
264
+ name : name. to_string ( ) ,
265
+ filters,
266
+ } )
267
+ } ) ,
268
+ }
269
+ }
270
+ }
246
271
pub struct Filters < ' a > {
247
272
filters : HashMap < String , FilterType < ' a > > ,
248
273
}
@@ -256,7 +281,7 @@ where
256
281
self . filter_info ( ) . filter_name ( ) . to_string ( )
257
282
}
258
283
}
259
- impl < T > Info for InstantiatedFilter < T >
284
+ impl < T > Info for InstantiatedFilter < T >
260
285
where
261
286
SupportedLanguages : From < T > ,
262
287
{
0 commit comments