nostrdb-rs

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

ingest.rs (1236B)


      1 use crate::bindings;
      2 use std::ffi::CString;
      3 
      4 pub struct IngestMetadata {
      5     meta: bindings::ndb_ingest_meta,
      6     relay: Option<CString>,
      7 }
      8 
      9 impl Default for IngestMetadata {
     10     fn default() -> Self {
     11         Self {
     12             relay: None,
     13             meta: bindings::ndb_ingest_meta {
     14                 client: 0,
     15                 relay: std::ptr::null_mut(),
     16             },
     17         }
     18     }
     19 }
     20 
     21 impl IngestMetadata {
     22     pub fn new() -> Self {
     23         Self::default()
     24     }
     25 
     26     /// Ingest a relay-sent event in the form `["EVENT", {"id:"...}]`
     27     pub fn client(mut self, from_client: bool) -> Self {
     28         self.meta.client = if from_client { 1 } else { 0 };
     29         self
     30     }
     31 
     32     fn relay_str(&self) -> *const ::std::os::raw::c_char {
     33         match &self.relay {
     34             Some(relay_cstr) => relay_cstr.as_ptr(),
     35             None => std::ptr::null(),
     36         }
     37     }
     38 
     39     pub fn as_mut_ptr(&mut self) -> *mut bindings::ndb_ingest_meta {
     40         // update the ingest relay str with our cstr if we have one
     41         self.meta.relay = self.relay_str();
     42         &mut self.meta
     43     }
     44 
     45     pub fn relay(mut self, relay: &str) -> Self {
     46         self.relay = Some(CString::new(relay).expect("should never happen"));
     47         self
     48     }
     49 }