#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(unsafe_code)]
#![cfg_attr(test, deny(warnings))]
#![deny(missing_docs, unused_imports)]
extern crate alloc;
mod de;
mod error;
mod number;
mod ser;
mod tests;
use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
pub use error::Data as FoundData;
pub use error::Error;
pub use error::ErrorKind;
pub use error::Expected;
pub use error::Found;
pub use error::Result;
pub use number::Number;
#[cfg(feature = "serde")]
pub use {de::Deserializer, de::Unexpected, de::ValueVisitor, ser::Serializer};
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum Data<'a> {
Unit,
NewType {
value: Value<'a>,
},
Tuple {
values: Vec<Value<'a>>,
},
Struct {
fields: Vec<(Cow<'static, str>, Value<'a>)>,
},
}
impl Data<'_> {
pub fn into_owned(self) -> Data<'static> {
match self {
Data::Unit => Data::Unit,
Data::NewType { value } => Data::NewType {
value: value.into_owned(),
},
Data::Tuple { values } => Data::Tuple {
values: values.into_iter().map(Value::into_owned).collect(),
},
Data::Struct { fields } => Data::Struct {
fields: fields
.into_iter()
.map(|(key, value)| (key, value.into_owned()))
.collect(),
},
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
#[cfg_attr(feature = "derive", derive(serde::Serialize, serde::Deserialize))]
pub enum DataType {
Unit,
NewType,
Tuple,
Struct,
}
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct Struct<'a> {
pub name: Cow<'static, str>,
pub data: Data<'a>,
}
impl Struct<'_> {
pub fn into_owned(self) -> Struct<'static> {
Struct {
data: self.data.into_owned(),
..self
}
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct Enum<'a> {
pub name: Cow<'static, str>,
pub variant_index: u32,
pub variant: Cow<'static, str>,
pub data: Data<'a>,
}
impl Enum<'_> {
pub fn into_owned(self) -> Enum<'static> {
Enum {
data: self.data.into_owned(),
..self
}
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum Value<'a> {
Unit,
Bool(bool),
Number(Number),
Char(char),
String(Cow<'a, str>),
Bytes(Cow<'a, [u8]>),
Seq(Vec<Value<'a>>),
Map(Vec<(Value<'a>, Value<'a>)>),
Option(Option<Box<Value<'a>>>),
Struct(Box<Struct<'a>>),
Enum(Box<Enum<'a>>),
Tuple(Vec<Value<'a>>),
}
impl Value<'_> {
pub fn into_owned(self) -> Value<'static> {
match self {
Value::Unit => Value::Unit,
Value::Bool(v) => Value::Bool(v),
Value::Number(v) => Value::Number(v),
Value::Char(v) => Value::Char(v),
Value::String(v) => Value::String(Cow::Owned(v.into_owned())),
Value::Bytes(v) => Value::Bytes(Cow::Owned(v.into_owned())),
Value::Seq(v) => Value::Seq(v.into_iter().map(Self::into_owned).collect()),
Value::Map(v) => Value::Map(
v.into_iter()
.map(|(key, value)| (key.into_owned(), value.into_owned()))
.collect(),
),
Value::Option(v) => match v {
Some(v) => Value::Option(Some(Box::new(v.into_owned()))),
None => Value::Option(None),
},
Value::Struct(v) => Value::Struct(Box::new(v.into_owned())),
Value::Enum(v) => Value::Enum(Box::new(v.into_owned())),
Value::Tuple(v) => Value::Tuple(v.into_iter().map(Self::into_owned).collect()),
}
}
}
impl From<()> for Value<'static> {
fn from(_: ()) -> Self {
Self::Unit
}
}
impl From<bool> for Value<'static> {
fn from(value: bool) -> Self {
Self::Bool(value)
}
}
impl<T> From<T> for Value<'static>
where
T: Into<Number>,
{
fn from(value: T) -> Self {
Self::Number(value.into())
}
}
impl From<char> for Value<'static> {
fn from(value: char) -> Self {
Self::Char(value)
}
}
impl<'a> From<Cow<'a, str>> for Value<'a> {
fn from(value: Cow<'a, str>) -> Self {
Self::String(value)
}
}
impl<'a> From<&'a str> for Value<'a> {
fn from(value: &'a str) -> Self {
Self::String(Cow::Borrowed(value))
}
}
impl<'a> From<&'a String> for Value<'a> {
fn from(value: &'a String) -> Self {
Self::String(Cow::Borrowed(value))
}
}
impl From<String> for Value<'static> {
fn from(value: String) -> Self {
Self::String(Cow::Owned(value))
}
}
impl<'a> From<&'a [u8]> for Value<'a> {
fn from(value: &'a [u8]) -> Self {
Self::Bytes(Cow::Borrowed(value))
}
}
impl<'a> From<&'a Vec<u8>> for Value<'a> {
fn from(value: &'a Vec<u8>) -> Self {
Self::Bytes(Cow::Borrowed(value))
}
}
impl From<Vec<u8>> for Value<'static> {
fn from(value: Vec<u8>) -> Self {
Self::Bytes(Cow::Owned(value))
}
}