use crate::{Error, Map, Result};
mod de;
mod ser;
pub use de::from_object;
pub use ser::{to_object, Serializer};
#[derive(Debug, PartialEq, Clone)]
pub enum Object {
Null,
Boolean(bool),
Integer(i64),
Float(f64),
String(std::string::String),
Raw(Vec<u8>),
List(Vec<Object>),
Map(Map),
}
impl Object {
pub fn as_null(&self) -> Result<()> {
match self {
Object::Null => Ok(()),
_ => Err(Error::WrongType),
}
}
pub fn as_boolean(&self) -> Result<bool> {
match self {
Object::Boolean(b) => Ok(*b),
_ => Err(Error::WrongType),
}
}
pub fn as_integer(&self) -> Result<i64> {
match self {
Object::Integer(i) => Ok(*i),
_ => Err(Error::WrongType),
}
}
pub fn as_float(&self) -> Result<f64> {
match self {
Object::Float(f) => Ok(*f),
_ => Err(Error::WrongType),
}
}
pub fn as_string(&self) -> Result<&str> {
match self {
Object::String(s) => Ok(s),
_ => Err(Error::WrongType),
}
}
pub fn as_raw(&self) -> Result<&[u8]> {
match self {
Object::Raw(r) => Ok(r),
_ => Err(Error::WrongType),
}
}
pub fn as_list(&self) -> Result<&[Object]> {
match self {
Object::List(l) => Ok(l),
_ => Err(Error::WrongType),
}
}
pub fn as_map(&self) -> Result<&Map> {
match self {
Object::Map(d) => Ok(d),
_ => Err(Error::WrongType),
}
}
pub fn to_null(self) -> Result<()> {
self.as_null()
}
pub fn to_boolean(self) -> Result<bool> {
self.as_boolean()
}
pub fn to_integer(self) -> Result<i64> {
self.as_integer()
}
pub fn to_float(self) -> Result<f64> {
self.as_float()
}
pub fn to_string(self) -> Result<String> {
match self {
Object::String(s) => Ok(s),
_ => Err(Error::WrongType),
}
}
pub fn to_raw(self) -> Result<Vec<u8>> {
match self {
Object::Raw(r) => Ok(r),
_ => Err(Error::WrongType),
}
}
pub fn to_list(self) -> Result<Vec<Object>> {
match self {
Object::List(l) => Ok(l),
_ => Err(Error::WrongType),
}
}
pub fn to_map(self) -> Result<Map> {
match self {
Object::Map(d) => Ok(d),
_ => Err(Error::WrongType),
}
}
}