nostrdb-rs

nostrdb in rust!
git clone git://jb55.com/nostrdb-rs
Log | Files | Refs | Submodules | README | LICENSE

filter.rs (6768B)


      1 use crate::bindings;
      2 use crate::Note;
      3 use std::ffi::CString;
      4 use std::os::raw::c_char;
      5 use std::ptr::null_mut;
      6 use tracing::debug;
      7 
      8 #[derive(Debug)]
      9 pub struct FilterBuilder {
     10     pub data: bindings::ndb_filter,
     11 }
     12 
     13 #[derive(Debug)]
     14 pub struct Filter {
     15     pub data: bindings::ndb_filter,
     16 }
     17 
     18 impl Clone for Filter {
     19     fn clone(&self) -> Self {
     20         let mut new_filter: bindings::ndb_filter = Default::default();
     21         unsafe {
     22             bindings::ndb_filter_clone(
     23                 new_filter.as_mut_ptr(),
     24                 self.as_ptr() as *mut bindings::ndb_filter,
     25             );
     26         };
     27         Filter { data: new_filter }
     28     }
     29 }
     30 
     31 impl bindings::ndb_filter {
     32     fn as_ptr(&self) -> *const bindings::ndb_filter {
     33         self as *const bindings::ndb_filter
     34     }
     35 
     36     fn as_mut_ptr(&mut self) -> *mut bindings::ndb_filter {
     37         self as *mut bindings::ndb_filter
     38     }
     39 }
     40 
     41 impl Default for bindings::ndb_filter {
     42     fn default() -> Self {
     43         let null = null_mut();
     44         let mut filter_data = bindings::ndb_filter {
     45             finalized: 0,
     46             elem_buf: bindings::cursor {
     47                 start: null,
     48                 p: null,
     49                 end: null,
     50             },
     51             data_buf: bindings::cursor {
     52                 start: null,
     53                 p: null,
     54                 end: null,
     55             },
     56             num_elements: 0,
     57             current: -1,
     58             elements: [0, 0, 0, 0, 0, 0, 0],
     59         };
     60 
     61         unsafe {
     62             bindings::ndb_filter_init(filter_data.as_mut_ptr());
     63         };
     64 
     65         filter_data
     66     }
     67 }
     68 
     69 impl Filter {
     70     pub fn new() -> FilterBuilder {
     71         FilterBuilder {
     72             data: Default::default(),
     73         }
     74     }
     75 
     76     pub fn matches(&self, note: &Note) -> bool {
     77         unsafe {
     78             bindings::ndb_filter_matches(self.as_ptr() as *mut bindings::ndb_filter, note.as_ptr())
     79                 != 0
     80         }
     81     }
     82 
     83     pub fn as_ptr(&self) -> *const bindings::ndb_filter {
     84         return self.data.as_ptr();
     85     }
     86 
     87     pub fn as_mut_ptr(&mut self) -> *mut bindings::ndb_filter {
     88         return self.data.as_mut_ptr() as *mut bindings::ndb_filter;
     89     }
     90 }
     91 
     92 impl FilterBuilder {
     93     pub fn new() -> FilterBuilder {
     94         Self {
     95             data: Default::default(),
     96         }
     97     }
     98 
     99     pub fn as_ptr(&self) -> *const bindings::ndb_filter {
    100         return self.data.as_ptr();
    101     }
    102 
    103     pub fn as_mut_ptr(&mut self) -> *mut bindings::ndb_filter {
    104         return self.data.as_mut_ptr();
    105     }
    106 
    107     fn add_int_element(&mut self, i: u64) {
    108         unsafe { bindings::ndb_filter_add_int_element(self.as_mut_ptr(), i) };
    109     }
    110 
    111     fn add_str_element(&mut self, s: &str) {
    112         let c_str = CString::new(s).expect("string to cstring conversion failed");
    113         unsafe {
    114             bindings::ndb_filter_add_str_element(self.as_mut_ptr(), c_str.as_ptr());
    115         };
    116     }
    117 
    118     fn add_id_element(&mut self, id: &[u8; 32]) {
    119         let ptr: *const ::std::os::raw::c_uchar = id.as_ptr() as *const ::std::os::raw::c_uchar;
    120         unsafe {
    121             bindings::ndb_filter_add_id_element(self.as_mut_ptr(), ptr);
    122         };
    123     }
    124 
    125     fn start_field(&mut self, field: bindings::ndb_filter_fieldtype) {
    126         unsafe { bindings::ndb_filter_start_field(self.as_mut_ptr(), field) };
    127     }
    128 
    129     fn start_tags_field(&mut self, tag: char) {
    130         unsafe { bindings::ndb_filter_start_tag_field(self.as_mut_ptr(), tag as u8 as c_char) };
    131     }
    132 
    133     fn start_kinds_field(&mut self) {
    134         self.start_field(bindings::ndb_filter_fieldtype_NDB_FILTER_KINDS);
    135     }
    136 
    137     fn start_authors_field(&mut self) {
    138         self.start_field(bindings::ndb_filter_fieldtype_NDB_FILTER_AUTHORS);
    139     }
    140 
    141     fn start_since_field(&mut self) {
    142         self.start_field(bindings::ndb_filter_fieldtype_NDB_FILTER_SINCE);
    143     }
    144 
    145     #[allow(dead_code)]
    146     fn start_limit_field(&mut self) {
    147         self.start_field(bindings::ndb_filter_fieldtype_NDB_FILTER_LIMIT);
    148     }
    149 
    150     fn start_ids_field(&mut self) {
    151         self.start_field(bindings::ndb_filter_fieldtype_NDB_FILTER_IDS);
    152     }
    153 
    154     #[allow(dead_code)]
    155     fn start_events_field(&mut self) {
    156         self.start_tags_field('e');
    157     }
    158 
    159     fn start_pubkeys_field(&mut self) {
    160         self.start_tags_field('p');
    161     }
    162 
    163     fn start_tag_field(&mut self, tag: char) {
    164         unsafe { bindings::ndb_filter_start_tag_field(self.as_mut_ptr(), tag as u8 as c_char) };
    165     }
    166 
    167     fn end_field(&mut self) {
    168         unsafe { bindings::ndb_filter_end_field(self.as_mut_ptr()) }
    169     }
    170 
    171     pub fn events(&mut self, events: Vec<[u8; 32]>) -> &mut Self {
    172         self.start_tag_field('e');
    173         for ref id in events {
    174             self.add_id_element(id);
    175         }
    176         self.end_field();
    177         self
    178     }
    179 
    180     pub fn ids(&mut self, ids: Vec<[u8; 32]>) -> &mut Self {
    181         self.start_ids_field();
    182         for ref id in ids {
    183             self.add_id_element(id);
    184         }
    185         self.end_field();
    186         self
    187     }
    188 
    189     pub fn pubkeys(&mut self, pubkeys: Vec<[u8; 32]>) -> &mut Self {
    190         self.start_tag_field('p');
    191         for ref pk in pubkeys {
    192             self.add_id_element(pk);
    193         }
    194         self.end_field();
    195         self
    196     }
    197 
    198     pub fn authors(&mut self, authors: Vec<[u8; 32]>) -> &mut Self {
    199         self.start_authors_field();
    200         for author in authors {
    201             self.add_id_element(&author);
    202         }
    203         self.end_field();
    204         self
    205     }
    206 
    207     pub fn kinds(&mut self, kinds: Vec<u64>) -> &mut Self {
    208         self.start_kinds_field();
    209         for kind in kinds {
    210             self.add_int_element(kind);
    211         }
    212         self.end_field();
    213         self
    214     }
    215 
    216     pub fn pubkey(&mut self, pubkeys: Vec<[u8; 32]>) -> &mut Self {
    217         self.start_pubkeys_field();
    218         for ref pubkey in pubkeys {
    219             self.add_id_element(pubkey);
    220         }
    221         self.end_field();
    222         self
    223     }
    224 
    225     pub fn tags(&mut self, tags: Vec<String>, tag: char) -> &mut Self {
    226         self.start_tag_field(tag);
    227         for tag in tags {
    228             self.add_str_element(&tag);
    229         }
    230         self.end_field();
    231         self
    232     }
    233 
    234     pub fn since(&mut self, since: u64) -> &mut Self {
    235         self.start_since_field();
    236         self.add_int_element(since);
    237         self.end_field();
    238         self
    239     }
    240 
    241     pub fn limit(&mut self, limit: u64) -> &mut Self {
    242         self.start_since_field();
    243         self.add_int_element(limit);
    244         self.end_field();
    245         self
    246     }
    247 
    248     pub fn build(&mut self) -> Filter {
    249         unsafe {
    250             bindings::ndb_filter_end(self.as_mut_ptr());
    251         };
    252         Filter { data: self.data }
    253     }
    254 }
    255 
    256 impl Drop for Filter {
    257     fn drop(&mut self) {
    258         debug!("dropping filter {:?}", self);
    259         unsafe { bindings::ndb_filter_destroy(self.as_mut_ptr()) };
    260     }
    261 }