Enum kiwi_schema::Value
source · pub enum Value<'a> {
Bool(bool),
Byte(u8),
Int(i32),
UInt(u32),
Float(f32),
String(String),
Int64(i64),
UInt64(u64),
Array(Vec<Value<'a>>),
Enum(&'a str, &'a str),
Object(&'a str, HashMap<&'a str, Value<'a>>),
}
Expand description
This type holds dynamic Kiwi data.
Values can represent anything in a Kiwi schema and can be converted to and from byte arrays using the corresponding Schema. Enums and field names are stored using string slices from their Schema for efficiency. This means that a Value can outlive the buffer it was parsed from but can’t outlive the schema.
Variants§
Bool(bool)
Byte(u8)
Int(i32)
UInt(u32)
Float(f32)
String(String)
Int64(i64)
UInt64(u64)
Array(Vec<Value<'a>>)
Enum(&'a str, &'a str)
Object(&'a str, HashMap<&'a str, Value<'a>>)
Implementations§
source§impl<'a> Value<'a>
impl<'a> Value<'a>
sourcepub fn as_bool(&self) -> bool
pub fn as_bool(&self) -> bool
A convenience method to extract the value out of a Bool.
Returns false
for other value kinds.
sourcepub fn as_byte(&self) -> u8
pub fn as_byte(&self) -> u8
A convenience method to extract the value out of a Byte.
Returns 0
for other value kinds.
sourcepub fn as_int(&self) -> i32
pub fn as_int(&self) -> i32
A convenience method to extract the value out of an Int.
Returns 0
for other value kinds.
sourcepub fn as_uint(&self) -> u32
pub fn as_uint(&self) -> u32
A convenience method to extract the value out of a UInt.
Returns 0
for other value kinds.
sourcepub fn as_float(&self) -> f32
pub fn as_float(&self) -> f32
A convenience method to extract the value out of a Float.
Returns 0.0
for other value kinds.
sourcepub fn as_string(&self) -> &str
pub fn as_string(&self) -> &str
A convenience method to extract the value out of a String.
Returns ""
for other value kinds.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
A convenience method to extract the length out of an Array.
Returns 0
for other value kinds.
sourcepub fn push(&mut self, value: Value<'a>)
pub fn push(&mut self, value: Value<'a>)
A convenience method to append to an Array. Does nothing for other value kinds.
sourcepub fn get(&self, name: &str) -> Option<&Value<'a>>
pub fn get(&self, name: &str) -> Option<&Value<'a>>
A convenience method to extract a field out of an Object.
Returns None
for other value kinds or if the field isn’t present.
sourcepub fn set(&mut self, name: &'a str, value: Value<'a>)
pub fn set(&mut self, name: &'a str, value: Value<'a>)
A convenience method to update a field on an Object. Does nothing for other value kinds.
sourcepub fn remove(&mut self, name: &'a str)
pub fn remove(&mut self, name: &'a str)
A convenience method to remove a field on an Object. Does nothing for other value kinds.
sourcepub fn decode(
schema: &'a Schema,
type_id: i32,
bytes: &[u8]
) -> Result<Value<'a>, ()>
pub fn decode( schema: &'a Schema, type_id: i32, bytes: &[u8] ) -> Result<Value<'a>, ()>
Decodes the type specified by type_id
and schema
from bytes
.
sourcepub fn encode(&self, schema: &Schema) -> Vec<u8>
pub fn encode(&self, schema: &Schema) -> Vec<u8>
Encodes this value into an array of bytes using the provided schema
.
sourcepub fn decode_bb(
schema: &'a Schema,
type_id: i32,
bb: &mut ByteBuffer<'_>
) -> Result<Value<'a>, ()>
pub fn decode_bb( schema: &'a Schema, type_id: i32, bb: &mut ByteBuffer<'_> ) -> Result<Value<'a>, ()>
Decodes the type specified by type_id
and schema
from bb
starting
at the current index. After this function returns, the current index will
be advanced by the amount of data that was successfully parsed. This is
mainly useful as a helper routine for decode, which you
probably want to use instead.
sourcepub fn decode_field_bb(
schema: &'a Schema,
field: &Field,
bb: &mut ByteBuffer<'_>
) -> Result<Value<'a>, ()>
pub fn decode_field_bb( schema: &'a Schema, field: &Field, bb: &mut ByteBuffer<'_> ) -> Result<Value<'a>, ()>
Decodes the field specified by field
and schema
from bb
starting
at the current index. This is used by decode_bb but
may also be useful by itself.
sourcepub fn encode_bb(&self, schema: &Schema, bb: &mut ByteBufferMut)
pub fn encode_bb(&self, schema: &Schema, bb: &mut ByteBufferMut)
Encodes the current value to the end of bb
using the provided schema
.
This is mainly useful as a helper routine for encode,
which you probably want to use instead.