Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose ndb_note_json #13

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nostrdb
Submodule nostrdb updated 9 files
+1 −1 .envrc
+22 −674 LICENSE
+1 −1 Makefile
+67 −11 ndb.c
+84 −6 src/nostrdb.c
+9 −0 src/nostrdb.h
+6 −18 src/print_util.h
+1 −2 src/protected_queue.h
+0 −32 src/thread.h
46 changes: 44 additions & 2 deletions src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2663,6 +2663,8 @@ pub type ndb_id_fn = ::std::option::Option<
arg2: *const ::std::os::raw::c_char,
) -> ndb_idres,
>;
pub type ndb_sub_fn =
::std::option::Option<unsafe extern "C" fn(arg1: *mut ::std::os::raw::c_void, subid: u64)>;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ndb_id_cb {
Expand Down Expand Up @@ -3538,14 +3540,16 @@ pub struct ndb_config {
pub mapsize: usize,
pub filter_context: *mut ::std::os::raw::c_void,
pub ingest_filter: ndb_ingest_filter_fn,
pub sub_cb_ctx: *mut ::std::os::raw::c_void,
pub sub_cb: ndb_sub_fn,
}
#[test]
fn bindgen_test_layout_ndb_config() {
const UNINIT: ::std::mem::MaybeUninit<ndb_config> = ::std::mem::MaybeUninit::uninit();
let ptr = UNINIT.as_ptr();
assert_eq!(
::std::mem::size_of::<ndb_config>(),
32usize,
48usize,
concat!("Size of: ", stringify!(ndb_config))
);
assert_eq!(
Expand Down Expand Up @@ -3603,6 +3607,26 @@ fn bindgen_test_layout_ndb_config() {
stringify!(ingest_filter)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).sub_cb_ctx) as usize - ptr as usize },
32usize,
concat!(
"Offset of field: ",
stringify!(ndb_config),
"::",
stringify!(sub_cb_ctx)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).sub_cb) as usize - ptr as usize },
40usize,
concat!(
"Offset of field: ",
stringify!(ndb_config),
"::",
stringify!(sub_cb)
)
);
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -4794,6 +4818,13 @@ extern "C" {
arg1: *mut ::std::os::raw::c_void,
);
}
extern "C" {
pub fn ndb_config_set_subscription_callback(
config: *mut ndb_config,
fn_: ndb_sub_fn,
ctx: *mut ::std::os::raw::c_void,
);
}
extern "C" {
pub fn ndb_calculate_id(
note: *mut ndb_note,
Expand Down Expand Up @@ -5097,7 +5128,10 @@ extern "C" {
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn ndb_unsubscribe(subid: ::std::os::raw::c_int) -> ::std::os::raw::c_int;
pub fn ndb_unsubscribe(arg1: *mut ndb, subid: u64) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn ndb_num_subscriptions(arg1: *mut ndb) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn ndb_text_search(
Expand Down Expand Up @@ -5171,6 +5205,14 @@ extern "C" {
extern "C" {
pub fn ndb_str_len(str_: *mut ndb_str) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " write the note as json to a buffer"]
pub fn ndb_note_json(
arg1: *mut ndb_note,
buf: *mut ::std::os::raw::c_char,
buflen: ::std::os::raw::c_int,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn ndb_tags_iterate_start(note: *mut ndb_note, iter: *mut ndb_iterator);
}
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ impl Config {
pub fn new() -> Self {
let mut config = bindings::ndb_config {
filter_context: std::ptr::null_mut(),
sub_cb: None,
sub_cb_ctx: std::ptr::null_mut(),
ingest_filter: None,
flags: 0,
ingester_threads: 0,
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum Error {
NoteProcessFailed,
TransactionFailed,
SubscriptionError,
BufferOverflow,
}

impl fmt::Display for Error {
Expand All @@ -21,6 +22,7 @@ impl fmt::Display for Error {
Error::NoteProcessFailed => "Note process failed",
Error::TransactionFailed => "Transaction failed",
Error::SubscriptionError => "Subscription failed",
Error::BufferOverflow => "Buffer overflow",
};
write!(f, "{}", s)
}
Expand Down
33 changes: 28 additions & 5 deletions src/note.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::bindings;
use crate::tags::Tags;
use crate::transaction::Transaction;
use crate::{bindings, Error};
use ::std::os::raw::c_uchar;
use std::hash::Hash;

Expand Down Expand Up @@ -127,11 +127,30 @@ impl<'a> Note<'a> {
}
}

/*
pub fn json() -> String {
unsafe { bindings::ndb_note_json() }
pub fn json_with_bufsize(&self, bufsize: usize) -> Result<String, Error> {
let mut buf = Vec::with_capacity(bufsize);
unsafe {
let size = bindings::ndb_note_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)
}
*/

fn content_size(&self) -> usize {
unsafe { bindings::ndb_note_content_length(self.as_ptr()) as usize }
Expand Down Expand Up @@ -506,5 +525,9 @@ mod tests {
assert_eq!(tag.get_unchecked(1).variant().str().unwrap(), "something");
break;
}

let json = note.json().expect("note json");
// the signature changes so 267 is everything up until the signature
assert_eq!(&json[..267], "{\"id\":\"fb165be22c7b2518b749aabb7140c73f0887fe84475c82785700663be85ba859\",\"pubkey\":\"6c540ed060bfc2b0c5b6f09cd3ebedf980ef7bc836d69582361d20f2ad124f23\",\"created_at\":42,\"kind\":1,\"tags\":[[\"comment\",\"this is a comment\"],[\"blah\",\"something\"]],\"content\":\"this is the content\"");
}
}
Loading