nostrdb-rs

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

commit f9a8330a04f8fa41a8175ad8a61ceb0f79471da0
parent 830bc47e529df178337106945d74dbb961b841cb
Author: William Casarin <jb55@jb55.com>
Date:   Sat,  3 Aug 2024 14:27:51 -0700

add filter::json support

Thanks to the newly added ndb_filter_json

Changelog-Added: Add filter::json support
Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Msrc/filter.rs | 28++++++++++++++++++++++++++--
1 file changed, 26 insertions(+), 2 deletions(-)

diff --git 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<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 {