| 1 | use super::Error; |
| 2 | |
| 3 | /// Serialization for TOML [values][crate::Value]. |
| 4 | /// |
| 5 | /// This structure implements serialization support for TOML to serialize an |
| 6 | /// arbitrary type to TOML. Note that the TOML format does not support all |
| 7 | /// datatypes in Rust, such as enums, tuples, and tuple structs. These types |
| 8 | /// will generate an error when serialized. |
| 9 | /// |
| 10 | /// Currently a serializer always writes its output to an in-memory `String`, |
| 11 | /// which is passed in when creating the serializer itself. |
| 12 | /// |
| 13 | /// # Examples |
| 14 | /// |
| 15 | /// ``` |
| 16 | /// # #[cfg (feature = "parse" )] { |
| 17 | /// # #[cfg (feature = "display" )] { |
| 18 | /// use serde::Serialize; |
| 19 | /// |
| 20 | /// #[derive(Serialize)] |
| 21 | /// struct Config { |
| 22 | /// database: Database, |
| 23 | /// } |
| 24 | /// |
| 25 | /// #[derive(Serialize)] |
| 26 | /// struct Database { |
| 27 | /// ip: String, |
| 28 | /// port: Vec<u16>, |
| 29 | /// connection_max: u32, |
| 30 | /// enabled: bool, |
| 31 | /// } |
| 32 | /// |
| 33 | /// let config = Config { |
| 34 | /// database: Database { |
| 35 | /// ip: "192.168.1.1" .to_string(), |
| 36 | /// port: vec![8001, 8002, 8003], |
| 37 | /// connection_max: 5000, |
| 38 | /// enabled: false, |
| 39 | /// }, |
| 40 | /// }; |
| 41 | /// |
| 42 | /// let value = serde::Serialize::serialize( |
| 43 | /// &config, |
| 44 | /// toml_edit::ser::ValueSerializer::new() |
| 45 | /// ).unwrap(); |
| 46 | /// println!("{}" , value) |
| 47 | /// # } |
| 48 | /// # } |
| 49 | /// ``` |
| 50 | #[derive (Default)] |
| 51 | #[non_exhaustive ] |
| 52 | pub struct ValueSerializer {} |
| 53 | |
| 54 | impl ValueSerializer { |
| 55 | /// Creates a new serializer generate a TOML document. |
| 56 | pub fn new() -> Self { |
| 57 | Self {} |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | impl serde::ser::Serializer for ValueSerializer { |
| 62 | type Ok = crate::Value; |
| 63 | type Error = Error; |
| 64 | type SerializeSeq = super::SerializeValueArray; |
| 65 | type SerializeTuple = super::SerializeValueArray; |
| 66 | type SerializeTupleStruct = super::SerializeValueArray; |
| 67 | type SerializeTupleVariant = super::SerializeTupleVariant; |
| 68 | type SerializeMap = super::SerializeMap; |
| 69 | type SerializeStruct = super::SerializeMap; |
| 70 | type SerializeStructVariant = super::SerializeStructVariant; |
| 71 | |
| 72 | fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> { |
| 73 | Ok(v.into()) |
| 74 | } |
| 75 | |
| 76 | fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> { |
| 77 | self.serialize_i64(v as i64) |
| 78 | } |
| 79 | |
| 80 | fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> { |
| 81 | self.serialize_i64(v as i64) |
| 82 | } |
| 83 | |
| 84 | fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> { |
| 85 | self.serialize_i64(v as i64) |
| 86 | } |
| 87 | |
| 88 | fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> { |
| 89 | Ok(v.into()) |
| 90 | } |
| 91 | |
| 92 | fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> { |
| 93 | self.serialize_i64(v as i64) |
| 94 | } |
| 95 | |
| 96 | fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> { |
| 97 | self.serialize_i64(v as i64) |
| 98 | } |
| 99 | |
| 100 | fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> { |
| 101 | self.serialize_i64(v as i64) |
| 102 | } |
| 103 | |
| 104 | fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> { |
| 105 | let v: i64 = v |
| 106 | .try_into() |
| 107 | .map_err(|_err| Error::OutOfRange(Some("u64" )))?; |
| 108 | self.serialize_i64(v) |
| 109 | } |
| 110 | |
| 111 | fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> { |
| 112 | self.serialize_f64(v as f64) |
| 113 | } |
| 114 | |
| 115 | fn serialize_f64(self, mut v: f64) -> Result<Self::Ok, Self::Error> { |
| 116 | // Discard sign of NaN when serialized using Serde. |
| 117 | // |
| 118 | // In all likelihood the sign of NaNs is not meaningful in the user's |
| 119 | // program. Ending up with `-nan` in the TOML document would usually be |
| 120 | // surprising and undesirable, when the sign of the NaN was not |
| 121 | // intentionally controlled by the caller, or may even be |
| 122 | // nondeterministic if it comes from arithmetic operations or a cast. |
| 123 | if v.is_nan() { |
| 124 | v = v.copysign(1.0); |
| 125 | } |
| 126 | Ok(v.into()) |
| 127 | } |
| 128 | |
| 129 | fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> { |
| 130 | let mut buf = [0; 4]; |
| 131 | self.serialize_str(v.encode_utf8(&mut buf)) |
| 132 | } |
| 133 | |
| 134 | fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> { |
| 135 | Ok(v.into()) |
| 136 | } |
| 137 | |
| 138 | fn serialize_bytes(self, value: &[u8]) -> Result<Self::Ok, Self::Error> { |
| 139 | use serde::ser::Serialize; |
| 140 | value.serialize(self) |
| 141 | } |
| 142 | |
| 143 | fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
| 144 | Err(Error::UnsupportedNone) |
| 145 | } |
| 146 | |
| 147 | fn serialize_some<T>(self, value: &T) -> Result<Self::Ok, Self::Error> |
| 148 | where |
| 149 | T: serde::ser::Serialize + ?Sized, |
| 150 | { |
| 151 | value.serialize(self) |
| 152 | } |
| 153 | |
| 154 | fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
| 155 | Err(Error::UnsupportedType(Some("unit" ))) |
| 156 | } |
| 157 | |
| 158 | fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> { |
| 159 | Err(Error::UnsupportedType(Some(name))) |
| 160 | } |
| 161 | |
| 162 | fn serialize_unit_variant( |
| 163 | self, |
| 164 | _name: &'static str, |
| 165 | _variant_index: u32, |
| 166 | variant: &'static str, |
| 167 | ) -> Result<Self::Ok, Self::Error> { |
| 168 | self.serialize_str(variant) |
| 169 | } |
| 170 | |
| 171 | fn serialize_newtype_struct<T>( |
| 172 | self, |
| 173 | _name: &'static str, |
| 174 | value: &T, |
| 175 | ) -> Result<Self::Ok, Self::Error> |
| 176 | where |
| 177 | T: serde::ser::Serialize + ?Sized, |
| 178 | { |
| 179 | value.serialize(self) |
| 180 | } |
| 181 | |
| 182 | fn serialize_newtype_variant<T>( |
| 183 | self, |
| 184 | _name: &'static str, |
| 185 | _variant_index: u32, |
| 186 | variant: &'static str, |
| 187 | value: &T, |
| 188 | ) -> Result<Self::Ok, Self::Error> |
| 189 | where |
| 190 | T: serde::ser::Serialize + ?Sized, |
| 191 | { |
| 192 | let value = value.serialize(self)?; |
| 193 | let mut table = crate::InlineTable::new(); |
| 194 | table.insert(variant, value); |
| 195 | Ok(table.into()) |
| 196 | } |
| 197 | |
| 198 | fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
| 199 | let serializer = match len { |
| 200 | Some(len) => super::SerializeValueArray::with_capacity(len), |
| 201 | None => super::SerializeValueArray::new(), |
| 202 | }; |
| 203 | Ok(serializer) |
| 204 | } |
| 205 | |
| 206 | fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> { |
| 207 | self.serialize_seq(Some(len)) |
| 208 | } |
| 209 | |
| 210 | fn serialize_tuple_struct( |
| 211 | self, |
| 212 | _name: &'static str, |
| 213 | len: usize, |
| 214 | ) -> Result<Self::SerializeTupleStruct, Self::Error> { |
| 215 | self.serialize_seq(Some(len)) |
| 216 | } |
| 217 | |
| 218 | fn serialize_tuple_variant( |
| 219 | self, |
| 220 | _name: &'static str, |
| 221 | _variant_index: u32, |
| 222 | variant: &'static str, |
| 223 | len: usize, |
| 224 | ) -> Result<Self::SerializeTupleVariant, Self::Error> { |
| 225 | Ok(super::SerializeTupleVariant::tuple(variant, len)) |
| 226 | } |
| 227 | |
| 228 | fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
| 229 | let serializer = match len { |
| 230 | Some(len) => super::SerializeMap::table_with_capacity(len), |
| 231 | None => super::SerializeMap::table(), |
| 232 | }; |
| 233 | Ok(serializer) |
| 234 | } |
| 235 | |
| 236 | fn serialize_struct( |
| 237 | self, |
| 238 | name: &'static str, |
| 239 | len: usize, |
| 240 | ) -> Result<Self::SerializeStruct, Self::Error> { |
| 241 | if name == toml_datetime::__unstable::NAME { |
| 242 | Ok(super::SerializeMap::datetime()) |
| 243 | } else { |
| 244 | self.serialize_map(Some(len)) |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | fn serialize_struct_variant( |
| 249 | self, |
| 250 | _name: &'static str, |
| 251 | _variant_index: u32, |
| 252 | variant: &'static str, |
| 253 | len: usize, |
| 254 | ) -> Result<Self::SerializeStructVariant, Self::Error> { |
| 255 | Ok(super::SerializeStructVariant::struct_(variant, len)) |
| 256 | } |
| 257 | } |
| 258 | |