1#![cfg_attr(not(feature = "std"), no_std)]
4#![forbid(unsafe_code)]
5#![cfg_attr(test, deny(warnings))]
6#![deny(missing_docs, unused_imports)]
7
8extern crate alloc;
9
10mod de;
11mod error;
12mod number;
13mod ser;
14mod tests;
15
16use alloc::borrow::Cow;
17use alloc::boxed::Box;
18use alloc::string::String;
19use alloc::vec::Vec;
20
21pub use error::Data as FoundData;
22pub use error::Error;
23pub use error::ErrorKind;
24pub use error::Expected;
25pub use error::Found;
26pub use error::Result;
27pub use number::Number;
28#[cfg(feature = "serde")]
29pub use {de::Deserializer, de::Unexpected, de::ValueVisitor, ser::Serializer};
30
31#[derive(Debug, Clone, PartialEq, PartialOrd)]
33pub enum Data<'a> {
34 Unit,
36 NewType {
38 value: Value<'a>,
40 },
41 Tuple {
43 values: Vec<Value<'a>>,
45 },
46 Struct {
48 fields: Vec<(Cow<'static, str>, Value<'a>)>,
50 },
51}
52
53impl Data<'_> {
54 pub fn into_owned(self) -> Data<'static> {
56 match self {
57 Data::Unit => Data::Unit,
58 Data::NewType { value } => Data::NewType {
59 value: value.into_owned(),
60 },
61 Data::Tuple { values } => Data::Tuple {
62 values: values.into_iter().map(Value::into_owned).collect(),
63 },
64 Data::Struct { fields } => Data::Struct {
65 fields: fields
66 .into_iter()
67 .map(|(key, value)| (key, value.into_owned()))
68 .collect(),
69 },
70 }
71 }
72}
73
74#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
76#[cfg_attr(feature = "derive", derive(serde::Serialize, serde::Deserialize))]
77pub enum DataType {
78 Unit,
80 NewType,
82 Tuple,
84 Struct,
86}
87
88#[derive(Debug, Clone, PartialEq, PartialOrd)]
90pub struct Struct<'a> {
91 pub name: Cow<'static, str>,
93 pub data: Data<'a>,
95}
96
97impl Struct<'_> {
98 pub fn into_owned(self) -> Struct<'static> {
100 Struct {
101 data: self.data.into_owned(),
102 ..self
103 }
104 }
105}
106
107#[derive(Debug, Clone, PartialEq, PartialOrd)]
109pub struct Enum<'a> {
110 pub name: Cow<'static, str>,
112 pub variant_index: u32,
114 pub variant: Cow<'static, str>,
116 pub data: Data<'a>,
118}
119
120impl Enum<'_> {
121 pub fn into_owned(self) -> Enum<'static> {
123 Enum {
124 data: self.data.into_owned(),
125 ..self
126 }
127 }
128}
129
130#[derive(Debug, Clone, PartialEq, PartialOrd)]
132pub enum Value<'a> {
133 Unit,
135 Bool(bool),
137 Number(Number),
139 Char(char),
141 String(Cow<'a, str>),
143 Bytes(Cow<'a, [u8]>),
145 Seq(Vec<Value<'a>>),
147 Map(Vec<(Value<'a>, Value<'a>)>),
149 Option(Option<Box<Value<'a>>>),
151 Struct(Box<Struct<'a>>),
153 Enum(Box<Enum<'a>>),
155 Tuple(Vec<Value<'a>>),
157}
158
159impl Value<'_> {
160 pub fn into_owned(self) -> Value<'static> {
162 match self {
163 Value::Unit => Value::Unit,
164 Value::Bool(v) => Value::Bool(v),
165 Value::Number(v) => Value::Number(v),
166 Value::Char(v) => Value::Char(v),
167 Value::String(v) => Value::String(Cow::Owned(v.into_owned())),
168 Value::Bytes(v) => Value::Bytes(Cow::Owned(v.into_owned())),
169 Value::Seq(v) => Value::Seq(v.into_iter().map(Self::into_owned).collect()),
170 Value::Map(v) => Value::Map(
171 v.into_iter()
172 .map(|(key, value)| (key.into_owned(), value.into_owned()))
173 .collect(),
174 ),
175 Value::Option(v) => match v {
176 Some(v) => Value::Option(Some(Box::new(v.into_owned()))),
177 None => Value::Option(None),
178 },
179 Value::Struct(v) => Value::Struct(Box::new(v.into_owned())),
180 Value::Enum(v) => Value::Enum(Box::new(v.into_owned())),
181 Value::Tuple(v) => Value::Tuple(v.into_iter().map(Self::into_owned).collect()),
182 }
183 }
184}
185
186impl From<()> for Value<'static> {
187 fn from(_: ()) -> Self {
188 Self::Unit
189 }
190}
191
192impl From<bool> for Value<'static> {
193 fn from(value: bool) -> Self {
194 Self::Bool(value)
195 }
196}
197
198impl<T> From<T> for Value<'static>
199where
200 T: Into<Number>,
201{
202 fn from(value: T) -> Self {
203 Self::Number(value.into())
204 }
205}
206
207impl From<char> for Value<'static> {
208 fn from(value: char) -> Self {
209 Self::Char(value)
210 }
211}
212
213impl<'a> From<Cow<'a, str>> for Value<'a> {
214 fn from(value: Cow<'a, str>) -> Self {
215 Self::String(value)
216 }
217}
218
219impl<'a> From<&'a str> for Value<'a> {
220 fn from(value: &'a str) -> Self {
221 Self::String(Cow::Borrowed(value))
222 }
223}
224
225impl<'a> From<&'a String> for Value<'a> {
226 fn from(value: &'a String) -> Self {
227 Self::String(Cow::Borrowed(value))
228 }
229}
230
231impl From<String> for Value<'static> {
232 fn from(value: String) -> Self {
233 Self::String(Cow::Owned(value))
234 }
235}
236
237impl<'a> From<&'a [u8]> for Value<'a> {
238 fn from(value: &'a [u8]) -> Self {
239 Self::Bytes(Cow::Borrowed(value))
240 }
241}
242
243impl<'a> From<&'a Vec<u8>> for Value<'a> {
244 fn from(value: &'a Vec<u8>) -> Self {
245 Self::Bytes(Cow::Borrowed(value))
246 }
247}
248
249impl From<Vec<u8>> for Value<'static> {
250 fn from(value: Vec<u8>) -> Self {
251 Self::Bytes(Cow::Owned(value))
252 }
253}