Skip to content

Commit

Permalink
add filter::json support
Browse files Browse the repository at this point in the history
Thanks to the newly added ndb_filter_json

Changelog-Added: Add filter::json support
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Aug 3, 2024
1 parent 830bc47 commit f9a8330
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::bindings;
use crate::Note;
use crate::{bindings, Error, Note};
use std::ffi::CString;
use std::os::raw::c_char;
use std::ptr::null_mut;
Expand Down Expand Up @@ -88,6 +87,31 @@ impl Filter {
pub fn as_mut_ptr(&mut self) -> *mut bindings::ndb_filter {
self.data.as_mut_ptr()
}

pub fn json_with_bufsize(&self, bufsize: usize) -> Result<String, Error> {
let mut buf = Vec::with_capacity(bufsize);
unsafe {
let size = bindings::ndb_filter_json(
self.as_ptr(),
buf.as_mut_ptr() as *mut ::std::os::raw::c_char,
bufsize as ::std::os::raw::c_int,
) as usize;

// Step 4: Check the return value for success
if size == 0 {
return Err(Error::BufferOverflow); // Handle the error appropriately
}

buf.set_len(size);

Ok(std::str::from_utf8_unchecked(&buf[..size - 1]).to_string())
}
}

pub fn json(&self) -> Result<String, Error> {
// 1mb buffer
self.json_with_bufsize(1024usize * 1024usize)
}
}

impl Default for FilterBuilder {
Expand Down

0 comments on commit f9a8330

Please sign in to comment.