1use serde_core::de;
2use serde_core::ser;
3
4use crate::Value;
5use crate::alloc_prelude::*;
6use crate::map::Map;
7
8pub type Table = Map<String, Value>;
15
16impl Table {
17 pub fn try_from<T>(value: T) -> Result<Self, crate::ser::Error>
22 where
23 T: ser::Serialize,
24 {
25 value.serialize(TableSerializer)
26 }
27
28 pub fn try_into<'de, T>(self) -> Result<T, crate::de::Error>
36 where
37 T: de::Deserialize<'de>,
38 {
39 de::Deserialize::deserialize(self)
40 }
41}
42
43#[cfg(feature = "display")]
44impl core::fmt::Display for Table {
45 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
46 crate::ser::to_string(self)
47 .expect("Unable to represent value as string")
48 .fmt(f)
49 }
50}
51
52#[cfg(feature = "parse")]
53impl core::str::FromStr for Table {
54 type Err = crate::de::Error;
55 fn from_str(s: &str) -> Result<Self, Self::Err> {
56 crate::from_str(s)
57 }
58}
59impl ser::Serialize for Table {
60 #[inline]
61 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
62 where
63 S: ser::Serializer,
64 {
65 use serde_core::ser::SerializeMap;
66 let mut map = serializer.serialize_map(Some(self.len()))?;
67 for (k, v) in self {
68 map.serialize_key(k)?;
69 map.serialize_value(v)?;
70 }
71 map.end()
72 }
73}
74
75impl<'de> de::Deserialize<'de> for Table {
76 #[inline]
77 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
78 where
79 D: de::Deserializer<'de>,
80 {
81 struct Visitor;
82
83 impl<'de> de::Visitor<'de> for Visitor {
84 type Value = Map<String, Value>;
85
86 fn expecting(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
87 formatter.write_str("a map")
88 }
89
90 #[inline]
91 fn visit_unit<E>(self) -> Result<Self::Value, E>
92 where
93 E: de::Error,
94 {
95 Ok(Map::new())
96 }
97
98 #[inline]
99 fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
100 where
101 V: de::MapAccess<'de>,
102 {
103 let mut values = Map::new();
104
105 while let Some((key, value)) = visitor.next_entry()? {
106 values.insert(key, value);
107 }
108
109 Ok(values)
110 }
111 }
112
113 deserializer.deserialize_map(Visitor)
114 }
115}
116
117impl<'de> de::Deserializer<'de> for Table {
118 type Error = crate::de::Error;
119
120 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, crate::de::Error>
121 where
122 V: de::Visitor<'de>,
123 {
124 Value::Table(self).deserialize_any(visitor)
125 }
126
127 #[inline]
128 fn deserialize_enum<V>(
129 self,
130 name: &'static str,
131 variants: &'static [&'static str],
132 visitor: V,
133 ) -> Result<V::Value, crate::de::Error>
134 where
135 V: de::Visitor<'de>,
136 {
137 Value::Table(self).deserialize_enum(name, variants, visitor)
138 }
139
140 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, crate::de::Error>
143 where
144 V: de::Visitor<'de>,
145 {
146 Value::Table(self).deserialize_option(visitor)
147 }
148
149 fn deserialize_newtype_struct<V>(
150 self,
151 name: &'static str,
152 visitor: V,
153 ) -> Result<V::Value, crate::de::Error>
154 where
155 V: de::Visitor<'de>,
156 {
157 Value::Table(self).deserialize_newtype_struct(name, visitor)
158 }
159
160 fn deserialize_struct<V>(
161 self,
162 name: &'static str,
163 fields: &'static [&'static str],
164 visitor: V,
165 ) -> Result<V::Value, crate::de::Error>
166 where
167 V: de::Visitor<'de>,
168 {
169 Value::Table(self).deserialize_struct(name, fields, visitor)
170 }
171
172 serde_core::forward_to_deserialize_any! {
173 bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit seq
174 bytes byte_buf map unit_struct tuple_struct
175 tuple ignored_any identifier
176 }
177}
178
179impl de::IntoDeserializer<'_, crate::de::Error> for Table {
180 type Deserializer = Self;
181
182 fn into_deserializer(self) -> Self {
183 self
184 }
185}
186
187pub(crate) struct TableSerializer;
188
189impl ser::Serializer for TableSerializer {
190 type Ok = Table;
191 type Error = crate::ser::Error;
192
193 type SerializeSeq = ser::Impossible<Self::Ok, Self::Error>;
194 type SerializeTuple = ser::Impossible<Self::Ok, Self::Error>;
195 type SerializeTupleStruct = ser::Impossible<Self::Ok, Self::Error>;
196 type SerializeTupleVariant = ser::Impossible<Self::Ok, Self::Error>;
197 type SerializeMap = SerializeMap;
198 type SerializeStruct = SerializeMap;
199 type SerializeStructVariant = ser::Impossible<Self::Ok, Self::Error>;
200
201 fn serialize_bool(self, _value: bool) -> Result<Table, crate::ser::Error> {
202 Err(crate::ser::Error::unsupported_type(None))
203 }
204
205 fn serialize_i8(self, _value: i8) -> Result<Table, crate::ser::Error> {
206 Err(crate::ser::Error::unsupported_type(None))
207 }
208
209 fn serialize_i16(self, _value: i16) -> Result<Table, crate::ser::Error> {
210 Err(crate::ser::Error::unsupported_type(None))
211 }
212
213 fn serialize_i32(self, _value: i32) -> Result<Table, crate::ser::Error> {
214 Err(crate::ser::Error::unsupported_type(None))
215 }
216
217 fn serialize_i64(self, _value: i64) -> Result<Table, crate::ser::Error> {
218 Err(crate::ser::Error::unsupported_type(None))
219 }
220
221 fn serialize_u8(self, _value: u8) -> Result<Table, crate::ser::Error> {
222 Err(crate::ser::Error::unsupported_type(None))
223 }
224
225 fn serialize_u16(self, _value: u16) -> Result<Table, crate::ser::Error> {
226 Err(crate::ser::Error::unsupported_type(None))
227 }
228
229 fn serialize_u32(self, _value: u32) -> Result<Table, crate::ser::Error> {
230 Err(crate::ser::Error::unsupported_type(None))
231 }
232
233 fn serialize_u64(self, _value: u64) -> Result<Table, crate::ser::Error> {
234 Err(crate::ser::Error::unsupported_type(None))
235 }
236
237 fn serialize_f32(self, _value: f32) -> Result<Table, crate::ser::Error> {
238 Err(crate::ser::Error::unsupported_type(None))
239 }
240
241 fn serialize_f64(self, _value: f64) -> Result<Table, crate::ser::Error> {
242 Err(crate::ser::Error::unsupported_type(None))
243 }
244
245 fn serialize_char(self, _value: char) -> Result<Table, crate::ser::Error> {
246 Err(crate::ser::Error::unsupported_type(None))
247 }
248
249 fn serialize_str(self, _value: &str) -> Result<Table, crate::ser::Error> {
250 Err(crate::ser::Error::unsupported_type(None))
251 }
252
253 fn serialize_bytes(self, _value: &[u8]) -> Result<Table, crate::ser::Error> {
254 Err(crate::ser::Error::unsupported_type(None))
255 }
256
257 fn serialize_unit(self) -> Result<Table, crate::ser::Error> {
258 Err(crate::ser::Error::unsupported_type(None))
259 }
260
261 fn serialize_unit_struct(self, _name: &'static str) -> Result<Table, crate::ser::Error> {
262 Err(crate::ser::Error::unsupported_type(None))
263 }
264
265 fn serialize_unit_variant(
266 self,
267 name: &'static str,
268 _variant_index: u32,
269 _variant: &'static str,
270 ) -> Result<Table, crate::ser::Error> {
271 Err(crate::ser::Error::unsupported_type(Some(name)))
272 }
273
274 fn serialize_newtype_struct<T>(
275 self,
276 _name: &'static str,
277 value: &T,
278 ) -> Result<Table, crate::ser::Error>
279 where
280 T: ser::Serialize + ?Sized,
281 {
282 value.serialize(self)
283 }
284
285 fn serialize_newtype_variant<T>(
286 self,
287 _name: &'static str,
288 _variant_index: u32,
289 variant: &'static str,
290 value: &T,
291 ) -> Result<Table, crate::ser::Error>
292 where
293 T: ser::Serialize + ?Sized,
294 {
295 let value = value.serialize(crate::value::ValueSerializer)?;
296 let mut table = Table::new();
297 table.insert(variant.to_owned(), value);
298 Ok(table)
299 }
300
301 fn serialize_none(self) -> Result<Table, crate::ser::Error> {
302 Err(crate::ser::Error::unsupported_none())
303 }
304
305 fn serialize_some<T>(self, value: &T) -> Result<Table, crate::ser::Error>
306 where
307 T: ser::Serialize + ?Sized,
308 {
309 value.serialize(self)
310 }
311
312 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, crate::ser::Error> {
313 Err(crate::ser::Error::unsupported_type(None))
314 }
315
316 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, crate::ser::Error> {
317 Err(crate::ser::Error::unsupported_type(None))
318 }
319
320 fn serialize_tuple_struct(
321 self,
322 name: &'static str,
323 _len: usize,
324 ) -> Result<Self::SerializeTupleStruct, crate::ser::Error> {
325 Err(crate::ser::Error::unsupported_type(Some(name)))
326 }
327
328 fn serialize_tuple_variant(
329 self,
330 name: &'static str,
331 _variant_index: u32,
332 _variant: &'static str,
333 _len: usize,
334 ) -> Result<Self::SerializeTupleVariant, crate::ser::Error> {
335 Err(crate::ser::Error::unsupported_type(Some(name)))
336 }
337
338 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, crate::ser::Error> {
339 Ok(SerializeMap::new())
340 }
341
342 fn serialize_struct(
343 self,
344 _name: &'static str,
345 len: usize,
346 ) -> Result<Self::SerializeStruct, crate::ser::Error> {
347 self.serialize_map(Some(len))
348 }
349
350 fn serialize_struct_variant(
351 self,
352 name: &'static str,
353 _variant_index: u32,
354 _variant: &'static str,
355 _len: usize,
356 ) -> Result<Self::SerializeStructVariant, crate::ser::Error> {
357 Err(crate::ser::Error::unsupported_type(Some(name)))
358 }
359}
360
361pub(crate) struct SerializeMap {
362 map: Table,
363 next_key: Option<String>,
364}
365
366impl SerializeMap {
367 pub(crate) fn new() -> Self {
368 Self {
369 map: Table::new(),
370 next_key: None,
371 }
372 }
373
374 pub(crate) fn with_capacity(capacity: usize) -> Self {
375 Self {
376 map: Table::with_capacity(capacity),
377 next_key: None,
378 }
379 }
380}
381
382impl ser::SerializeMap for SerializeMap {
383 type Ok = Table;
384 type Error = crate::ser::Error;
385
386 fn serialize_key<T>(&mut self, key: &T) -> Result<(), crate::ser::Error>
387 where
388 T: ser::Serialize + ?Sized,
389 {
390 match Value::try_from(key)? {
391 Value::String(s) => self.next_key = Some(s),
392 _ => return Err(crate::ser::Error::key_not_string()),
393 };
394 Ok(())
395 }
396
397 fn serialize_value<T>(&mut self, value: &T) -> Result<(), crate::ser::Error>
398 where
399 T: ser::Serialize + ?Sized,
400 {
401 let key = self.next_key.take();
402 let key = key.expect("serialize_value called before serialize_key");
403 match Value::try_from(value) {
404 Ok(value) => {
405 self.map.insert(key, value);
406 }
407 Err(crate::ser::Error {
408 inner: crate::ser::ErrorInner::UnsupportedNone,
409 }) => {}
410 Err(e) => return Err(e),
411 }
412 Ok(())
413 }
414
415 fn end(self) -> Result<Table, crate::ser::Error> {
416 Ok(self.map)
417 }
418}
419
420impl ser::SerializeStruct for SerializeMap {
421 type Ok = Table;
422 type Error = crate::ser::Error;
423
424 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), crate::ser::Error>
425 where
426 T: ser::Serialize + ?Sized,
427 {
428 ser::SerializeMap::serialize_key(self, key)?;
429 ser::SerializeMap::serialize_value(self, value)
430 }
431
432 fn end(self) -> Result<Table, crate::ser::Error> {
433 ser::SerializeMap::end(self)
434 }
435}