summaryrefslogtreecommitdiffstats
path: root/cli
diff options
context:
space:
mode:
Diffstat (limited to 'cli')
-rw-r--r--cli/src/search.rs72
1 files changed, 35 insertions, 37 deletions
diff --git a/cli/src/search.rs b/cli/src/search.rs
index 4047c4b..c9b8adb 100644
--- a/cli/src/search.rs
+++ b/cli/src/search.rs
@@ -104,65 +104,63 @@ pub enum Filter {
Subtract(Constraint),
}
-impl Filter {
- fn apply<'s>(&self, mut search: Search<'s>) -> Search<'s> {
- match self {
+pub struct Search<'t> {
+ filtered: Vec<usize>,
+ transactions: &'t [Transaction],
+}
+
+impl<'t> Search<'t> {
+ pub fn new(transactions: &'t [Transaction]) -> Self {
+ Self {
+ filtered: std::iter::successors(Some(0_usize), |n| Some(n.checked_add(1).unwrap()))
+ .take(transactions.len())
+ .collect(),
+ transactions,
+ }
+ }
+
+ pub fn get(&self) -> Vec<&'t Transaction> {
+ self
+ .filtered
+ .iter()
+ .map(|idx| &self.transactions[*idx])
+ .collect()
+ }
+
+ pub fn apply(mut self, filter: Filter) -> Self {
+ match filter {
Filter::Union(constraint) => {
//TODO binary search and insert sorted
- for idx in search
+ for idx in self
.transactions
.iter()
.enumerate()
.filter(|(_, t)| constraint.satisfies(t))
.map(|(idx, _)| idx)
{
- search.filtered.push(idx);
+ self.filtered.push(idx);
}
- search.filtered.sort();
- search.filtered.dedup();
+ self.filtered.sort();
+ self.filtered.dedup();
}
Filter::Intersect(constraint) => {
- search.filtered = search
+ self.filtered = self
.filtered
.iter()
- .filter(|t| constraint.satisfies(&search.transactions[**t]))
+ .filter(|t| constraint.satisfies(&self.transactions[**t]))
.copied()
.collect();
}
Filter::Subtract(constraint) => {
- search.filtered = search
+ self.filtered = self
.filtered
.iter()
- .filter(|t| !constraint.satisfies(&search.transactions[**t]))
+ .filter(|t| !constraint.satisfies(&self.transactions[**t]))
.copied()
.collect();
}
}
- search
- }
-}
-
-pub struct Search<'t> {
- filtered: Vec<usize>,
- transactions: &'t [Transaction],
-}
-
-impl<'t> Search<'t> {
- pub fn new(transactions: &'t [Transaction]) -> Self {
- Self {
- filtered: std::iter::successors(Some(0_usize), |n| Some(n.checked_add(1).unwrap()))
- .take(transactions.len())
- .collect(),
- transactions,
- }
- }
-
- pub fn get(&self) -> Vec<&'t Transaction> {
self
- .filtered
- .iter()
- .map(|idx| &self.transactions[*idx])
- .collect()
}
pub fn parse(mut self, rules: String) -> Self {
@@ -189,7 +187,7 @@ impl<'t> Search<'t> {
_ => panic!(),
};
- self = filter(constraint).apply(self);
+ self = self.apply(filter(constraint));
}
self
}
@@ -233,7 +231,7 @@ mod test {
assert_eq!(search.get().len(), 3);
let category_filter = Filter::Intersect(Constraint::Category("C1".to_string()));
- search = category_filter.apply(search);
+ search = search.apply(category_filter);
assert_eq!(search.get().len(), 2);
}
}