Struct kiwi_schema::Schema

source ·
pub struct Schema {
    pub defs: Vec<Def>,
    pub def_name_to_index: HashMap<String, usize>,
}
Expand description

Holds the contents of a Kiwi schema.

Each schema consists of a list of definitions, each of which has a list of fields. It can either be constructed in memory or decoded from a file in the binary Kiwi schema format.

Example usage:

// This is the encoding of the Kiwi schema "message ABC { int[] xyz = 1; }"
let schema_bytes = [1, 65, 66, 67, 0, 2, 1, 120, 121, 122, 0, 5, 1, 1];
let schema = kiwi_schema::Schema::decode(&schema_bytes).unwrap();

let def = schema.def("ABC").unwrap();
assert_eq!(def.kind, kiwi_schema::DefKind::Message);

let field = def.field("xyz").unwrap();
assert_eq!(field.type_id, kiwi_schema::TYPE_INT);
assert_eq!(field.is_array, true);
assert_eq!(field.value, 1);

Fields§

§defs: Vec<Def>§def_name_to_index: HashMap<String, usize>

Maps the name member of each Def in the defs array to its index in that array. This is helpful when decoding and encoding a field to be able to quickly get to the field metadata.

Implementations§

source§

impl Schema

source

pub fn new(defs: Vec<Def>) -> Schema

source

pub fn decode(bytes: &[u8]) -> Result<Schema, ()>

Parses a Kiwi schema encoded in the binary format and returns the parsed schema if successful. A textual schema can be compiled into a binary schema using the command-line tools:

kiwic --schema example.kiwi --binary example.bkiwi
source

pub fn encode(&self) -> Vec<u8>

The opposite of decode. Turns this schema back into a binary file.

source

pub fn def(&self, name: &str) -> Option<&Def>

Returns the Def with the provided name if one exists.

source

pub fn skip_with_options( &self, bb: &mut ByteBuffer<'_>, type_id: i32, options: &SchemaOptions ) -> Result<(), ()>

Advances the current index of the provided ByteBuffer by the size of a field with the provided type information. The Kiwi format doesn’t support seeking around to arbitrary points (it must be read from start to end) so this method is helpful when you need to to skip past unimportant fields.

source

pub fn skip(&self, bb: &mut ByteBuffer<'_>, type_id: i32) -> Result<(), ()>

source

pub fn skip_field_with_options( &self, bb: &mut ByteBuffer<'_>, field: &Field, options: &SchemaOptions ) -> Result<(), ()>

Advances the current index of the provided ByteBuffer by the size of the provided field. This is used by skip but may also be useful by itself.

source

pub fn skip_field( &self, bb: &mut ByteBuffer<'_>, field: &Field ) -> Result<(), ()>

Trait Implementations§

source§

impl Debug for Schema

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq<Schema> for Schema

source§

fn eq(&self, other: &Schema) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl StructuralPartialEq for Schema

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.