#[derive(Debug, Clone, PartialEq)]
pub enum Value {
Integer(i128),
Bytes(Vec<u8>),
Text(String),
Array(Vec<Value>),
Map(Vec<(Value, Value)>),
Tag(u64, Box<Value>),
Bool(bool),
Null,
Float(f64),
}
impl Value {
pub fn as_text(&self) -> Option<&str> {
match self {
Value::Text(s) => Some(s),
_ => None,
}
}
pub fn as_bytes(&self) -> Option<&[u8]> {
match self {
Value::Bytes(b) => Some(b),
_ => None,
}
}
pub fn as_map(&self) -> Option<&[(Value, Value)]> {
match self {
Value::Map(m) => Some(m),
_ => None,
}
}
pub fn get(&self, key: &str) -> Option<&Value> {
match self {
Value::Map(m) => m
.iter()
.find(|(k, _)| k.as_text() == Some(key))
.map(|(_, v)| v),
_ => None,
}
}
}
impl From<&str> for Value {
fn from(s: &str) -> Self {
Value::Text(s.to_string())
}
}
impl From<String> for Value {
fn from(s: String) -> Self {
Value::Text(s)
}
}
impl From<i128> for Value {
fn from(n: i128) -> Self {
Value::Integer(n)
}
}