|
1 |
| -use general_filters::{FunctionInImpl, FunctionInLines}; |
| 1 | +use general_filters::{FunctionInImpl, FunctionInLines, FunctionWithParameter}; |
2 | 2 | use std::{collections::HashMap, fmt, hash::Hash};
|
3 | 3 |
|
4 | 4 | mod filter_parsers;
|
@@ -174,23 +174,86 @@ impl InstantiatedFilter {
|
174 | 174 | }
|
175 | 175 | }
|
176 | 176 |
|
177 |
| -macro_rules! default_filters_by_info { |
178 |
| - ($($filter:ident),*) => { |
179 |
| - HashMap::from([$(($filter.filter_info(), &$filter as &'static dyn Filter)),*]) |
180 |
| - }; |
181 |
| -} |
182 |
| - |
183 | 177 | macro_rules! default_filters {
|
184 | 178 | ($($filter:ident),*) => {
|
185 | 179 | HashMap::from([$(($filter.filter_info().filter_name().to_string(), &$filter as &'static dyn Filter)),*])
|
186 | 180 | };
|
187 | 181 | }
|
188 |
| -#[must_use] |
189 |
| -// TODO: do we really need more than filter name to find the correct filter |
190 |
| -pub fn builtin_filters_by_info() -> HashMap<FilterInformation, &'static dyn Filter> { |
191 |
| - default_filters_by_info!(FunctionInLines, FunctionInImpl) |
| 182 | +pub enum FilterType<'a> { |
| 183 | + All(&'a dyn Filter), |
| 184 | + Many(HashMap<String, &'a dyn Filter>), |
192 | 185 | }
|
193 |
| -#[must_use] |
194 |
| -pub fn builtin_filters() -> HashMap<String, &'static dyn Filter> { |
195 |
| - default_filters!(FunctionInLines, FunctionInImpl) |
| 186 | +pub struct Filters<'a> { |
| 187 | + filters: HashMap<String, FilterType<'a>>, |
| 188 | +} |
| 189 | + |
| 190 | +impl Filters<'static> { |
| 191 | + pub fn default() -> Self { |
| 192 | + Self { |
| 193 | + filters: HashMap::from([ |
| 194 | + ( |
| 195 | + FunctionInLines.filter_info().filter_name().to_string(), |
| 196 | + FilterType::All(&FunctionInLines as &'static dyn Filter), |
| 197 | + ), |
| 198 | + ( |
| 199 | + FunctionInImpl.filter_info().filter_name().to_string(), |
| 200 | + FilterType::Many(HashMap::from([( |
| 201 | + "Rust".to_string(), |
| 202 | + &FunctionInImpl as &'static dyn Filter, |
| 203 | + )])), |
| 204 | + ), |
| 205 | + ( |
| 206 | + FunctionWithParameter |
| 207 | + .filter_info() |
| 208 | + .filter_name() |
| 209 | + .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 | + ), |
| 224 | + ), |
| 225 | + ]), |
| 226 | + } |
| 227 | + } |
| 228 | +} |
| 229 | +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(()), |
| 246 | + } |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | +fn merge_filters<'a>( |
| 251 | + hash_map: &mut HashMap<String, &'a dyn Filter>, |
| 252 | + filter: &'a dyn Filter, |
| 253 | +) -> Result<(), String> { |
| 254 | + todo!() |
| 255 | +} |
| 256 | + |
| 257 | +fn to_filter_type<'a>(filter: &'a dyn Filter) -> FilterType<'a> { |
| 258 | + todo!() |
196 | 259 | }
|
0 commit comments