Эх сурвалжийг харах

Extend filter to support since, until and status

Cesar Rodas 10 сар өмнө
parent
commit
2aee263e82
1 өөрчлөгдсөн 77 нэмэгдсэн , 0 устгасан
  1. 77 0
      utxo/src/filter.rs

+ 77 - 0
utxo/src/filter.rs

@@ -55,6 +55,7 @@ impl Filter {
         self.revisions.sort();
         self.accounts.sort();
         self.typ.sort();
+        self.status.sort();
         self.tags.sort();
         self
     }
@@ -63,6 +64,24 @@ impl Filter {
     ///
     /// Before this function is called, the filter needs to be prepareted with `prepare` function
     pub fn matches(&self, base: &BaseTx, revision: &Revision) -> bool {
+        let since = self
+            .since
+            .map(|since| since <= base.created_at)
+            .unwrap_or(true);
+
+        let until = self
+            .until
+            .map(|until| until >= base.created_at)
+            .unwrap_or(true);
+
+        if !since || !until {
+            return false;
+        }
+
+        if !self.status.is_empty() && self.status.binary_search(&revision.status).is_err() {
+            return false;
+        }
+
         if !self.ids.is_empty() && self.ids.binary_search(&revision.transaction_id).is_err() {
             return false;
         }
@@ -197,3 +216,61 @@ pub(crate) mod option_ts_seconds {
         }
     }
 }
+
+#[cfg(test)]
+mod test {
+    use chrono::DateTime;
+
+    use crate::{Filter, Transaction};
+
+    #[test]
+    fn since_and_until() {
+        let current = Transaction::new_external_deposit(
+            "test".to_owned(),
+            "settled".parse().expect("valid status"),
+            vec![],
+            vec![],
+        )
+        .expect("valid transaction");
+
+        let filter_until = Filter {
+            until: DateTime::from_timestamp(9999, 99),
+            ..Default::default()
+        }
+        .prepare();
+        let filter_since = Filter {
+            since: DateTime::from_timestamp(9999, 99),
+            ..Default::default()
+        }
+        .prepare();
+        assert!(!filter_until.matches(&current.transaction, &current.revision));
+        assert!(filter_since.matches(&current.transaction, &current.revision));
+    }
+
+    #[test]
+    fn status() {
+        let current = Transaction::new_external_deposit(
+            "test".to_owned(),
+            "settled".parse().expect("valid status"),
+            vec![],
+            vec![],
+        )
+        .expect("valid transaction");
+
+        let filter_foo = Filter {
+            status: vec!["foo".parse().expect("valid status")],
+            ..Default::default()
+        }
+        .prepare();
+        let filter_settled = Filter {
+            status: vec![
+                "settled".parse().expect("valid status"),
+                "a".parse().expect("valid statuses"),
+            ],
+            ..Default::default()
+        }
+        .prepare();
+        assert!(!filter_foo.matches(&current.transaction, &current.revision));
+        assert!(filter_settled.matches(&current.transaction, &current.revision));
+    }
+}