Skip to content

Commit 59beb4b

Browse files
committed
Working on new way to store a filter list
1 parent 87ed9b9 commit 59beb4b

File tree

2 files changed

+103
-14
lines changed

2 files changed

+103
-14
lines changed

function-grep/src/filter.rs

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use general_filters::{FunctionInImpl, FunctionInLines};
1+
use general_filters::{FunctionInImpl, FunctionInLines, FunctionWithParameter};
22
use std::{collections::HashMap, fmt, hash::Hash};
33

44
mod filter_parsers;
@@ -174,23 +174,86 @@ impl InstantiatedFilter {
174174
}
175175
}
176176

177-
macro_rules! default_filters_by_info {
178-
($($filter:ident),*) => {
179-
HashMap::from([$(($filter.filter_info(), &$filter as &'static dyn Filter)),*])
180-
};
181-
}
182-
183177
macro_rules! default_filters {
184178
($($filter:ident),*) => {
185179
HashMap::from([$(($filter.filter_info().filter_name().to_string(), &$filter as &'static dyn Filter)),*])
186180
};
187181
}
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>),
192185
}
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!()
196259
}

function-grep/src/filter/general_filters.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,29 @@ impl HasFilterInformation for FunctionInImpl {
110110
HashMap::new()
111111
}
112112
}
113+
114+
pub struct FunctionWithParameter;
115+
116+
impl HasFilterInformation for FunctionWithParameter {
117+
fn filter_name(&self) -> String {
118+
todo!()
119+
}
120+
121+
fn description(&self) -> String {
122+
todo!()
123+
}
124+
125+
fn supported_languages(&self) -> SupportedLanguages {
126+
todo!()
127+
}
128+
129+
fn attributes(&self) -> HashMap<Attribute, AttributeType> {
130+
todo!()
131+
}
132+
}
133+
134+
impl Filter for FunctionWithParameter {
135+
fn parse_filter(&self, s: &str) -> Result<FilterFunction, String> {
136+
todo!()
137+
}
138+
}

0 commit comments

Comments
 (0)