1
- use general_filters:: { FunctionInImpl , FunctionInLines , FunctionWithParameter } ;
1
+ use general_filters:: { FunctionInImpl , FunctionInLines , FunctionWithParameterRust } ;
2
2
use std:: { collections:: HashMap , fmt, hash:: Hash } ;
3
3
4
4
mod filter_parsers;
@@ -38,49 +38,56 @@ pub trait Filter: HasFilterInformation {
38
38
// TODO: make way to parse from hashmap of attribute to string
39
39
fn to_filter ( & self , s : & str ) -> Result < InstantiatedFilter , String > {
40
40
let filter = self . parse_filter ( s) ?;
41
+ let info = self . filter_info ( ) ;
42
+ let info = FilterInformation {
43
+ supported_languages : info. supported_languages . into ( ) ,
44
+ description : info. description ,
45
+ attributes : info. attributes ,
46
+ filter_name : info. filter_name ,
47
+ } ;
41
48
Ok ( InstantiatedFilter {
42
- filter_information : self . filter_info ( ) ,
49
+ filter_information : info ,
43
50
filter_function : filter,
44
51
} )
45
52
}
46
53
}
47
54
48
55
#[ derive( Debug , Clone ) ]
49
- pub struct FilterInformation {
56
+ pub struct FilterInformation < Supports > {
50
57
/// the name of the filter (so users can find the filter)
51
58
filter_name : String ,
52
59
/// describes what the filter does and how it parses
53
60
description : String ,
54
61
/// what languages this filter works on
55
- supported_languages : SupportedLanguages ,
62
+ supported_languages : Supports ,
56
63
57
64
attributes : HashMap < Attribute , AttributeType > ,
58
65
}
59
66
60
- impl fmt:: Display for FilterInformation {
67
+ impl < Supports > fmt:: Display for FilterInformation < Supports > {
61
68
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
62
69
write ! ( f, "filter {}" , self . filter_name)
63
70
}
64
71
}
65
72
66
- impl Hash for FilterInformation {
73
+ impl < Supports : Hash > Hash for FilterInformation < Supports > {
67
74
fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
68
75
self . filter_name . hash ( state) ;
69
76
self . description . hash ( state) ;
70
77
self . supported_languages . hash ( state) ;
71
78
}
72
79
}
73
- impl PartialEq for FilterInformation {
80
+ impl < Supports : PartialEq > PartialEq for FilterInformation < Supports > {
74
81
fn eq ( & self , other : & Self ) -> bool {
75
82
self . filter_name == other. filter_name
76
83
&& self . description == other. description
77
84
&& self . supported_languages == other. supported_languages
78
85
}
79
86
}
80
- impl Eq for FilterInformation { }
81
- impl FilterInformation {
87
+ impl < Supports : PartialEq > Eq for FilterInformation < Supports > { }
88
+ impl < Supports > FilterInformation < Supports > {
82
89
#[ must_use]
83
- pub const fn supported_languages ( & self ) -> & SupportedLanguages {
90
+ pub const fn supported_languages ( & self ) -> & Supports {
84
91
& self . supported_languages
85
92
}
86
93
@@ -100,20 +107,21 @@ impl FilterInformation {
100
107
}
101
108
}
102
109
pub trait HasFilterInformation {
110
+ type Supports : Into < SupportedLanguages > ;
103
111
/// the name of the filter (so users can find the filter)
104
112
fn filter_name ( & self ) -> String ;
105
113
/// describes what the filter does and how it parses
106
114
fn description ( & self ) -> String ;
107
115
/// what languages this filter works on
108
- fn supported_languages ( & self ) -> SupportedLanguages ;
116
+ fn supports ( & self ) -> Self :: Supports ;
109
117
fn attributes ( & self ) -> HashMap < Attribute , AttributeType > ;
110
118
// TODO: have filter creation informaton about types and fields for uis
111
- fn filter_info ( & self ) -> FilterInformation {
119
+ fn filter_info ( & self ) -> FilterInformation < Self :: Supports > {
112
120
FilterInformation {
113
121
filter_name : self . filter_name ( ) ,
114
122
attributes : self . attributes ( ) ,
115
123
description : self . description ( ) ,
116
- supported_languages : self . supported_languages ( ) ,
124
+ supported_languages : self . supports ( ) ,
117
125
}
118
126
}
119
127
}
@@ -123,7 +131,7 @@ type FilterFunction = Box<dyn Fn(&Node<'_>) -> bool + Send + Sync>;
123
131
// filter has and their type so that we can make macro that creates parser, and also so that we can
124
132
// communicate to a gui (or tui) that labels, and types of each input
125
133
pub struct InstantiatedFilter {
126
- filter_information : FilterInformation ,
134
+ filter_information : FilterInformation < SupportedLanguages > ,
127
135
filter_function : FilterFunction ,
128
136
}
129
137
@@ -174,14 +182,44 @@ impl InstantiatedFilter {
174
182
}
175
183
}
176
184
185
+ pub struct All ;
186
+ impl From < All > for SupportedLanguages {
187
+ fn from ( _: All ) -> Self {
188
+ Self :: All
189
+ }
190
+ // add code here
191
+ }
192
+ pub struct Language ( String ) ;
193
+ impl From < Language > for SupportedLanguages {
194
+ fn from ( value : Language ) -> Self {
195
+ Self :: Single ( value. 0 )
196
+ }
197
+ }
198
+ // impl Language for
177
199
macro_rules! default_filters {
178
- ( $( $filter: ident) ,* ) => {
200
+ ( $( $filter: ident) ,* ) => { S
179
201
HashMap :: from( [ $( ( $filter. filter_info( ) . filter_name( ) . to_string( ) , & $filter as & ' static dyn Filter ) ) ,* ] )
180
202
} ;
181
203
}
204
+ pub struct Many < ' a > {
205
+ pub name : String ,
206
+ pub filters : HashMap < String , & ' a dyn Filter < Supports = Language > > ,
207
+ }
208
+
182
209
pub enum FilterType < ' a > {
183
- All ( & ' a dyn Filter ) ,
184
- Many ( HashMap < String , & ' a dyn Filter > ) ,
210
+ All ( & ' a dyn Filter < Supports = All > ) ,
211
+ Many ( Many < ' a > ) ,
212
+ }
213
+ impl < ' a > FilterType < ' a > {
214
+ fn filter_name ( & self ) -> String {
215
+ todo ! ( )
216
+ }
217
+
218
+ fn supports ( & self ) -> SupportedLanguages {
219
+ todo ! ( )
220
+ }
221
+
222
+ // add code here
185
223
}
186
224
pub struct Filters < ' a > {
187
225
filters : HashMap < String , FilterType < ' a > > ,
@@ -193,67 +231,59 @@ impl Filters<'static> {
193
231
filters : HashMap :: from ( [
194
232
(
195
233
FunctionInLines . filter_info ( ) . filter_name ( ) . to_string ( ) ,
196
- FilterType :: All ( & FunctionInLines as & ' static dyn Filter ) ,
234
+ FilterType :: All ( & FunctionInLines as & ' static dyn Filter < Supports = All > ) ,
197
235
) ,
198
236
(
199
237
FunctionInImpl . filter_info ( ) . filter_name ( ) . to_string ( ) ,
200
- FilterType :: Many ( HashMap :: from ( [ (
201
- "Rust" . to_string ( ) ,
202
- & FunctionInImpl as & ' static dyn Filter ,
203
- ) ] ) ) ,
238
+ FilterType :: Many ( Many {
239
+ name : "function_in_impl" . to_string ( ) ,
240
+ filters : HashMap :: from ( [ (
241
+ "Rust" . to_string ( ) ,
242
+ & FunctionInImpl as & ' static dyn Filter < Supports = Language > ,
243
+ ) ] ) ,
244
+ } ) ,
204
245
) ,
205
246
(
206
- FunctionWithParameter
247
+ FunctionWithParameterRust
207
248
. filter_info ( )
208
249
. filter_name ( )
209
250
. to_string ( ) ,
210
- FilterType :: Many (
211
- match FunctionWithParameter . filter_info ( ) . supported_languages ( ) {
212
- SupportedLanguages :: All => unreachable ! ( ) ,
213
- SupportedLanguages :: Many ( vec) => {
214
- HashMap :: from_iter ( vec. into_iter ( ) . map ( |lang| {
215
- (
216
- lang. to_string ( ) ,
217
- & FunctionWithParameter as & ' static dyn Filter ,
218
- )
219
- } ) )
220
- }
221
- SupportedLanguages :: Single ( _) => unreachable ! ( ) ,
222
- } ,
223
- ) ,
251
+ FilterType :: Many ( Many {
252
+ name : "function_with_parameter" . to_string ( ) ,
253
+ filters : HashMap :: from ( [ (
254
+ FunctionWithParameterRust . supports ( ) . 0 ,
255
+ & FunctionWithParameterRust as & ' static dyn Filter < Supports = Language > ,
256
+ ) ] ) ,
257
+ } ) ,
224
258
) ,
225
259
] ) ,
226
260
}
227
261
}
228
262
}
263
+
229
264
impl < ' a > Filters < ' a > {
230
- pub fn add_filter ( & mut self , filter : & ' a dyn Filter ) -> Result < ( ) , String > {
231
- let result = self
232
- . filters
233
- . get_mut ( & filter. filter_name ( ) )
234
- . map ( |filters| match filters {
235
- FilterType :: All ( _) => Err ( "cannot add to an all filter" . to_string ( ) ) ,
236
- FilterType :: Many ( hash_map) => merge_filters ( hash_map, filter) ,
237
- } )
238
- . or_else ( || {
239
- self . filters
240
- . insert ( filter. filter_name ( ) , to_filter_type ( filter) ) ;
241
- Some ( Ok ( ( ) ) )
242
- } ) ;
243
- match result {
244
- Some ( res) => res,
245
- None => Ok ( ( ) ) ,
265
+ pub fn add_filter ( & mut self , filter : impl Into < FilterType < ' a > > ) -> Result < ( ) , String > {
266
+ let filter = filter. into ( ) ;
267
+ let name = filter. filter_name ( ) . clone ( ) ;
268
+ {
269
+ let this = self . filters . get_mut ( & name) ;
270
+ match this {
271
+ Some ( filters) => match filters {
272
+ FilterType :: All ( _) => Err ( "cannot add to an all filter" . to_string ( ) ) ,
273
+ FilterType :: Many ( Many { filters, .. } ) => merge_filters ( filters, filter) ,
274
+ } ,
275
+ None => {
276
+ self . filters . insert ( name, filter) ;
277
+ Ok ( ( ) )
278
+ }
279
+ }
246
280
}
247
281
}
248
282
}
249
283
250
284
fn merge_filters < ' a > (
251
- hash_map : & mut HashMap < String , & ' a dyn Filter > ,
252
- filter : & ' a dyn Filter ,
285
+ hash_map : & mut HashMap < String , & ' a dyn Filter < Supports = Language > > ,
286
+ filter : FilterType < ' a > ,
253
287
) -> Result < ( ) , String > {
254
288
todo ! ( )
255
289
}
256
-
257
- fn to_filter_type < ' a > ( filter : & ' a dyn Filter ) -> FilterType < ' a > {
258
- todo ! ( )
259
- }
0 commit comments