#[derive(Debug, Clone, PartialEq)]
pub enum Value {
Null,
String(String),
Bulk(Vec<u8>),
Integer(i64),
Boolean(bool),
Double(f64),
Array(Vec<Value>),
Map(Vec<(Value, Value)>),
Error(String),
}
impl Value {
pub fn as_str(&self) -> Option<&str> {
match self {
Value::String(s) => Some(s),
Value::Bulk(b) => std::str::from_utf8(b).ok(),
_ => None,
}
}
pub fn as_bytes(&self) -> Option<&[u8]> {
match self {
Value::Bulk(b) => Some(b),
Value::String(s) => Some(s.as_bytes()),
_ => None,
}
}
pub fn as_int(&self) -> Option<i64> {
match self {
Value::Integer(i) => Some(*i),
_ => None,
}
}
pub fn as_bool(&self) -> Option<bool> {
match self {
Value::Boolean(b) => Some(*b),
Value::Integer(i) => Some(*i != 0),
_ => None,
}
}
pub fn is_null(&self) -> bool {
matches!(self, Value::Null)
}
pub fn is_error(&self) -> bool {
matches!(self, Value::Error(_))
}
}