commit fe6b5268673eb1eb6296f34b08668f2f7486f69c
parent 5c3beed3cfefdf786923dd2f2114b24b543f17c3
Author: William Casarin <jb55@jb55.com>
Date: Wed, 7 Feb 2024 14:46:51 -0800
filter: friendlier builder interface
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
2 files changed, 17 insertions(+), 11 deletions(-)
diff --git a/src/filter.rs b/src/filter.rs
@@ -121,7 +121,7 @@ impl Filter {
unsafe { bindings::ndb_filter_end_field(self.as_mut_ptr()) }
}
- pub fn events(self, events: Vec<&[u8; 32]>) -> Filter {
+ pub fn events(&mut self, events: Vec<&[u8; 32]>) -> &mut Filter {
self.start_tag_field('e');
for id in events {
self.add_id_element(id);
@@ -130,7 +130,7 @@ impl Filter {
self
}
- pub fn ids(self, ids: Vec<&[u8; 32]>) -> Filter {
+ pub fn ids(&mut self, ids: Vec<&[u8; 32]>) -> &mut Filter {
self.start_ids_field();
for id in ids {
self.add_id_element(id);
@@ -139,7 +139,7 @@ impl Filter {
self
}
- pub fn pubkeys(self, pubkeys: Vec<&[u8; 32]>) -> Filter {
+ pub fn pubkeys(&mut self, pubkeys: Vec<&[u8; 32]>) -> &mut Filter {
self.start_tag_field('p');
for pk in pubkeys {
self.add_id_element(pk);
@@ -148,7 +148,7 @@ impl Filter {
self
}
- pub fn authors(self, authors: Vec<&[u8; 32]>) -> Filter {
+ pub fn authors(&mut self, authors: Vec<&[u8; 32]>) -> &mut Filter {
self.start_authors_field();
for author in authors {
self.add_id_element(author);
@@ -157,7 +157,7 @@ impl Filter {
self
}
- pub fn kinds(self, kinds: Vec<u64>) -> Filter {
+ pub fn kinds(&mut self, kinds: Vec<u64>) -> &mut Filter {
self.start_kinds_field();
for kind in kinds {
self.add_int_element(kind);
@@ -166,7 +166,7 @@ impl Filter {
self
}
- pub fn pubkey<'a>(self, pubkeys: Vec<&'a [u8; 32]>) -> Filter {
+ pub fn pubkey(&mut self, pubkeys: Vec<&[u8; 32]>) -> &mut Filter {
self.start_pubkeys_field();
for pubkey in pubkeys {
self.add_id_element(pubkey);
@@ -175,7 +175,7 @@ impl Filter {
self
}
- pub fn tags(self, tags: Vec<String>, tag: char) -> Filter {
+ pub fn tags(&mut self, tags: Vec<String>, tag: char) -> &mut Filter {
self.start_tag_field(tag);
for tag in tags {
self.add_str_element(&tag);
@@ -184,14 +184,14 @@ impl Filter {
self
}
- pub fn since(self, since: u64) -> Filter {
+ pub fn since(&mut self, since: u64) -> &mut Filter {
self.start_since_field();
self.add_int_element(since);
self.end_field();
self
}
- pub fn limit(self, limit: u64) -> Filter {
+ pub fn limit(&mut self, limit: u64) -> &mut Filter {
self.start_since_field();
self.add_int_element(limit);
self.end_field();
diff --git a/src/ndb.rs b/src/ndb.rs
@@ -256,7 +256,10 @@ mod tests {
{
let ndb = Ndb::new(db, &Config::new()).expect("ndb");
- let filter = Filter::new().kinds(vec![1]);
+
+ let mut filter = Filter::new();
+ filter.kinds(vec![1]);
+
let sub = ndb.subscribe(filter).expect("sub_id");
let waiter = ndb.wait_for_notes(&sub, 1);
ndb.process_event(r#"["EVENT","b",{"id": "702555e52e82cc24ad517ba78c21879f6e47a7c0692b9b20df147916ae8731a3","pubkey": "32bf915904bfde2d136ba45dde32c88f4aca863783999faea2e847a8fafd2f15","created_at": 1702675561,"kind": 1,"tags": [],"content": "hello, world","sig": "2275c5f5417abfd644b7bc74f0388d70feb5d08b6f90fa18655dda5c95d013bfbc5258ea77c05b7e40e0ee51d8a2efa931dc7a0ec1db4c0a94519762c6625675"}]"#).expect("process ok");
@@ -272,7 +275,10 @@ mod tests {
{
let ndb = Ndb::new(db, &Config::new()).expect("ndb");
- let filter = Filter::new().kinds(vec![1]);
+
+ let mut filter = Filter::new();
+ filter.kinds(vec![1]);
+
let sub = ndb.subscribe(filter).expect("sub_id");
ndb.process_event(r#"["EVENT","b",{"id": "702555e52e82cc24ad517ba78c21879f6e47a7c0692b9b20df147916ae8731a3","pubkey": "32bf915904bfde2d136ba45dde32c88f4aca863783999faea2e847a8fafd2f15","created_at": 1702675561,"kind": 1,"tags": [],"content": "hello, world","sig": "2275c5f5417abfd644b7bc74f0388d70feb5d08b6f90fa18655dda5c95d013bfbc5258ea77c05b7e40e0ee51d8a2efa931dc7a0ec1db4c0a94519762c6625675"}]"#).expect("process ok");
// this is too fast, we should have nothing