Skip to content

Commit

Permalink
database: impl FlatBufferDecodeBorrowed for EventBorrow
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Jan 4, 2025
1 parent b0a9525 commit b356591
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* nostr: add `CowTag` ([Yuki Kishimoto])
* nostr: add `EventBorrow` ([Yuki Kishimoto])
* database: add `Events::first_owned` and `Events::last_owned` ([Yuki Kishimoto])
* database: impl `FlatBufferDecodeBorrowed` for `EventBorrow` ([Yuki Kishimoto])
* pool: add `Relay::try_connect` ([Yuki Kishimoto])
* pool: add `Relay::wait_for_connection` ([Yuki Kishimoto])
* pool: add `RelayPool::try_connect` ([Yuki Kishimoto])
Expand Down
24 changes: 24 additions & 0 deletions crates/nostr-database/src/flatbuffers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

//! Nostr Database Flatbuffers
use std::borrow::Cow;
use std::collections::HashSet;
use std::fmt;

Expand Down Expand Up @@ -145,6 +146,29 @@ impl FlatBufferDecode for Event {
}
}

impl<'a> FlatBufferDecodeBorrowed<'a> for EventBorrow<'a> {
fn decode(buf: &'a [u8]) -> Result<Self, Error> {
let ev = event_fbs::root_as_event(buf)?;

let fb_tags = ev.tags().ok_or(Error::NotFound)?;
let mut tags = Vec::with_capacity(fb_tags.len());

for tag in fb_tags.iter().filter_map(|t| t.data()) {
tags.push(CowTag::parse(tag.into_iter().map(Cow::Borrowed).collect())?);
}

Ok(Self {
id: &ev.id().ok_or(Error::NotFound)?.0,
pubkey: &ev.pubkey().ok_or(Error::NotFound)?.0,
created_at: Timestamp::from_secs(ev.created_at()),
kind: ev.kind() as u16, // TODO: should use try_into
tags,
content: ev.content().ok_or(Error::NotFound)?,
sig: &ev.sig().ok_or(Error::NotFound)?.0,
})
}
}

impl FlatBufferEncode for HashSet<RelayUrl> {
fn encode<'a>(&self, fbb: &'a mut FlatBufferBuilder) -> &'a [u8] {
fbb.reset();
Expand Down

0 comments on commit b356591

Please sign in to comment.