From ce2f5dbc3e289c65860982505b28adff053b49d3 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Mon, 20 Jan 2025 13:54:48 -0800 Subject: [PATCH nostrdb-rs] tags: add porcelain api for getting ids and strings It was a bit verbose before Suggested-by: kieran Signed-off-by: William Casarin --- src/ndb_str.rs | 8 ++++++++ src/tags.rs | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/ndb_str.rs b/src/ndb_str.rs index 9624d3ae88ac..3c7bd5508483 100644 --- a/src/ndb_str.rs +++ b/src/ndb_str.rs @@ -58,6 +58,14 @@ impl<'a> NdbStr<'a> { } } + pub fn id(&self) -> Option<&'a [u8; 32]> { + self.variant().id() + } + + pub fn str(&self) -> Option<&'a str> { + self.variant().str() + } + pub fn variant(&self) -> NdbStrVariant<'a> { if self.ndb_str.flag == (bindings::NDB_PACKED_ID as u8) { unsafe { NdbStrVariant::Id(&*(self.ndb_str.id() as *const [u8; 32])) } diff --git a/src/tags.rs b/src/tags.rs index 93caf5082da2..ba6ec8a09f1a 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -26,6 +26,14 @@ impl<'n> Tag<'n> { NdbStr::new(nstr, self.note.clone()) } + pub fn get_str(&self, ind: u16) -> Option<&'n str> { + self.get(ind).and_then(|s| s.str()) + } + + pub fn get_id(&self, ind: u16) -> Option<&'n [u8; 32]> { + self.get(ind).and_then(|s| s.id()) + } + pub fn get(&self, ind: u16) -> Option> { if ind >= self.count() { return None; -- 2.47.0