From f9a8330a04f8fa41a8175ad8a61ceb0f79471da0 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Sat, 3 Aug 2024 14:27:51 -0700 Subject: [PATCH] add filter::json support Thanks to the newly added ndb_filter_json Changelog-Added: Add filter::json support Signed-off-by: William Casarin --- src/filter.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/filter.rs b/src/filter.rs index 6a9b353..ab80801 100644 --- a/src/filter.rs +++ b/src/filter.rs @@ -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; @@ -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 { + 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 { + // 1mb buffer + self.json_with_bufsize(1024usize * 1024usize) + } } impl Default for FilterBuilder {