Skip to content

Commit

Permalink
fbs: add index schema
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Oct 27, 2023
1 parent 35a719a commit 8602437
Show file tree
Hide file tree
Showing 4 changed files with 342 additions and 9 deletions.
3 changes: 2 additions & 1 deletion crates/nostr-sdk-fbs/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
all: build

build:
flatc --rust -o ./src/ ./fbs/event.fbs
flatc --rust -o ./src/ ./fbs/event.fbs
flatc --rust -o ./src/ ./fbs/index.fbs
11 changes: 11 additions & 0 deletions crates/nostr-sdk-fbs/fbs/index.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace IndexFbs;

struct Fixed32Bytes {
val: [ubyte:32];
}

table IndexSet {
data: [Fixed32Bytes];
}

root_type IndexSet;
288 changes: 288 additions & 0 deletions crates/nostr-sdk-fbs/src/index_generated.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
// automatically generated by the FlatBuffers compiler, do not modify

// @generated

use core::cmp::Ordering;
use core::mem;

extern crate flatbuffers;
use self::flatbuffers::{EndianScalar, Follow};

#[allow(unused_imports, dead_code)]
pub mod index_fbs {

use core::cmp::Ordering;
use core::mem;

extern crate flatbuffers;
use self::flatbuffers::{EndianScalar, Follow};

// struct Fixed32Bytes, aligned to 1
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq)]
pub struct Fixed32Bytes(pub [u8; 32]);
impl Default for Fixed32Bytes {
fn default() -> Self {
Self([0; 32])
}
}
impl core::fmt::Debug for Fixed32Bytes {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.debug_struct("Fixed32Bytes")
.field("val", &self.val())
.finish()
}
}

impl flatbuffers::SimpleToVerifyInSlice for Fixed32Bytes {}
impl<'a> flatbuffers::Follow<'a> for Fixed32Bytes {
type Inner = &'a Fixed32Bytes;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
<&'a Fixed32Bytes>::follow(buf, loc)
}
}
impl<'a> flatbuffers::Follow<'a> for &'a Fixed32Bytes {
type Inner = &'a Fixed32Bytes;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
flatbuffers::follow_cast_ref::<Fixed32Bytes>(buf, loc)
}
}
impl<'b> flatbuffers::Push for Fixed32Bytes {
type Output = Fixed32Bytes;
#[inline]
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
let src = ::core::slice::from_raw_parts(
self as *const Fixed32Bytes as *const u8,
Self::size(),
);
dst.copy_from_slice(src);
}
}

impl<'a> flatbuffers::Verifiable for Fixed32Bytes {
#[inline]
fn run_verifier(
v: &mut flatbuffers::Verifier,
pos: usize,
) -> Result<(), flatbuffers::InvalidFlatbuffer> {
use self::flatbuffers::Verifiable;
v.in_buffer::<Self>(pos)
}
}

impl<'a> Fixed32Bytes {
#[allow(clippy::too_many_arguments)]
pub fn new(val: &[u8; 32]) -> Self {
let mut s = Self([0; 32]);
s.set_val(val);
s
}

pub fn val(&'a self) -> flatbuffers::Array<'a, u8, 32> {
// Safety:
// Created from a valid Table for this object
// Which contains a valid array in this slot
unsafe { flatbuffers::Array::follow(&self.0, 0) }
}

pub fn set_val(&mut self, items: &[u8; 32]) {
// Safety:
// Created from a valid Table for this object
// Which contains a valid array in this slot
unsafe { flatbuffers::emplace_scalar_array(&mut self.0, 0, items) };
}
}

pub enum IndexSetOffset {}
#[derive(Copy, Clone, PartialEq)]

pub struct IndexSet<'a> {
pub _tab: flatbuffers::Table<'a>,
}

impl<'a> flatbuffers::Follow<'a> for IndexSet<'a> {
type Inner = IndexSet<'a>;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
Self {
_tab: flatbuffers::Table::new(buf, loc),
}
}
}

impl<'a> IndexSet<'a> {
pub const VT_DATA: flatbuffers::VOffsetT = 4;

#[inline]
pub unsafe fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
IndexSet { _tab: table }
}
#[allow(unused_mut)]
pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>(
_fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>,
args: &'args IndexSetArgs<'args>,
) -> flatbuffers::WIPOffset<IndexSet<'bldr>> {
let mut builder = IndexSetBuilder::new(_fbb);
if let Some(x) = args.data {
builder.add_data(x);
}
builder.finish()
}

#[inline]
pub fn data(&self) -> Option<flatbuffers::Vector<'a, Fixed32Bytes>> {
// Safety:
// Created from valid Table for this object
// which contains a valid value in this slot
unsafe {
self._tab
.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'a, Fixed32Bytes>>>(
IndexSet::VT_DATA,
None,
)
}
}
}

impl flatbuffers::Verifiable for IndexSet<'_> {
#[inline]
fn run_verifier(
v: &mut flatbuffers::Verifier,
pos: usize,
) -> Result<(), flatbuffers::InvalidFlatbuffer> {
use self::flatbuffers::Verifiable;
v.visit_table(pos)?
.visit_field::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'_, Fixed32Bytes>>>(
"data",
Self::VT_DATA,
false,
)?
.finish();
Ok(())
}
}
pub struct IndexSetArgs<'a> {
pub data: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a, Fixed32Bytes>>>,
}
impl<'a> Default for IndexSetArgs<'a> {
#[inline]
fn default() -> Self {
IndexSetArgs { data: None }
}
}

pub struct IndexSetBuilder<'a: 'b, 'b> {
fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>,
start_: flatbuffers::WIPOffset<flatbuffers::TableUnfinishedWIPOffset>,
}
impl<'a: 'b, 'b> IndexSetBuilder<'a, 'b> {
#[inline]
pub fn add_data(
&mut self,
data: flatbuffers::WIPOffset<flatbuffers::Vector<'b, Fixed32Bytes>>,
) {
self.fbb_
.push_slot_always::<flatbuffers::WIPOffset<_>>(IndexSet::VT_DATA, data);
}
#[inline]
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> IndexSetBuilder<'a, 'b> {
let start = _fbb.start_table();
IndexSetBuilder {
fbb_: _fbb,
start_: start,
}
}
#[inline]
pub fn finish(self) -> flatbuffers::WIPOffset<IndexSet<'a>> {
let o = self.fbb_.end_table(self.start_);
flatbuffers::WIPOffset::new(o.value())
}
}

impl core::fmt::Debug for IndexSet<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut ds = f.debug_struct("IndexSet");
ds.field("data", &self.data());
ds.finish()
}
}
#[inline]
/// Verifies that a buffer of bytes contains a `IndexSet`
/// and returns it.
/// Note that verification is still experimental and may not
/// catch every error, or be maximally performant. For the
/// previous, unchecked, behavior use
/// `root_as_index_set_unchecked`.
pub fn root_as_index_set(buf: &[u8]) -> Result<IndexSet, flatbuffers::InvalidFlatbuffer> {
flatbuffers::root::<IndexSet>(buf)
}
#[inline]
/// Verifies that a buffer of bytes contains a size prefixed
/// `IndexSet` and returns it.
/// Note that verification is still experimental and may not
/// catch every error, or be maximally performant. For the
/// previous, unchecked, behavior use
/// `size_prefixed_root_as_index_set_unchecked`.
pub fn size_prefixed_root_as_index_set(
buf: &[u8],
) -> Result<IndexSet, flatbuffers::InvalidFlatbuffer> {
flatbuffers::size_prefixed_root::<IndexSet>(buf)
}
#[inline]
/// Verifies, with the given options, that a buffer of bytes
/// contains a `IndexSet` and returns it.
/// Note that verification is still experimental and may not
/// catch every error, or be maximally performant. For the
/// previous, unchecked, behavior use
/// `root_as_index_set_unchecked`.
pub fn root_as_index_set_with_opts<'b, 'o>(
opts: &'o flatbuffers::VerifierOptions,
buf: &'b [u8],
) -> Result<IndexSet<'b>, flatbuffers::InvalidFlatbuffer> {
flatbuffers::root_with_opts::<IndexSet<'b>>(opts, buf)
}
#[inline]
/// Verifies, with the given verifier options, that a buffer of
/// bytes contains a size prefixed `IndexSet` and returns
/// it. Note that verification is still experimental and may not
/// catch every error, or be maximally performant. For the
/// previous, unchecked, behavior use
/// `root_as_index_set_unchecked`.
pub fn size_prefixed_root_as_index_set_with_opts<'b, 'o>(
opts: &'o flatbuffers::VerifierOptions,
buf: &'b [u8],
) -> Result<IndexSet<'b>, flatbuffers::InvalidFlatbuffer> {
flatbuffers::size_prefixed_root_with_opts::<IndexSet<'b>>(opts, buf)
}
#[inline]
/// Assumes, without verification, that a buffer of bytes contains a IndexSet and returns it.
/// # Safety
/// Callers must trust the given bytes do indeed contain a valid `IndexSet`.
pub unsafe fn root_as_index_set_unchecked(buf: &[u8]) -> IndexSet {
flatbuffers::root_unchecked::<IndexSet>(buf)
}
#[inline]
/// Assumes, without verification, that a buffer of bytes contains a size prefixed IndexSet and returns it.
/// # Safety
/// Callers must trust the given bytes do indeed contain a valid size prefixed `IndexSet`.
pub unsafe fn size_prefixed_root_as_index_set_unchecked(buf: &[u8]) -> IndexSet {
flatbuffers::size_prefixed_root_unchecked::<IndexSet>(buf)
}
#[inline]
pub fn finish_index_set_buffer<'a, 'b>(
fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>,
root: flatbuffers::WIPOffset<IndexSet<'a>>,
) {
fbb.finish(root, None);
}

#[inline]
pub fn finish_size_prefixed_index_set_buffer<'a, 'b>(
fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>,
root: flatbuffers::WIPOffset<IndexSet<'a>>,
) {
fbb.finish_size_prefixed(root, None);
}
} // pub mod IndexFbs
Loading

0 comments on commit 8602437

Please sign in to comment.