use std::collections::BTreeMap;
use iso8601::DateTime;
pub mod de;
pub mod ser;
pub use de::Deserializer;
pub use ser::Serializer;
pub fn to_value<T>(value: T) -> crate::Result<Value>
where
T: serde::Serialize,
{
value.serialize(Serializer)
}
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
Int(i32),
Int64(i64),
Bool(bool),
String(String),
Double(f64),
DateTime(DateTime),
Base64(Vec<u8>),
Struct(BTreeMap<String, Value>),
Array(Vec<Value>),
Nil,
}
impl Value {
pub fn as_i32(&self) -> Option<i32> {
match *self {
Value::Int(i) => Some(i),
_ => None,
}
}
pub fn as_i64(&self) -> Option<i64> {
match *self {
Value::Int(i) => Some(i64::from(i)),
Value::Int64(i) => Some(i),
_ => None,
}
}
pub fn as_bool(&self) -> Option<bool> {
match *self {
Value::Bool(b) => Some(b),
_ => None,
}
}
pub fn as_str(&self) -> Option<&str> {
match *self {
Value::String(ref s) => Some(s),
_ => None,
}
}
pub fn as_f64(&self) -> Option<f64> {
match *self {
Value::Double(d) => Some(d),
_ => None,
}
}
pub fn as_datetime(&self) -> Option<DateTime> {
match *self {
Value::DateTime(dt) => Some(dt),
_ => None,
}
}
pub fn as_bytes(&self) -> Option<&[u8]> {
match *self {
Value::Base64(ref data) => Some(data),
_ => None,
}
}
pub fn as_struct(&self) -> Option<&BTreeMap<String, Value>> {
match *self {
Value::Struct(ref map) => Some(map),
_ => None,
}
}
pub fn as_array(&self) -> Option<&[Value]> {
match *self {
Value::Array(ref array) => Some(array),
_ => None,
}
}
}
impl From<i32> for Value {
fn from(other: i32) -> Self {
Value::Int(other)
}
}
impl From<i64> for Value {
fn from(other: i64) -> Self {
Value::Int64(other)
}
}
impl From<bool> for Value {
fn from(other: bool) -> Self {
Value::Bool(other)
}
}
impl From<String> for Value {
fn from(other: String) -> Self {
Value::String(other)
}
}
impl From<&str> for Value {
fn from(other: &str) -> Self {
Value::String(other.to_string())
}
}
impl From<f64> for Value {
fn from(other: f64) -> Self {
Value::Double(other)
}
}
impl From<DateTime> for Value {
fn from(other: DateTime) -> Self {
Value::DateTime(other)
}
}
impl From<Vec<Value>> for Value {
fn from(other: Vec<Value>) -> Value {
Value::Array(other)
}
}
impl From<BTreeMap<String, Value>> for Value {
fn from(other: BTreeMap<String, Value>) -> Value {
Value::Struct(other)
}
}
impl From<Vec<u8>> for Value {
fn from(other: Vec<u8>) -> Self {
Value::Base64(other)
}
}