pub enum Value {
Null,
Bool(bool),
Int(i64),
Float(f64),
Str(String),
Bytes(Vec<u8>),
Array(Vec<Value>),
Object(Document),
}Expand description
A single field value inside a Document.
Value is deliberately small and closed: it models the shapes a schemaless
document store needs and nothing more. Nesting is expressed through
Value::Array and Value::Object, so an arbitrarily deep record is
just a tree of Values.
§Examples
use bison_db::Value;
let v = Value::from("hello");
assert_eq!(v.as_str(), Some("hello"));
assert!(Value::from(42_i64).as_int() == Some(42));
assert!(Value::Null.is_null());Variants§
Null
The absence of a value.
Bool(bool)
A boolean.
Int(i64)
A signed 64-bit integer. All integral fields are stored at this width.
Float(f64)
A 64-bit IEEE-754 float.
Str(String)
A UTF-8 string.
Bytes(Vec<u8>)
An opaque byte string, for binary fields that are not valid UTF-8.
Array(Vec<Value>)
An ordered list of values.
Object(Document)
A nested document.
Implementations§
Source§impl Value
impl Value
Sourcepub const fn is_null(&self) -> bool
pub const fn is_null(&self) -> bool
Returns true if this value is Value::Null.
§Examples
use bison_db::Value;
assert!(Value::Null.is_null());
assert!(!Value::from(0_i64).is_null());Sourcepub const fn as_bool(&self) -> Option<bool>
pub const fn as_bool(&self) -> Option<bool>
Returns the boolean if this is a Value::Bool, otherwise None.
§Examples
use bison_db::Value;
assert_eq!(Value::from(true).as_bool(), Some(true));
assert_eq!(Value::from(1_i64).as_bool(), None);Sourcepub const fn as_int(&self) -> Option<i64>
pub const fn as_int(&self) -> Option<i64>
Returns the integer if this is a Value::Int, otherwise None.
Floats are not coerced; a Value::Float returns None.
§Examples
use bison_db::Value;
assert_eq!(Value::from(7_i64).as_int(), Some(7));
assert_eq!(Value::from(7.0_f64).as_int(), None);Sourcepub const fn as_float(&self) -> Option<f64>
pub const fn as_float(&self) -> Option<f64>
Returns the float if this is a Value::Float, otherwise None.
§Examples
use bison_db::Value;
assert_eq!(Value::from(1.5_f64).as_float(), Some(1.5));
assert_eq!(Value::from(1_i64).as_float(), None);Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
Returns the string slice if this is a Value::Str, otherwise None.
§Examples
use bison_db::Value;
assert_eq!(Value::from("bison").as_str(), Some("bison"));Sourcepub fn as_bytes(&self) -> Option<&[u8]>
pub fn as_bytes(&self) -> Option<&[u8]>
Returns the byte slice if this is a Value::Bytes, otherwise None.
§Examples
use bison_db::Value;
assert_eq!(Value::Bytes(vec![1, 2, 3]).as_bytes(), Some(&[1, 2, 3][..]));Sourcepub fn as_array(&self) -> Option<&[Value]>
pub fn as_array(&self) -> Option<&[Value]>
Returns the element slice if this is a Value::Array, otherwise None.
§Examples
use bison_db::Value;
let v = Value::Array(vec![Value::from(1_i64), Value::from(2_i64)]);
assert_eq!(v.as_array().map(<[_]>::len), Some(2));Sourcepub fn as_object(&self) -> Option<&Document>
pub fn as_object(&self) -> Option<&Document>
Returns the nested document if this is a Value::Object, otherwise None.
§Examples
use bison_db::{Document, Value};
let mut inner = Document::new();
inner.set("k", 1_i64);
let v = Value::Object(inner);
assert_eq!(v.as_object().and_then(|d| d.get("k")).and_then(Value::as_int), Some(1));Sourcepub const fn type_name(&self) -> &'static str
pub const fn type_name(&self) -> &'static str
Returns a short, stable name for the value’s variant.
Intended for diagnostics and error messages, not for logic; match on the variant directly when behaviour depends on the type.
§Examples
use bison_db::Value;
assert_eq!(Value::from(1_i64).type_name(), "int");
assert_eq!(Value::Null.type_name(), "null");Trait Implementations§
Source§impl<'de> Deserialize<'de> for Value
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Value
serde only.Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl<T: Into<Value>> From<Option<T>> for Value
impl<T: Into<Value>> From<Option<T>> for Value
Source§fn from(v: Option<T>) -> Self
fn from(v: Option<T>) -> Self
Some(x) becomes x’s value; None becomes Value::Null.