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
impl Schema
pub fn new(defs: Vec<Def>) -> Schema
sourcepub fn decode(bytes: &[u8]) -> Result<Schema, ()>
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
sourcepub fn encode(&self) -> Vec<u8>
pub fn encode(&self) -> Vec<u8>
The opposite of decode. Turns this schema back into a binary file.
sourcepub fn def(&self, name: &str) -> Option<&Def>
pub fn def(&self, name: &str) -> Option<&Def>
Returns the Def with the provided name if one exists.
sourcepub fn skip_with_options(
&self,
bb: &mut ByteBuffer<'_>,
type_id: i32,
options: &SchemaOptions
) -> Result<(), ()>
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.
pub fn skip(&self, bb: &mut ByteBuffer<'_>, type_id: i32) -> Result<(), ()>
sourcepub fn skip_field_with_options(
&self,
bb: &mut ByteBuffer<'_>,
field: &Field,
options: &SchemaOptions
) -> Result<(), ()>
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.