1use serde::de;
2use serde::ser;
3
4use crate::map::Map;
5use crate::Value;
6
7pub type Table = Map<String, Value>;
14
15impl Table {
16 pub fn try_from<T>(value: T) -> Result<Self, crate::ser::Error>
21 where
22 T: ser::Serialize,
23 {
24 value.serialize(crate::value::TableSerializer)
25 }
26
27 pub fn try_into<'de, T>(self) -> Result<T, crate::de::Error>
35 where
36 T: de::Deserialize<'de>,
37 {
38 de::Deserialize::deserialize(self)
39 }
40}
41
42#[cfg(feature = "display")]
43impl std::fmt::Display for Table {
44 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45 crate::ser::to_string(self)
46 .expect("Unable to represent value as string")
47 .fmt(f)
48 }
49}
50
51#[cfg(feature = "parse")]
52impl std::str::FromStr for Table {
53 type Err = crate::de::Error;
54 fn from_str(s: &str) -> Result<Self, Self::Err> {
55 crate::from_str(s)
56 }
57}
58
59impl<'de> de::Deserializer<'de> for Table {
60 type Error = crate::de::Error;
61
62 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, crate::de::Error>
63 where
64 V: de::Visitor<'de>,
65 {
66 Value::Table(self).deserialize_any(visitor)
67 }
68
69 #[inline]
70 fn deserialize_enum<V>(
71 self,
72 name: &'static str,
73 variants: &'static [&'static str],
74 visitor: V,
75 ) -> Result<V::Value, crate::de::Error>
76 where
77 V: de::Visitor<'de>,
78 {
79 Value::Table(self).deserialize_enum(name, variants, visitor)
80 }
81
82 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, crate::de::Error>
85 where
86 V: de::Visitor<'de>,
87 {
88 Value::Table(self).deserialize_option(visitor)
89 }
90
91 fn deserialize_newtype_struct<V>(
92 self,
93 name: &'static str,
94 visitor: V,
95 ) -> Result<V::Value, crate::de::Error>
96 where
97 V: de::Visitor<'de>,
98 {
99 Value::Table(self).deserialize_newtype_struct(name, visitor)
100 }
101
102 serde::forward_to_deserialize_any! {
103 bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit seq
104 bytes byte_buf map unit_struct tuple_struct struct
105 tuple ignored_any identifier
106 }
107}
108
109impl de::IntoDeserializer<'_, crate::de::Error> for Table {
110 type Deserializer = Self;
111
112 fn into_deserializer(self) -> Self {
113 self
114 }
115}