Skip to content

Commit

Permalink
filter: friendlier builder interface
Browse files Browse the repository at this point in the history
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Feb 7, 2024
1 parent 5c3beed commit fe6b526
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
18 changes: 9 additions & 9 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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();
Expand Down
10 changes: 8 additions & 2 deletions src/ndb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
Expand Down

0 comments on commit fe6b526

Please sign in to comment.