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