diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-07-31 02:00:05 +0200 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-07-31 02:00:05 +0200 |
| commit | e970d6f46271b167d2d2f8add29af984da3c31b4 (patch) | |
| tree | 132a4d06fdd9842385061630bd979e5fe94357b1 /cli/src/search.rs | |
| parent | c5e306135b77ac9761ed41b2501b0563124ab703 (diff) | |
| download | money-e970d6f46271b167d2d2f8add29af984da3c31b4.tar.gz | |
rename to set ops
Diffstat (limited to 'cli/src/search.rs')
| -rw-r--r-- | cli/src/search.rs | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/cli/src/search.rs b/cli/src/search.rs index 993a924..b74d20d 100644 --- a/cli/src/search.rs +++ b/cli/src/search.rs @@ -33,15 +33,15 @@ pub enum Constraint { } enum FilterType { - Add(Constraint), + Union(Constraint), + Intersect(Constraint), Subtract(Constraint), - Only(Constraint), } impl FilterType { fn apply<'s>(&self, mut search: Search<'s>) -> Search<'s> { match self { - FilterType::Add(_) => { + FilterType::Union(_) => { //TODO binary search and insert sorted for idx in search .transactions @@ -55,19 +55,19 @@ impl FilterType { search.filtered.sort(); search.filtered.dedup(); } - FilterType::Subtract(_) => { + FilterType::Intersect(_) => { search.filtered = search .filtered .iter() - .filter(|t| !self.satisfies(search.transactions[**t])) + .filter(|t| self.satisfies(search.transactions[**t])) .copied() .collect(); } - FilterType::Only(_) => { + FilterType::Subtract(_) => { search.filtered = search .filtered .iter() - .filter(|t| self.satisfies(search.transactions[**t])) + .filter(|t| !self.satisfies(search.transactions[**t])) .copied() .collect(); } @@ -78,27 +78,27 @@ impl FilterType { fn satisfies(&self, transaction: &Transaction) -> bool { match self { // Category - FilterType::Add(Constraint::Category(category)) + FilterType::Union(Constraint::Category(category)) | FilterType::Subtract(Constraint::Category(category)) - | FilterType::Only(Constraint::Category(category)) + | FilterType::Intersect(Constraint::Category(category)) => &transaction.category == category, - FilterType::Only(Constraint::Before(date)) + FilterType::Intersect(Constraint::Before(date)) => transaction.date < date.clone().get(), - FilterType::Only(Constraint::After(date)) + FilterType::Intersect(Constraint::After(date)) => transaction.date >= date.clone().get(), - FilterType::Add(Constraint::Before(date)) + FilterType::Subtract(Constraint::Before(date)) => transaction.date < date.clone().get(), - FilterType::Add(Constraint::After(date)) + FilterType::Subtract(Constraint::After(date)) => transaction.date >= date.clone().get(), - FilterType::Subtract(Constraint::Before(date)) + FilterType::Union(Constraint::Before(date)) => transaction.date < date.clone().get(), - FilterType::Subtract(Constraint::After(date)) + FilterType::Union(Constraint::After(date)) => transaction.date >= date.clone().get(), } } @@ -126,8 +126,8 @@ impl<'t> Search<'t> { for rule in rules.split(' ') { let (filter_type, rule): (fn(Constraint) -> FilterType, &str) = match rule.chars().nth(0).unwrap() { '-' => (FilterType::Subtract, &rule[1..]), - '+' => (FilterType::Add, &rule[1..]), - _ => (FilterType::Only, &rule[..]), + '+' => (FilterType::Union, &rule[1..]), + _ => (FilterType::Intersect, &rule[..]), }; //TODO lexing? can do a function for "spaces inside" instead |
