use serde::{Serialize, Serializer};
use crate::function_executor::function_error::FunctionError;
#[derive(Debug)]
pub enum Value {
String(String),
Bool(bool),
Bytes(Vec<u8>),
Char(char),
Option(Option<Box<Value>>),
UInt(u128),
Int(i128),
Float(f64),
}
impl Value {
pub fn as_string(&self) -> Result<&String, FunctionError> {
if let Value::String(ref value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as string", self)));
}
pub fn as_mut_string(&mut self) -> Result<&mut String, FunctionError> {
if let Value::String(ref mut value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as string", self)));
}
pub fn into_string(self) -> Result<String, FunctionError> {
if let Value::String(value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as string", self)));
}
pub fn as_bool(&self) -> Result<&bool, FunctionError> {
if let Value::Bool(ref value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as bool", self)));
}
pub fn as_mut_bool(&mut self) -> Result<&mut bool, FunctionError> {
if let Value::Bool(ref mut value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as bool", self)));
}
pub fn into_bool(self) -> Result<bool, FunctionError> {
if let Value::Bool(value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as bool", self)));
}
pub fn as_bytes(&self) -> Result<&Vec<u8>, FunctionError> {
if let Value::Bytes(ref value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as bytes", self)));
}
pub fn as_mut_bytes(&mut self) -> Result<&mut Vec<u8>, FunctionError> {
if let Value::Bytes(ref mut value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as bytes", self)));
}
pub fn into_bytes(self) -> Result<Vec<u8>, FunctionError> {
if let Value::Bytes(value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as bytes", self)));
}
pub fn as_char(&self) -> Result<&char, FunctionError> {
if let Value::Char(ref value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as char", self)));
}
pub fn as_mut_char(&mut self) -> Result<&mut char, FunctionError> {
if let Value::Char(ref mut value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as char", self)));
}
pub fn as_option(&self) -> Result<Option<&Value>, FunctionError> {
if let Value::Option(ref value) = self {
let value = value.as_ref();
let value = value.map(Box::as_ref);
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as option", self)));
}
pub fn as_mut_option(&mut self) -> Result<Option<&mut Value>, FunctionError> {
if let Value::Option(ref mut value) = self {
let value = value.as_mut();
let value = value.map(Box::as_mut);
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as option", self)));
}
pub fn into_option(self) -> Result<Option<Value>, FunctionError> {
if let Value::Option(value) = self {
let value = value.map(|value| {
return *value;
});
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as option", self)));
}
pub fn as_uint(&self) -> Result<&u128, FunctionError> {
if let Value::UInt(ref value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as uint", self)));
}
pub fn as_mut_uint(&mut self) -> Result<&mut u128, FunctionError> {
if let Value::UInt(ref mut value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as uint", self)));
}
pub fn into_uint(self) -> Result<u128, FunctionError> {
if let Value::UInt(value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as uint", self)));
}
pub fn as_int(&self) -> Result<&i128, FunctionError> {
if let Value::Int(ref value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as int", self)));
}
pub fn as_mut_int(&mut self) -> Result<&mut i128, FunctionError> {
if let Value::Int(ref mut value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as int", self)));
}
pub fn into_int(self) -> Result<i128, FunctionError> {
if let Value::Int(value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as int", self)));
}
pub fn as_float(&self) -> Result<&f64, FunctionError> {
if let Value::Float(ref value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as float", self)));
}
pub fn as_mut_float(&mut self) -> Result<&mut f64, FunctionError> {
if let Value::Float(ref mut value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as float", self)));
}
pub fn floato_float(self) -> Result<f64, FunctionError> {
if let Value::Float(value) = self {
return Ok(value);
}
return Err(FunctionError::msg(format!("Trying to cast {:?} as float", self)));
}
}
impl Serialize for Value {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer
{
match self {
Value::String(ref value) => {
return value.serialize(serializer);
},
Value::Bool(ref value) => {
return value.serialize(serializer);
},
Value::Bytes(ref value) => {
return value.serialize(serializer);
},
Value::Char(ref value) => {
return value.serialize(serializer);
},
Value::Option(ref value) => {
return value.serialize(serializer);
},
Value::UInt(ref value) => {
return value.serialize(serializer);
},
Value::Int(ref value) => {
return value.serialize(serializer);
},
Value::Float(ref value) => {
return value.serialize(serializer);
},
}
}
}