11use general_filters:: { FunctionInImpl , FunctionInLines , FunctionWithParameterRust } ;
22use std:: {
33 collections:: { hash_map, HashMap } ,
4- fmt,
4+ fmt:: { self , Display } ,
55 hash:: Hash ,
66} ;
77
@@ -185,13 +185,14 @@ impl<Supports> InstantiatedFilter<Supports> {
185185 }
186186}
187187
188+ #[ derive( Debug , PartialEq , Eq ) ]
188189pub struct All ;
189190impl From < All > for SupportedLanguages {
190191 fn from ( _: All ) -> Self {
191192 Self :: All
192193 }
193- // add code here
194194}
195+ #[ derive( Debug , PartialEq , Eq ) ]
195196pub struct Language ( String ) ;
196197impl From < Language > for SupportedLanguages {
197198 fn from ( value : Language ) -> Self {
@@ -205,12 +206,13 @@ macro_rules! default_filters {
205206 HashMap :: from( [ $( ( $filter. filter_info( ) . filter_name( ) . to_string( ) , & $filter as & ' static dyn Filter ) ) ,* ] )
206207 } ;
207208}
209+ #[ derive( Debug , PartialEq , Eq ) ]
208210pub struct Many < T : Info < Supported = Language > > {
209211 pub name : String ,
210212 pub filters : HashMap < String , T > ,
211213}
212214
213- trait Info {
215+ pub trait Info {
214216 type Supported ;
215217 fn filter_name ( & self ) -> String ;
216218}
@@ -220,6 +222,7 @@ pub type FilterType<'a> =
220222 SingleOrMany < & ' a dyn Filter < Supports = All > , & ' a dyn Filter < Supports = Language > > ;
221223pub type InstantiatedFilterType =
222224 SingleOrMany < InstantiatedFilter < All > , InstantiatedFilter < Language > > ;
225+ #[ derive( Debug , PartialEq , Eq ) ]
223226pub enum SingleOrMany < A : Info < Supported = All > , M : Info < Supported = Language > > {
224227 All ( A ) ,
225228 Many ( Many < M > ) ,
@@ -228,21 +231,43 @@ impl<A: Info<Supported = All>, M: Info<Supported = Language>> SingleOrMany<A, M>
228231 #[ must_use]
229232 pub fn filter_name ( & self ) -> String {
230233 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 ( ) ,
233236 }
234237 }
235238
236239 #[ must_use]
237240 pub fn supports ( & self ) -> SupportedLanguages {
238241 match self {
239- SingleOrMany :: All ( _) => SupportedLanguages :: All ,
240- SingleOrMany :: Many ( many) => {
242+ Self :: All ( _) => SupportedLanguages :: All ,
243+ Self :: Many ( many) => {
241244 SupportedLanguages :: Many ( many. filters . keys ( ) . cloned ( ) . collect ( ) )
242245 }
243246 }
244247 }
245248}
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+ }
246271pub struct Filters < ' a > {
247272 filters : HashMap < String , FilterType < ' a > > ,
248273}
@@ -256,7 +281,7 @@ where
256281 self . filter_info ( ) . filter_name ( ) . to_string ( )
257282 }
258283}
259- impl < T > Info for InstantiatedFilter < T >
284+ impl < T > Info for InstantiatedFilter < T >
260285where
261286 SupportedLanguages : From < T > ,
262287{
0 commit comments