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 /// 11 /// By default it entries are stored in 12 /// [lexicographic order](https://doc.rust-lang.org/std/primitive.str.html#impl-Ord-for-str) 13 /// of the keys. Enable the `preserve_order` feature to store entries in the order they appear in 14 /// the source file. 15 pub type Table = Map<String, Value>; 16 17 impl Table { 18 /// Convert a `T` into `toml::Table`. 19 /// 20 /// This conversion can fail if `T`'s implementation of `Serialize` decides to 21 /// fail, or if `T` contains a map with non-string keys. try_from<T>(value: T) -> Result<Self, crate::ser::Error> where T: ser::Serialize,22 pub fn try_from<T>(value: T) -> Result<Self, crate::ser::Error> 23 where 24 T: ser::Serialize, 25 { 26 value.serialize(crate::value::TableSerializer) 27 } 28 29 /// Interpret a `toml::Table` as an instance of type `T`. 30 /// 31 /// This conversion can fail if the structure of the `Table` does not match the structure 32 /// expected by `T`, for example if `T` is a bool which can't be mapped to a `Table`. It can 33 /// also fail if the structure is correct but `T`'s implementation of `Deserialize` decides 34 /// that something is wrong with the data, for example required struct fields are missing from 35 /// the TOML map or some number is too big to fit in the expected primitive type. try_into<'de, T>(self) -> Result<T, crate::de::Error> where T: de::Deserialize<'de>,36 pub fn try_into<'de, T>(self) -> Result<T, crate::de::Error> 37 where 38 T: de::Deserialize<'de>, 39 { 40 de::Deserialize::deserialize(self) 41 } 42 } 43 44 #[cfg(feature = "display")] 45 impl fmt::Display for Table { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 47 crate::ser::to_string(self) 48 .expect("Unable to represent value as string") 49 .fmt(f) 50 } 51 } 52 53 #[cfg(feature = "parse")] 54 impl std::str::FromStr for Table { 55 type Err = crate::de::Error; from_str(s: &str) -> Result<Self, Self::Err>56 fn from_str(s: &str) -> Result<Self, Self::Err> { 57 crate::from_str(s) 58 } 59 } 60 61 impl<'de> de::Deserializer<'de> for Table { 62 type Error = crate::de::Error; 63 deserialize_any<V>(self, visitor: V) -> Result<V::Value, crate::de::Error> where V: de::Visitor<'de>,64 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, crate::de::Error> 65 where 66 V: de::Visitor<'de>, 67 { 68 Value::Table(self).deserialize_any(visitor) 69 } 70 71 #[inline] deserialize_enum<V>( self, name: &'static str, variants: &'static [&'static str], visitor: V, ) -> Result<V::Value, crate::de::Error> where V: de::Visitor<'de>,72 fn deserialize_enum<V>( 73 self, 74 name: &'static str, 75 variants: &'static [&'static str], 76 visitor: V, 77 ) -> Result<V::Value, crate::de::Error> 78 where 79 V: de::Visitor<'de>, 80 { 81 Value::Table(self).deserialize_enum(name, variants, visitor) 82 } 83 84 // `None` is interpreted as a missing field so be sure to implement `Some` 85 // as a present field. deserialize_option<V>(self, visitor: V) -> Result<V::Value, crate::de::Error> where V: de::Visitor<'de>,86 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, crate::de::Error> 87 where 88 V: de::Visitor<'de>, 89 { 90 Value::Table(self).deserialize_option(visitor) 91 } 92 deserialize_newtype_struct<V>( self, name: &'static str, visitor: V, ) -> Result<V::Value, crate::de::Error> where V: de::Visitor<'de>,93 fn deserialize_newtype_struct<V>( 94 self, 95 name: &'static str, 96 visitor: V, 97 ) -> Result<V::Value, crate::de::Error> 98 where 99 V: de::Visitor<'de>, 100 { 101 Value::Table(self).deserialize_newtype_struct(name, visitor) 102 } 103 104 serde::forward_to_deserialize_any! { 105 bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit seq 106 bytes byte_buf map unit_struct tuple_struct struct 107 tuple ignored_any identifier 108 } 109 } 110 111 impl<'de> de::IntoDeserializer<'de, crate::de::Error> for Table { 112 type Deserializer = Self; 113 into_deserializer(self) -> Self114 fn into_deserializer(self) -> Self { 115 self 116 } 117 } 118