Skip to content

Commit

Permalink
nip10: add initial thread reply utils
Browse files Browse the repository at this point in the history
These are some util helpers for nip10. It supports deprecated and marker
variants.

Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Apr 27, 2024
1 parent ee256e8 commit 8092af6
Show file tree
Hide file tree
Showing 7 changed files with 404 additions and 9 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

tags: fake
find src *.rs | xargs ctags

find src -name '*.rs' | xargs ctags

.PHONY: fake
2 changes: 1 addition & 1 deletion nostrdb
Submodule nostrdb updated 5 files
+1 −1 .envrc
+39 −4 ndb.c
+42 −14 src/nostrdb.c
+2 −1 src/nostrdb.h
+5 −1 test.c
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod result;
mod subscription;
mod tags;
mod transaction;
mod util;

pub use block::{Block, BlockType, Blocks, Mention};
pub use config::Config;
Expand All @@ -36,5 +37,6 @@ pub use result::Result;
pub use subscription::Subscription;
pub use tags::{Tag, TagIter, Tags, TagsIter};
pub use transaction::Transaction;
pub use util::nip10::{Marker, NoteIdRef, NoteReply};

mod test_util;
16 changes: 16 additions & 0 deletions src/ndb_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ pub enum NdbStrVariant<'a> {
Str(&'a str),
}

impl<'a> NdbStrVariant<'a> {
pub fn id(&self) -> Option<&'a [u8; 32]> {
match self {
Self::Id(id) => Some(id),
_ => None,
}
}

pub fn str(&self) -> Option<&'a str> {
match self {
Self::Str(s) => Some(s),
_ => None,
}
}
}

impl bindings::ndb_str {
pub fn str(&self) -> *const ::std::os::raw::c_char {
unsafe { self.__bindgen_anon_1.str_ }
Expand Down
15 changes: 9 additions & 6 deletions src/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ impl<'a> Tag<'a> {
unsafe { bindings::ndb_tag_count(self.as_ptr()) }
}

pub fn get(&self, ind: u16) -> Option<NdbStr<'a>> {
if ind >= self.count() {
return None;
}
pub fn get_unchecked(&self, ind: u16) -> NdbStr<'a> {
let nstr = unsafe {
bindings::ndb_tag_str(
self.note().as_ptr(),
self.as_ptr(),
ind as ::std::os::raw::c_int,
)
};
Some(NdbStr::new(nstr, self.note))
NdbStr::new(nstr, self.note)
}

pub fn get(&self, ind: u16) -> Option<NdbStr<'a>> {
if ind >= self.count() {
return None;
}
Some(self.get_unchecked(ind))
}

pub fn note(&self) -> &'a Note<'a> {
Expand Down Expand Up @@ -172,7 +176,6 @@ impl<'a> Iterator for TagsIter<'a> {

#[cfg(test)]
mod tests {
use super::*;
use crate::config::Config;
use crate::test_util;
use crate::{Filter, Ndb, NdbStrVariant, Transaction};
Expand Down
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod nip10;
Loading

0 comments on commit 8092af6

Please sign in to comment.