| 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 | /// use serde::Serialize; |
| 17 | /// |
| 18 | /// #[derive(Serialize)] |
| 19 | /// struct Config { |
| 20 | /// database: Database, |
| 21 | /// } |
| 22 | /// |
| 23 | /// #[derive(Serialize)] |
| 24 | /// struct Database { |
| 25 | /// ip: String, |
| 26 | /// port: Vec<u16>, |
| 27 | /// connection_max: u32, |
| 28 | /// enabled: bool, |
| 29 | /// } |
| 30 | /// |
| 31 | /// let config = Config { |
| 32 | /// database: Database { |
| 33 | /// ip: "192.168.1.1" .to_string(), |
| 34 | /// port: vec![8001, 8002, 8003], |
| 35 | /// connection_max: 5000, |
| 36 | /// enabled: false, |
| 37 | /// }, |
| 38 | /// }; |
| 39 | /// |
| 40 | /// let value = serde::Serialize::serialize( |
| 41 | /// &config, |
| 42 | /// toml_edit::ser::ValueSerializer::new() |
| 43 | /// ).unwrap(); |
| 44 | /// println!("{}" , value) |
| 45 | /// ``` |
| 46 | #[derive (Default)] |
| 47 | #[non_exhaustive ] |
| 48 | pub struct ValueSerializer {} |
| 49 | |
| 50 | impl ValueSerializer { |
| 51 | /// Creates a new serializer generate a TOML document. |
| 52 | pub fn new() -> Self { |
| 53 | Self {} |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | impl serde::ser::Serializer for ValueSerializer { |
| 58 | type Ok = crate::Value; |
| 59 | type Error = Error; |
| 60 | type SerializeSeq = super::SerializeValueArray; |
| 61 | type SerializeTuple = super::SerializeValueArray; |
| 62 | type SerializeTupleStruct = super::SerializeValueArray; |
| 63 | type SerializeTupleVariant = super::SerializeValueArray; |
| 64 | type SerializeMap = super::SerializeMap; |
| 65 | type SerializeStruct = super::SerializeMap; |
| 66 | type SerializeStructVariant = serde::ser::Impossible<Self::Ok, Self::Error>; |
| 67 | |
| 68 | fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> { |
| 69 | Ok(v.into()) |
| 70 | } |
| 71 | |
| 72 | fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> { |
| 73 | self.serialize_i64(v as i64) |
| 74 | } |
| 75 | |
| 76 | fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> { |
| 77 | self.serialize_i64(v as i64) |
| 78 | } |
| 79 | |
| 80 | fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> { |
| 81 | self.serialize_i64(v as i64) |
| 82 | } |
| 83 | |
| 84 | fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> { |
| 85 | Ok(v.into()) |
| 86 | } |
| 87 | |
| 88 | fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> { |
| 89 | self.serialize_i64(v as i64) |
| 90 | } |
| 91 | |
| 92 | fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> { |
| 93 | self.serialize_i64(v as i64) |
| 94 | } |
| 95 | |
| 96 | fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> { |
| 97 | self.serialize_i64(v as i64) |
| 98 | } |
| 99 | |
| 100 | fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> { |
| 101 | let v: i64 = v |
| 102 | .try_into() |
| 103 | .map_err(|_err| Error::OutOfRange(Some("u64" )))?; |
| 104 | self.serialize_i64(v) |
| 105 | } |
| 106 | |
| 107 | fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> { |
| 108 | self.serialize_f64(v as f64) |
| 109 | } |
| 110 | |
| 111 | fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> { |
| 112 | Ok(v.into()) |
| 113 | } |
| 114 | |
| 115 | fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> { |
| 116 | let mut buf = [0; 4]; |
| 117 | self.serialize_str(v.encode_utf8(&mut buf)) |
| 118 | } |
| 119 | |
| 120 | fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> { |
| 121 | Ok(v.into()) |
| 122 | } |
| 123 | |
| 124 | fn serialize_bytes(self, value: &[u8]) -> Result<Self::Ok, Self::Error> { |
| 125 | use serde::ser::Serialize; |
| 126 | value.serialize(self) |
| 127 | } |
| 128 | |
| 129 | fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
| 130 | Err(Error::UnsupportedNone) |
| 131 | } |
| 132 | |
| 133 | fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error> |
| 134 | where |
| 135 | T: serde::ser::Serialize, |
| 136 | { |
| 137 | value.serialize(self) |
| 138 | } |
| 139 | |
| 140 | fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
| 141 | Err(Error::UnsupportedType(Some("unit" ))) |
| 142 | } |
| 143 | |
| 144 | fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> { |
| 145 | Err(Error::UnsupportedType(Some(name))) |
| 146 | } |
| 147 | |
| 148 | fn serialize_unit_variant( |
| 149 | self, |
| 150 | _name: &'static str, |
| 151 | _variant_index: u32, |
| 152 | variant: &'static str, |
| 153 | ) -> Result<Self::Ok, Self::Error> { |
| 154 | self.serialize_str(variant) |
| 155 | } |
| 156 | |
| 157 | fn serialize_newtype_struct<T: ?Sized>( |
| 158 | self, |
| 159 | _name: &'static str, |
| 160 | value: &T, |
| 161 | ) -> Result<Self::Ok, Self::Error> |
| 162 | where |
| 163 | T: serde::ser::Serialize, |
| 164 | { |
| 165 | value.serialize(self) |
| 166 | } |
| 167 | |
| 168 | fn serialize_newtype_variant<T: ?Sized>( |
| 169 | self, |
| 170 | _name: &'static str, |
| 171 | _variant_index: u32, |
| 172 | variant: &'static str, |
| 173 | value: &T, |
| 174 | ) -> Result<Self::Ok, Self::Error> |
| 175 | where |
| 176 | T: serde::ser::Serialize, |
| 177 | { |
| 178 | let value = value.serialize(self)?; |
| 179 | let mut table = crate::InlineTable::new(); |
| 180 | table.insert(variant, value); |
| 181 | Ok(table.into()) |
| 182 | } |
| 183 | |
| 184 | fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
| 185 | let serializer = match len { |
| 186 | Some(len) => super::SerializeValueArray::with_capacity(len), |
| 187 | None => super::SerializeValueArray::new(), |
| 188 | }; |
| 189 | Ok(serializer) |
| 190 | } |
| 191 | |
| 192 | fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> { |
| 193 | self.serialize_seq(Some(len)) |
| 194 | } |
| 195 | |
| 196 | fn serialize_tuple_struct( |
| 197 | self, |
| 198 | _name: &'static str, |
| 199 | len: usize, |
| 200 | ) -> Result<Self::SerializeTupleStruct, Self::Error> { |
| 201 | self.serialize_seq(Some(len)) |
| 202 | } |
| 203 | |
| 204 | fn serialize_tuple_variant( |
| 205 | self, |
| 206 | _name: &'static str, |
| 207 | _variant_index: u32, |
| 208 | _variant: &'static str, |
| 209 | len: usize, |
| 210 | ) -> Result<Self::SerializeTupleVariant, Self::Error> { |
| 211 | self.serialize_seq(Some(len)) |
| 212 | } |
| 213 | |
| 214 | fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
| 215 | let serializer = match len { |
| 216 | Some(len) => super::SerializeMap::table_with_capacity(len), |
| 217 | None => super::SerializeMap::table(), |
| 218 | }; |
| 219 | Ok(serializer) |
| 220 | } |
| 221 | |
| 222 | fn serialize_struct( |
| 223 | self, |
| 224 | name: &'static str, |
| 225 | len: usize, |
| 226 | ) -> Result<Self::SerializeStruct, Self::Error> { |
| 227 | if name == toml_datetime::__unstable::NAME { |
| 228 | Ok(super::SerializeMap::datetime()) |
| 229 | } else { |
| 230 | self.serialize_map(Some(len)) |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | fn serialize_struct_variant( |
| 235 | self, |
| 236 | name: &'static str, |
| 237 | _variant_index: u32, |
| 238 | _variant: &'static str, |
| 239 | _len: usize, |
| 240 | ) -> Result<Self::SerializeStructVariant, Self::Error> { |
| 241 | Err(Error::UnsupportedType(Some(name))) |
| 242 | } |
| 243 | } |
| 244 | |