toml/
table.rs

1use serde::de;
2use serde::ser;
3
4use crate::map::Map;
5use crate::Value;
6
7/// Type representing a TOML table, payload of the `Value::Table` variant.
8///
9/// By default it entries are stored in
10/// [lexicographic order](https://doc.rust-lang.org/std/primitive.str.html#impl-Ord-for-str)
11/// of the keys. Enable the `preserve_order` feature to store entries in the order they appear in
12/// the source file.
13pub type Table = Map<String, Value>;
14
15impl Table {
16    /// Convert a `T` into `toml::Table`.
17    ///
18    /// This conversion can fail if `T`'s implementation of `Serialize` decides to
19    /// fail, or if `T` contains a map with non-string keys.
20    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    /// Interpret a `toml::Table` as an instance of type `T`.
28    ///
29    /// This conversion can fail if the structure of the `Table` does not match the structure
30    /// expected by `T`, for example if `T` is a bool which can't be mapped to a `Table`. It can
31    /// also fail if the structure is correct but `T`'s implementation of `Deserialize` decides
32    /// that something is wrong with the data, for example required struct fields are missing from
33    /// the TOML map or some number is too big to fit in the expected primitive type.
34    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    // `None` is interpreted as a missing field so be sure to implement `Some`
83    // as a present field.
84    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}