pub enum Value {
}
Expand description
Values
Usage
Converting data into Value
This is straightforward. You can make this enum directly, or via implementations of From
,
FromIterator
…
Extracting data from Value
There are several options:
- By using the good old
match
. - Or implementations of
TryFrom
. - Or via shortcut functions.
For numbers, if your have a strict specification of your data, it’s better to use match
. Otherwise, you can use
TryFrom
implementations.
Variants§
Null
True
False
U8(u8)
I8(i8)
U16(u16)
I16(i16)
U32(u32)
I32(i32)
U64(u64)
I64(i64)
Float(f32)
Double(f64)
Text(String)
DateTime(String)
Date(String)
Time(String)
DecimalStr(String)
Blob(Blob)
List(List)
Map(Map)
Object(Object)
Implementations§
source§impl Value
impl Value
sourcepub fn as_blob(&self) -> Result<&[u8]>
pub fn as_blob(&self) -> Result<&[u8]>
If the value is a blob, returns an immutable reference of it
Returns an error if the value is not a blob.
sourcepub fn as_mut_blob(&mut self) -> Result<&mut Blob>
pub fn as_mut_blob(&mut self) -> Result<&mut Blob>
If the value is a blob, returns a mutable reference of it
Returns an error if the value is not a blob.
source§impl Value
impl Value
sourcepub fn push<T>(&mut self, value: T) -> Result<()>where
T: Into<Self>,
pub fn push<T>(&mut self, value: T) -> Result<()>where T: Into<Self>,
If the value is a list, pushes new item into it
Returns an error if the value is not a list.
sourcepub fn at(&self, indexes: &[usize]) -> Result<&Self>
pub fn at(&self, indexes: &[usize]) -> Result<&Self>
Gets an immutable item from this list and its sub lists
The function returns an error on one of these conditions:
- Indexes are empty or invalid.
- The value or any of its sub items is not a list.
Examples
use binn_ir::Value;
let mut list = binn_ir::list();
list.push("first")?;
list.push(vec![Value::False, "second".into()])?;
assert_eq!(list.at(&[0])?.as_text()?, "first");
assert_eq!(list.at(&[1, 1])?.as_text()?, "second");
assert!(list.at(&[]).is_err());
assert!(list.at(&[0, 1]).is_err());
assert!(list.at(&[1, 2]).is_err());
assert!(list.at(&[1, 0, 0]).is_err());
assert!(list.at(&[1, 1, 2]).is_err());
sourcepub fn mut_at(&mut self, indexes: &[usize]) -> Result<&mut Self>
pub fn mut_at(&mut self, indexes: &[usize]) -> Result<&mut Self>
Gets a mutable item from this array and its sub arrays
The function returns an error on one of these conditions:
- Indexes are empty or invalid.
- The value or any of its sub items is not an array.
sourcepub fn take_at(&mut self, indexes: &[usize]) -> Result<Self>
pub fn take_at(&mut self, indexes: &[usize]) -> Result<Self>
Takes an item from this list and its sub lists
The function returns an error on one of these conditions:
- Indexes are empty or invalid.
- The value or any of its sub items is not a list.
Examples
use binn_ir::Value;
let mut list = binn_ir::list();
list.push("earth")?;
list.push(vec![Value::False, "moon".into()])?;
assert_eq!(list.take_at(&[0])?.as_text()?, "earth");
assert_eq!(list.take_at(&[0, 1])?.as_text()?, "moon");
assert!(list.take_at(&[]).is_err());
assert!(list.take_at(&[0, 1]).is_err());
sourcepub fn as_list(&self) -> Result<&List>
pub fn as_list(&self) -> Result<&List>
If the value is a list, returns an immutable reference of it
Returns an error if the value is not a list.
sourcepub fn as_mut_list(&mut self) -> Result<&mut List>
pub fn as_mut_list(&mut self) -> Result<&mut List>
If the value is a list, returns a mutable reference of it
Returns an error if the value is not a list.
source§impl Value
impl Value
sourcepub fn map_insert<K, V>(&mut self, key: K, value: V) -> Result<Option<Self>>where
K: Into<MapKey>,
V: Into<Self>,
pub fn map_insert<K, V>(&mut self, key: K, value: V) -> Result<Option<Self>>where K: Into<MapKey>, V: Into<Self>,
If the value is a map, inserts new item into it
On success, returns previous value (if it existed).
Returns an error if the value is not a map.
sourcepub fn map_by(&self, keys: &[MapKey]) -> Result<&Self>
pub fn map_by(&self, keys: &[MapKey]) -> Result<&Self>
Gets an immutable item from this map and its sub maps
The function returns an error on one of these conditions:
- Keys are empty.
- The value or any of its sub items is not a map.
Examples
let mut map = binn_ir::map();
map.map_insert(0, true)?;
map.map_insert(1, {
let mut map = binn_ir::Map::new();
binn_ir::map_insert(&mut map, 2, 99);
map
})?;
assert_eq!(bool::try_from(map.map_by(&[0])?)?, true);
assert_eq!(u8::try_from(map.map_by(&[1, 2])?)?, 99);
assert!(map.map_by(&[2]).is_err());
assert!(map.map_maybe_by(&[2])?.is_none());
assert!(map.map_by(&[]).is_err());
assert!(map.map_by(&[0, 2]).is_err());
assert!(map.map_by(&[1, 2, 3]).is_err());
sourcepub fn map_maybe_by(&self, keys: &[MapKey]) -> Result<Option<&Self>>
pub fn map_maybe_by(&self, keys: &[MapKey]) -> Result<Option<&Self>>
Gets an optional immutable item from this map and its sub maps
The function returns an error on one of these conditions:
- Keys are empty.
- The value or any of its sub items is not a map.
sourcepub fn map_mut_by(&mut self, keys: &[MapKey]) -> Result<&mut Self>
pub fn map_mut_by(&mut self, keys: &[MapKey]) -> Result<&mut Self>
Gets a mutable item from this map and its sub maps
The function returns an error on one of these conditions:
- Keys are empty.
- The value or any of its sub items is not a map.
sourcepub fn map_maybe_mut_by(&mut self, keys: &[MapKey]) -> Result<Option<&mut Self>>
pub fn map_maybe_mut_by(&mut self, keys: &[MapKey]) -> Result<Option<&mut Self>>
Gets an optional mutable item from this map and its sub maps
The function returns an error on one of these conditions:
- Keys are empty.
- The value or any of its sub items is not a map.
sourcepub fn map_take_by(&mut self, keys: &[MapKey]) -> Result<Self>
pub fn map_take_by(&mut self, keys: &[MapKey]) -> Result<Self>
Takes an item from this map and its sub maps
The function returns an error on one of these conditions:
- Keys are empty.
- The value or any of its sub items is not a map.
Examples
let mut map = binn_ir::map();
map.map_insert(0, "zero")?;
map.map_insert(1, {
let mut map = binn_ir::Map::new();
binn_ir::map_insert(&mut map, 2, "two");
map
})?;
assert_eq!(map.map_take_by(&[0])?.as_text()?, "zero");
assert_eq!(map.map_take_by(&[1, 2])?.as_text()?, "two");
assert!(map.map_take_by(&[0]).is_err());
assert!(map.map_maybe_take_by(&[0])?.is_none());
assert!(map.map_maybe_take_by(&[1, 2])?.is_none());
assert!(map.map_take_by(&[]).is_err());
assert!(map.map_take_by(&[3, 4]).is_err());
sourcepub fn map_maybe_take_by(&mut self, keys: &[MapKey]) -> Result<Option<Self>>
pub fn map_maybe_take_by(&mut self, keys: &[MapKey]) -> Result<Option<Self>>
Takes an optional item from this map and its sub maps
The function returns an error on one of these conditions:
- Keys are empty.
- The value or any of its sub items is not a map.
sourcepub fn as_map(&self) -> Result<&Map>
pub fn as_map(&self) -> Result<&Map>
If the value is a map, returns an immutable reference of it
Returns an error if the value is not a map.
sourcepub fn as_mut_map(&mut self) -> Result<&mut Map>
pub fn as_mut_map(&mut self) -> Result<&mut Map>
If the value is a map, returns a mutable reference of it
Returns an error if the value is not a map.
source§impl Value
impl Value
sourcepub fn try_into_or<T>(self, default: T) -> Result<T>where
T: TryFrom<Self, Error = Error>,
pub fn try_into_or<T>(self, default: T) -> Result<T>where T: TryFrom<Self, Error = Error>,
Tries to convert this value into something
If this is Null
, returns default
.
If your default value would be a result of a function call, you should use try_into_or_else()
.
Examples
assert!(binn_ir::Value::Null.try_into_or(true)?);
sourcepub fn try_into_or_else<T, F>(self, default: F) -> Result<T>where
T: TryFrom<Self, Error = Error>,
F: FnOnce() -> T,
pub fn try_into_or_else<T, F>(self, default: F) -> Result<T>where T: TryFrom<Self, Error = Error>, F: FnOnce() -> T,
Tries to convert this value into something
If this is Null
, calls default()
and returns its result.
sourcepub fn try_ref_into_or<'a, T>(&'a self, default: T) -> Result<T>where
T: TryFrom<&'a Self, Error = Error>,
pub fn try_ref_into_or<'a, T>(&'a self, default: T) -> Result<T>where T: TryFrom<&'a Self, Error = Error>,
Tries to convert a reference of this value into something
If this is Null
, returns default
.
If your default value would be a result of a function call, you should use try_ref_into_or_else()
.
Examples
assert_eq!(binn_ir::Value::Null.try_ref_into_or(0)?, 0);
sourcepub fn try_ref_into_or_else<'a, T, F>(&'a self, default: F) -> Result<T>where
T: TryFrom<&'a Self, Error = Error>,
F: FnOnce() -> T,
pub fn try_ref_into_or_else<'a, T, F>(&'a self, default: F) -> Result<T>where T: TryFrom<&'a Self, Error = Error>, F: FnOnce() -> T,
Tries to convert a reference of this value into something
If this is Null
, calls default()
and returns its result.
source§impl Value
impl Value
sourcepub fn object_insert<K, V>(&mut self, key: K, value: V) -> Result<Option<Value>>where
K: Into<ObjectKey>,
V: Into<Self>,
pub fn object_insert<K, V>(&mut self, key: K, value: V) -> Result<Option<Value>>where K: Into<ObjectKey>, V: Into<Self>,
If the value is an object, inserts new item into it
On success, returns previous value (if it existed).
Returns an error if the value is not an object.
sourcepub fn object_by(&self, keys: &[&str]) -> Result<&Self>
pub fn object_by(&self, keys: &[&str]) -> Result<&Self>
Gets an immutable item from this object and its sub objects
The function returns an error on one of these conditions:
- Keys are empty.
- The value or any of its sub items is not an object.
Examples
let mut object = binn_ir::object();
object.object_insert("zero", true)?;
object.object_insert("one", {
let mut object = binn_ir::Object::new();
binn_ir::object_insert(&mut object, "two", 99);
object
})?;
assert_eq!(bool::try_from(object.object_by(&["zero"])?)?, true);
assert_eq!(u8::try_from(object.object_by(&["one", "two"])?)?, 99);
assert!(object.object_by(&["two"]).is_err());
assert!(object.object_maybe_by(&["two"])?.is_none());
assert!(object.object_by(&[]).is_err());
assert!(object.object_by(&["zero", "two"]).is_err());
assert!(object.object_by(&["one", "two", "three"]).is_err());
sourcepub fn object_maybe_by(&self, keys: &[&str]) -> Result<Option<&Self>>
pub fn object_maybe_by(&self, keys: &[&str]) -> Result<Option<&Self>>
Gets an optional immutable item from this object and its sub objects
The function returns an error on one of these conditions:
- Keys are empty.
- The value or any of its sub items is not an object.
sourcepub fn object_mut_by(&mut self, keys: &[&str]) -> Result<&mut Self>
pub fn object_mut_by(&mut self, keys: &[&str]) -> Result<&mut Self>
Gets a mutable item from this object and its sub objects
The function returns an error on one of these conditions:
- Keys are empty.
- The value or any of its sub items is not an object.
sourcepub fn object_maybe_mut_by(&mut self, keys: &[&str]) -> Result<Option<&mut Self>>
pub fn object_maybe_mut_by(&mut self, keys: &[&str]) -> Result<Option<&mut Self>>
Gets an optional mutable item from this object and its sub objects
The function returns an error on one of these conditions:
- Keys are empty.
- The value or any of its sub items is not an object.
sourcepub fn object_take_by(&mut self, keys: &[&str]) -> Result<Self>
pub fn object_take_by(&mut self, keys: &[&str]) -> Result<Self>
Takes an item from this object and its sub objects
The function returns an error on one of these conditions:
- Keys are empty.
- The value or any of its sub items is not an object.
Examples
let mut object = binn_ir::object();
object.object_insert("first", "1st")?;
object.object_insert("second", {
let mut object = binn_ir::Object::new();
binn_ir::object_insert(&mut object, "third", "3rd");
object
})?;
assert_eq!(object.object_take_by(&["first"])?.as_text()?, "1st");
assert_eq!(object.object_take_by(&["second", "third"])?.as_text()?, "3rd");
assert!(object.object_take_by(&["zero"]).is_err());
assert!(object.object_maybe_take_by(&["zero"])?.is_none());
assert!(object.object_maybe_take_by(&["second", "fourth"])?.is_none());
assert!(object.object_take_by(&[]).is_err());
assert!(object.object_take_by(&["third", "fourth"]).is_err());
sourcepub fn object_maybe_take_by(&mut self, keys: &[&str]) -> Result<Option<Self>>
pub fn object_maybe_take_by(&mut self, keys: &[&str]) -> Result<Option<Self>>
Takes an optional item from this object and its sub objects
The function returns an error on one of these conditions:
- Keys are empty.
- The value or any of its sub items is not an object.
sourcepub fn as_object(&self) -> Result<&Object>
pub fn as_object(&self) -> Result<&Object>
If the value is an object, returns an immutable reference of it
Returns an error if the value is not an object.
sourcepub fn as_mut_object(&mut self) -> Result<&mut Object>
pub fn as_mut_object(&mut self) -> Result<&mut Object>
If the value is an object, returns a mutable reference of it
Returns an error if the value is not an object.
source§impl Value
impl Value
sourcepub fn as_text(&self) -> Result<&str>
pub fn as_text(&self) -> Result<&str>
If the value is a Text
, returns an immutable reference of it
Returns an error if the value is not a Text
.
sourcepub fn as_date_time(&self) -> Result<&str>
pub fn as_date_time(&self) -> Result<&str>
If the value is a DateTime
, returns an immutable reference of it
Returns an error if the value is not a DateTime
.
sourcepub fn as_date(&self) -> Result<&str>
pub fn as_date(&self) -> Result<&str>
If the value is a Date
, returns an immutable reference of it
Returns an error if the value is not a Date
.
sourcepub fn as_time(&self) -> Result<&str>
pub fn as_time(&self) -> Result<&str>
If the value is a Time
, returns an immutable reference of it
Returns an error if the value is not a Time
.
sourcepub fn as_decimal_str(&self) -> Result<&str>
pub fn as_decimal_str(&self) -> Result<&str>
If the value is a DecimalStr
, returns an immutable reference of it
Returns an error if the value is not a DecimalStr
.