use crate::dispatch::Dispatch;
use crate::error::ComError;
#[derive(Debug)]
pub enum Value {
Empty,
Null,
Bool(bool),
I32(i32),
I64(i64),
NullObject,
U64(u64),
F64(f64),
Str(String),
Object(Box<dyn Dispatch>),
}
impl Value {
#[must_use]
pub const fn kind(&self) -> &'static str {
match self {
Self::Empty => "Empty",
Self::Null => "Null",
Self::Bool(_) => "Bool",
Self::I32(_) => "I32",
Self::I64(_) => "I64",
Self::F64(_) => "F64",
Self::Str(_) => "Str",
Self::NullObject => "NullObject",
Self::U64(_) => "U64",
Self::Object(_) => "Object",
}
}
pub const fn as_i32(&self) -> Result<i32, ComError> {
match self {
Self::I32(value) => Ok(*value),
other => Err(ComError::UnexpectedType {
expected: "I32",
actual: other.kind(),
}),
}
}
pub const fn as_i64(&self) -> Result<i64, ComError> {
match self {
Self::I64(value) => Ok(*value),
other => Err(ComError::UnexpectedType {
expected: "I64",
actual: other.kind(),
}),
}
}
pub const fn as_f64(&self) -> Result<f64, ComError> {
match self {
Self::F64(value) => Ok(*value),
other => Err(ComError::UnexpectedType {
expected: "F64",
actual: other.kind(),
}),
}
}
pub const fn as_bool(&self) -> Result<bool, ComError> {
match self {
Self::Bool(value) => Ok(*value),
other => Err(ComError::UnexpectedType {
expected: "Bool",
actual: other.kind(),
}),
}
}
pub fn into_string(self) -> Result<String, ComError> {
match self {
Self::Str(value) => Ok(value),
other => Err(ComError::UnexpectedType {
expected: "Str",
actual: other.kind(),
}),
}
}
pub fn into_object(self) -> Result<Box<dyn Dispatch>, ComError> {
match self {
Self::Object(dispatch) => Ok(dispatch),
other => Err(ComError::UnexpectedType {
expected: "Object",
actual: other.kind(),
}),
}
}
}