1 | use std::fmt; |
2 | |
3 | use serde::de; |
4 | use serde::ser; |
5 | |
6 | use crate::map::Map; |
7 | use crate::Value; |
8 | |
9 | /// Type representing a TOML table, payload of the `Value::Table` variant. |
10 | /// By default it is backed by a BTreeMap, enable the `preserve_order` feature |
11 | /// to use a LinkedHashMap instead. |
12 | pub type Table = Map<String, Value>; |
13 | |
14 | impl Table { |
15 | /// Convert a `T` into `toml::Table`. |
16 | /// |
17 | /// This conversion can fail if `T`'s implementation of `Serialize` decides to |
18 | /// fail, or if `T` contains a map with non-string keys. |
19 | 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 | /// Interpret a `toml::Table` as an instance of type `T`. |
27 | /// |
28 | /// This conversion can fail if the structure of the `Table` does not match the structure |
29 | /// expected by `T`, for example if `T` is a bool which can't be mapped to a `Table`. It can |
30 | /// also fail if the structure is correct but `T`'s implementation of `Deserialize` decides |
31 | /// that something is wrong with the data, for example required struct fields are missing from |
32 | /// the TOML map or some number is too big to fit in the expected primitive type. |
33 | 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" )] |
42 | impl fmt::Display for Table { |
43 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
44 | crateString::ser::to_string(self) |
45 | .expect(msg:"Unable to represent value as string" ) |
46 | .fmt(f) |
47 | } |
48 | } |
49 | |
50 | #[cfg (feature = "parse" )] |
51 | impl 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 | |
58 | impl<'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 | // `None` is interpreted as a missing field so be sure to implement `Some` |
82 | // as a present field. |
83 | 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 | |
108 | impl<'de> de::IntoDeserializer<'de, crate::de::Error> for Table { |
109 | type Deserializer = Self; |
110 | |
111 | fn into_deserializer(self) -> Self { |
112 | self |
113 | } |
114 | } |
115 | |