use std::collections::BTreeMap;
use bytes::Bytes;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Value {
Integer(i64),
Bytes(Bytes),
List(Vec<Value>),
Dict(BTreeMap<Bytes, Value>),
}
impl Value {
pub fn string(s: &str) -> Self {
Value::Bytes(Bytes::copy_from_slice(s.as_bytes()))
}
pub fn as_integer(&self) -> Option<i64> {
match self {
Value::Integer(i) => Some(*i),
_ => None,
}
}
pub fn as_bytes(&self) -> Option<&Bytes> {
match self {
Value::Bytes(b) => Some(b),
_ => None,
}
}
pub fn as_str(&self) -> Option<&str> {
match self {
Value::Bytes(b) => std::str::from_utf8(b).ok(),
_ => None,
}
}
pub fn as_list(&self) -> Option<&Vec<Value>> {
match self {
Value::List(l) => Some(l),
_ => None,
}
}
pub fn as_dict(&self) -> Option<&BTreeMap<Bytes, Value>> {
match self {
Value::Dict(d) => Some(d),
_ => None,
}
}
pub fn into_dict(self) -> Option<BTreeMap<Bytes, Value>> {
match self {
Value::Dict(d) => Some(d),
_ => None,
}
}
pub fn get(&self, key: &[u8]) -> Option<&Value> {
self.as_dict()?.get(key)
}
}
impl From<i64> for Value {
fn from(i: i64) -> Self {
Value::Integer(i)
}
}
impl From<&str> for Value {
fn from(s: &str) -> Self {
Value::string(s)
}
}
impl From<Bytes> for Value {
fn from(b: Bytes) -> Self {
Value::Bytes(b)
}
}
impl From<Vec<Value>> for Value {
fn from(l: Vec<Value>) -> Self {
Value::List(l)
}
}
impl From<BTreeMap<Bytes, Value>> for Value {
fn from(d: BTreeMap<Bytes, Value>) -> Self {
Value::Dict(d)
}
}