| 1 | use super::{Error, KeySerializer, SerializeValueArray, ValueSerializer}; |
| 2 | |
| 3 | #[doc (hidden)] |
| 4 | #[allow (clippy::large_enum_variant)] |
| 5 | pub enum SerializeMap { |
| 6 | Datetime(SerializeDatetime), |
| 7 | Table(SerializeInlineTable), |
| 8 | } |
| 9 | |
| 10 | impl SerializeMap { |
| 11 | pub(crate) fn table() -> Self { |
| 12 | Self::Table(SerializeInlineTable::new()) |
| 13 | } |
| 14 | |
| 15 | pub(crate) fn table_with_capacity(len: usize) -> Self { |
| 16 | Self::Table(SerializeInlineTable::with_capacity(len)) |
| 17 | } |
| 18 | |
| 19 | pub(crate) fn datetime() -> Self { |
| 20 | Self::Datetime(SerializeDatetime::new()) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | impl serde::ser::SerializeMap for SerializeMap { |
| 25 | type Ok = crate::Value; |
| 26 | type Error = Error; |
| 27 | |
| 28 | fn serialize_key<T>(&mut self, input: &T) -> Result<(), Self::Error> |
| 29 | where |
| 30 | T: serde::ser::Serialize + ?Sized, |
| 31 | { |
| 32 | match self { |
| 33 | Self::Datetime(s) => s.serialize_key(input), |
| 34 | Self::Table(s) => s.serialize_key(input), |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error> |
| 39 | where |
| 40 | T: serde::ser::Serialize + ?Sized, |
| 41 | { |
| 42 | match self { |
| 43 | Self::Datetime(s) => s.serialize_value(value), |
| 44 | Self::Table(s) => s.serialize_value(value), |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | fn end(self) -> Result<Self::Ok, Self::Error> { |
| 49 | match self { |
| 50 | Self::Datetime(s) => s.end().map(|items| items.into()), |
| 51 | Self::Table(s) => s.end().map(|items| items.into()), |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | impl serde::ser::SerializeStruct for SerializeMap { |
| 57 | type Ok = crate::Value; |
| 58 | type Error = Error; |
| 59 | |
| 60 | fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> |
| 61 | where |
| 62 | T: serde::ser::Serialize + ?Sized, |
| 63 | { |
| 64 | match self { |
| 65 | Self::Datetime(s: &mut SerializeDatetime) => s.serialize_field(key, value), |
| 66 | Self::Table(s: &mut SerializeInlineTable) => s.serialize_field(key, value), |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | fn end(self) -> Result<Self::Ok, Self::Error> { |
| 71 | match self { |
| 72 | Self::Datetime(s: SerializeDatetime) => s.end().map(|items: Datetime| items.into()), |
| 73 | Self::Table(s: SerializeInlineTable) => s.end().map(|items: InlineTable| items.into()), |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | #[doc (hidden)] |
| 79 | pub struct SerializeDatetime { |
| 80 | value: Option<crate::Datetime>, |
| 81 | } |
| 82 | |
| 83 | impl SerializeDatetime { |
| 84 | pub(crate) fn new() -> Self { |
| 85 | Self { value: None } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | impl serde::ser::SerializeMap for SerializeDatetime { |
| 90 | type Ok = crate::Datetime; |
| 91 | type Error = Error; |
| 92 | |
| 93 | fn serialize_key<T>(&mut self, _input: &T) -> Result<(), Self::Error> |
| 94 | where |
| 95 | T: serde::ser::Serialize + ?Sized, |
| 96 | { |
| 97 | unreachable!("datetimes should only be serialized as structs, not maps" ) |
| 98 | } |
| 99 | |
| 100 | fn serialize_value<T>(&mut self, _value: &T) -> Result<(), Self::Error> |
| 101 | where |
| 102 | T: serde::ser::Serialize + ?Sized, |
| 103 | { |
| 104 | unreachable!("datetimes should only be serialized as structs, not maps" ) |
| 105 | } |
| 106 | |
| 107 | fn end(self) -> Result<Self::Ok, Self::Error> { |
| 108 | unreachable!("datetimes should only be serialized as structs, not maps" ) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | impl serde::ser::SerializeStruct for SerializeDatetime { |
| 113 | type Ok = crate::Datetime; |
| 114 | type Error = Error; |
| 115 | |
| 116 | fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> |
| 117 | where |
| 118 | T: serde::ser::Serialize + ?Sized, |
| 119 | { |
| 120 | if key == toml_datetime::__unstable::FIELD { |
| 121 | self.value = Some(value.serialize(serializer:DatetimeFieldSerializer::default())?); |
| 122 | } |
| 123 | |
| 124 | Ok(()) |
| 125 | } |
| 126 | |
| 127 | fn end(self) -> Result<Self::Ok, Self::Error> { |
| 128 | self.value.ok_or(err:Error::UnsupportedNone) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | #[doc (hidden)] |
| 133 | pub struct SerializeInlineTable { |
| 134 | items: crate::table::KeyValuePairs, |
| 135 | key: Option<crate::Key>, |
| 136 | } |
| 137 | |
| 138 | impl SerializeInlineTable { |
| 139 | pub(crate) fn new() -> Self { |
| 140 | Self { |
| 141 | items: Default::default(), |
| 142 | key: Default::default(), |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | pub(crate) fn with_capacity(len: usize) -> Self { |
| 147 | let mut s: SerializeInlineTable = Self::new(); |
| 148 | s.items.reserve(additional:len); |
| 149 | s |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | impl serde::ser::SerializeMap for SerializeInlineTable { |
| 154 | type Ok = crate::InlineTable; |
| 155 | type Error = Error; |
| 156 | |
| 157 | fn serialize_key<T>(&mut self, input: &T) -> Result<(), Self::Error> |
| 158 | where |
| 159 | T: serde::ser::Serialize + ?Sized, |
| 160 | { |
| 161 | self.key = Some(input.serialize(KeySerializer)?); |
| 162 | Ok(()) |
| 163 | } |
| 164 | |
| 165 | fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error> |
| 166 | where |
| 167 | T: serde::ser::Serialize + ?Sized, |
| 168 | { |
| 169 | let mut value_serializer = MapValueSerializer::new(); |
| 170 | let res = value.serialize(&mut value_serializer); |
| 171 | match res { |
| 172 | Ok(item) => { |
| 173 | let key = self.key.take().unwrap(); |
| 174 | let item = crate::Item::Value(item); |
| 175 | self.items.insert(key, item); |
| 176 | } |
| 177 | Err(e) => { |
| 178 | if !(e == Error::UnsupportedNone && value_serializer.is_none) { |
| 179 | return Err(e); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | Ok(()) |
| 184 | } |
| 185 | |
| 186 | fn end(self) -> Result<Self::Ok, Self::Error> { |
| 187 | Ok(crate::InlineTable::with_pairs(self.items)) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | impl serde::ser::SerializeStruct for SerializeInlineTable { |
| 192 | type Ok = crate::InlineTable; |
| 193 | type Error = Error; |
| 194 | |
| 195 | fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> |
| 196 | where |
| 197 | T: serde::ser::Serialize + ?Sized, |
| 198 | { |
| 199 | let mut value_serializer = MapValueSerializer::new(); |
| 200 | let res = value.serialize(&mut value_serializer); |
| 201 | match res { |
| 202 | Ok(item) => { |
| 203 | let item = crate::Item::Value(item); |
| 204 | self.items.insert(crate::Key::new(key), item); |
| 205 | } |
| 206 | Err(e) => { |
| 207 | if !(e == Error::UnsupportedNone && value_serializer.is_none) { |
| 208 | return Err(e); |
| 209 | } |
| 210 | } |
| 211 | }; |
| 212 | Ok(()) |
| 213 | } |
| 214 | |
| 215 | fn end(self) -> Result<Self::Ok, Self::Error> { |
| 216 | Ok(crate::InlineTable::with_pairs(self.items)) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | #[derive (Default)] |
| 221 | struct DatetimeFieldSerializer {} |
| 222 | |
| 223 | impl serde::ser::Serializer for DatetimeFieldSerializer { |
| 224 | type Ok = toml_datetime::Datetime; |
| 225 | type Error = Error; |
| 226 | type SerializeSeq = serde::ser::Impossible<Self::Ok, Self::Error>; |
| 227 | type SerializeTuple = serde::ser::Impossible<Self::Ok, Self::Error>; |
| 228 | type SerializeTupleStruct = serde::ser::Impossible<Self::Ok, Self::Error>; |
| 229 | type SerializeTupleVariant = serde::ser::Impossible<Self::Ok, Self::Error>; |
| 230 | type SerializeMap = serde::ser::Impossible<Self::Ok, Self::Error>; |
| 231 | type SerializeStruct = serde::ser::Impossible<Self::Ok, Self::Error>; |
| 232 | type SerializeStructVariant = serde::ser::Impossible<Self::Ok, Self::Error>; |
| 233 | |
| 234 | fn serialize_bool(self, _value: bool) -> Result<Self::Ok, Self::Error> { |
| 235 | Err(Error::DateInvalid) |
| 236 | } |
| 237 | |
| 238 | fn serialize_i8(self, _value: i8) -> Result<Self::Ok, Self::Error> { |
| 239 | Err(Error::DateInvalid) |
| 240 | } |
| 241 | |
| 242 | fn serialize_i16(self, _value: i16) -> Result<Self::Ok, Self::Error> { |
| 243 | Err(Error::DateInvalid) |
| 244 | } |
| 245 | |
| 246 | fn serialize_i32(self, _value: i32) -> Result<Self::Ok, Self::Error> { |
| 247 | Err(Error::DateInvalid) |
| 248 | } |
| 249 | |
| 250 | fn serialize_i64(self, _value: i64) -> Result<Self::Ok, Self::Error> { |
| 251 | Err(Error::DateInvalid) |
| 252 | } |
| 253 | |
| 254 | fn serialize_u8(self, _value: u8) -> Result<Self::Ok, Self::Error> { |
| 255 | Err(Error::DateInvalid) |
| 256 | } |
| 257 | |
| 258 | fn serialize_u16(self, _value: u16) -> Result<Self::Ok, Self::Error> { |
| 259 | Err(Error::DateInvalid) |
| 260 | } |
| 261 | |
| 262 | fn serialize_u32(self, _value: u32) -> Result<Self::Ok, Self::Error> { |
| 263 | Err(Error::DateInvalid) |
| 264 | } |
| 265 | |
| 266 | fn serialize_u64(self, _value: u64) -> Result<Self::Ok, Self::Error> { |
| 267 | Err(Error::DateInvalid) |
| 268 | } |
| 269 | |
| 270 | fn serialize_f32(self, _value: f32) -> Result<Self::Ok, Self::Error> { |
| 271 | Err(Error::DateInvalid) |
| 272 | } |
| 273 | |
| 274 | fn serialize_f64(self, _value: f64) -> Result<Self::Ok, Self::Error> { |
| 275 | Err(Error::DateInvalid) |
| 276 | } |
| 277 | |
| 278 | fn serialize_char(self, _value: char) -> Result<Self::Ok, Self::Error> { |
| 279 | Err(Error::DateInvalid) |
| 280 | } |
| 281 | |
| 282 | fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> { |
| 283 | v.parse::<toml_datetime::Datetime>().map_err(Error::custom) |
| 284 | } |
| 285 | |
| 286 | fn serialize_bytes(self, _value: &[u8]) -> Result<Self::Ok, Self::Error> { |
| 287 | Err(Error::DateInvalid) |
| 288 | } |
| 289 | |
| 290 | fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
| 291 | Err(Error::DateInvalid) |
| 292 | } |
| 293 | |
| 294 | fn serialize_some<T>(self, _value: &T) -> Result<Self::Ok, Self::Error> |
| 295 | where |
| 296 | T: serde::ser::Serialize + ?Sized, |
| 297 | { |
| 298 | Err(Error::DateInvalid) |
| 299 | } |
| 300 | |
| 301 | fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
| 302 | Err(Error::DateInvalid) |
| 303 | } |
| 304 | |
| 305 | fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> { |
| 306 | Err(Error::DateInvalid) |
| 307 | } |
| 308 | |
| 309 | fn serialize_unit_variant( |
| 310 | self, |
| 311 | _name: &'static str, |
| 312 | _variant_index: u32, |
| 313 | _variant: &'static str, |
| 314 | ) -> Result<Self::Ok, Self::Error> { |
| 315 | Err(Error::DateInvalid) |
| 316 | } |
| 317 | |
| 318 | fn serialize_newtype_struct<T>( |
| 319 | self, |
| 320 | _name: &'static str, |
| 321 | _value: &T, |
| 322 | ) -> Result<Self::Ok, Self::Error> |
| 323 | where |
| 324 | T: serde::ser::Serialize + ?Sized, |
| 325 | { |
| 326 | Err(Error::DateInvalid) |
| 327 | } |
| 328 | |
| 329 | fn serialize_newtype_variant<T>( |
| 330 | self, |
| 331 | _name: &'static str, |
| 332 | _variant_index: u32, |
| 333 | _variant: &'static str, |
| 334 | _value: &T, |
| 335 | ) -> Result<Self::Ok, Self::Error> |
| 336 | where |
| 337 | T: serde::ser::Serialize + ?Sized, |
| 338 | { |
| 339 | Err(Error::DateInvalid) |
| 340 | } |
| 341 | |
| 342 | fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
| 343 | Err(Error::DateInvalid) |
| 344 | } |
| 345 | |
| 346 | fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> { |
| 347 | Err(Error::DateInvalid) |
| 348 | } |
| 349 | |
| 350 | fn serialize_tuple_struct( |
| 351 | self, |
| 352 | _name: &'static str, |
| 353 | _len: usize, |
| 354 | ) -> Result<Self::SerializeTupleStruct, Self::Error> { |
| 355 | Err(Error::DateInvalid) |
| 356 | } |
| 357 | |
| 358 | fn serialize_tuple_variant( |
| 359 | self, |
| 360 | _name: &'static str, |
| 361 | _variant_index: u32, |
| 362 | _variant: &'static str, |
| 363 | _len: usize, |
| 364 | ) -> Result<Self::SerializeTupleVariant, Self::Error> { |
| 365 | Err(Error::DateInvalid) |
| 366 | } |
| 367 | |
| 368 | fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
| 369 | Err(Error::DateInvalid) |
| 370 | } |
| 371 | |
| 372 | fn serialize_struct( |
| 373 | self, |
| 374 | _name: &'static str, |
| 375 | _len: usize, |
| 376 | ) -> Result<Self::SerializeStruct, Self::Error> { |
| 377 | Err(Error::DateInvalid) |
| 378 | } |
| 379 | |
| 380 | fn serialize_struct_variant( |
| 381 | self, |
| 382 | _name: &'static str, |
| 383 | _variant_index: u32, |
| 384 | _variant: &'static str, |
| 385 | _len: usize, |
| 386 | ) -> Result<Self::SerializeStructVariant, Self::Error> { |
| 387 | Err(Error::DateInvalid) |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | #[derive (Default)] |
| 392 | struct MapValueSerializer { |
| 393 | is_none: bool, |
| 394 | } |
| 395 | |
| 396 | impl MapValueSerializer { |
| 397 | fn new() -> Self { |
| 398 | Self { is_none: false } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | impl serde::ser::Serializer for &mut MapValueSerializer { |
| 403 | type Ok = crate::Value; |
| 404 | type Error = Error; |
| 405 | type SerializeSeq = SerializeValueArray; |
| 406 | type SerializeTuple = SerializeValueArray; |
| 407 | type SerializeTupleStruct = SerializeValueArray; |
| 408 | type SerializeTupleVariant = SerializeTupleVariant; |
| 409 | type SerializeMap = SerializeMap; |
| 410 | type SerializeStruct = SerializeMap; |
| 411 | type SerializeStructVariant = SerializeStructVariant; |
| 412 | |
| 413 | fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> { |
| 414 | ValueSerializer::new().serialize_bool(v) |
| 415 | } |
| 416 | |
| 417 | fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> { |
| 418 | ValueSerializer::new().serialize_i8(v) |
| 419 | } |
| 420 | |
| 421 | fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> { |
| 422 | ValueSerializer::new().serialize_i16(v) |
| 423 | } |
| 424 | |
| 425 | fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> { |
| 426 | ValueSerializer::new().serialize_i32(v) |
| 427 | } |
| 428 | |
| 429 | fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> { |
| 430 | ValueSerializer::new().serialize_i64(v) |
| 431 | } |
| 432 | |
| 433 | fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> { |
| 434 | ValueSerializer::new().serialize_u8(v) |
| 435 | } |
| 436 | |
| 437 | fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> { |
| 438 | ValueSerializer::new().serialize_u16(v) |
| 439 | } |
| 440 | |
| 441 | fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> { |
| 442 | ValueSerializer::new().serialize_u32(v) |
| 443 | } |
| 444 | |
| 445 | fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> { |
| 446 | ValueSerializer::new().serialize_u64(v) |
| 447 | } |
| 448 | |
| 449 | fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> { |
| 450 | ValueSerializer::new().serialize_f32(v) |
| 451 | } |
| 452 | |
| 453 | fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> { |
| 454 | ValueSerializer::new().serialize_f64(v) |
| 455 | } |
| 456 | |
| 457 | fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> { |
| 458 | ValueSerializer::new().serialize_char(v) |
| 459 | } |
| 460 | |
| 461 | fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> { |
| 462 | ValueSerializer::new().serialize_str(v) |
| 463 | } |
| 464 | |
| 465 | fn serialize_bytes(self, value: &[u8]) -> Result<Self::Ok, Self::Error> { |
| 466 | ValueSerializer::new().serialize_bytes(value) |
| 467 | } |
| 468 | |
| 469 | fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
| 470 | self.is_none = true; |
| 471 | Err(Error::UnsupportedNone) |
| 472 | } |
| 473 | |
| 474 | fn serialize_some<T>(self, value: &T) -> Result<Self::Ok, Self::Error> |
| 475 | where |
| 476 | T: serde::ser::Serialize + ?Sized, |
| 477 | { |
| 478 | ValueSerializer::new().serialize_some(value) |
| 479 | } |
| 480 | |
| 481 | fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
| 482 | ValueSerializer::new().serialize_unit() |
| 483 | } |
| 484 | |
| 485 | fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> { |
| 486 | ValueSerializer::new().serialize_unit_struct(name) |
| 487 | } |
| 488 | |
| 489 | fn serialize_unit_variant( |
| 490 | self, |
| 491 | name: &'static str, |
| 492 | variant_index: u32, |
| 493 | variant: &'static str, |
| 494 | ) -> Result<Self::Ok, Self::Error> { |
| 495 | ValueSerializer::new().serialize_unit_variant(name, variant_index, variant) |
| 496 | } |
| 497 | |
| 498 | fn serialize_newtype_struct<T>( |
| 499 | self, |
| 500 | name: &'static str, |
| 501 | value: &T, |
| 502 | ) -> Result<Self::Ok, Self::Error> |
| 503 | where |
| 504 | T: serde::ser::Serialize + ?Sized, |
| 505 | { |
| 506 | ValueSerializer::new().serialize_newtype_struct(name, value) |
| 507 | } |
| 508 | |
| 509 | fn serialize_newtype_variant<T>( |
| 510 | self, |
| 511 | name: &'static str, |
| 512 | variant_index: u32, |
| 513 | variant: &'static str, |
| 514 | value: &T, |
| 515 | ) -> Result<Self::Ok, Self::Error> |
| 516 | where |
| 517 | T: serde::ser::Serialize + ?Sized, |
| 518 | { |
| 519 | ValueSerializer::new().serialize_newtype_variant(name, variant_index, variant, value) |
| 520 | } |
| 521 | |
| 522 | fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
| 523 | ValueSerializer::new().serialize_seq(len) |
| 524 | } |
| 525 | |
| 526 | fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> { |
| 527 | ValueSerializer::new().serialize_tuple(len) |
| 528 | } |
| 529 | |
| 530 | fn serialize_tuple_struct( |
| 531 | self, |
| 532 | name: &'static str, |
| 533 | len: usize, |
| 534 | ) -> Result<Self::SerializeTupleStruct, Self::Error> { |
| 535 | ValueSerializer::new().serialize_tuple_struct(name, len) |
| 536 | } |
| 537 | |
| 538 | fn serialize_tuple_variant( |
| 539 | self, |
| 540 | name: &'static str, |
| 541 | variant_index: u32, |
| 542 | variant: &'static str, |
| 543 | len: usize, |
| 544 | ) -> Result<Self::SerializeTupleVariant, Self::Error> { |
| 545 | ValueSerializer::new().serialize_tuple_variant(name, variant_index, variant, len) |
| 546 | } |
| 547 | |
| 548 | fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
| 549 | ValueSerializer::new().serialize_map(len) |
| 550 | } |
| 551 | |
| 552 | fn serialize_struct( |
| 553 | self, |
| 554 | name: &'static str, |
| 555 | len: usize, |
| 556 | ) -> Result<Self::SerializeStruct, Self::Error> { |
| 557 | ValueSerializer::new().serialize_struct(name, len) |
| 558 | } |
| 559 | |
| 560 | fn serialize_struct_variant( |
| 561 | self, |
| 562 | name: &'static str, |
| 563 | variant_index: u32, |
| 564 | variant: &'static str, |
| 565 | len: usize, |
| 566 | ) -> Result<Self::SerializeStructVariant, Self::Error> { |
| 567 | ValueSerializer::new().serialize_struct_variant(name, variant_index, variant, len) |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | pub(crate) type SerializeTupleVariant = SerializeVariant<SerializeValueArray>; |
| 572 | pub(crate) type SerializeStructVariant = SerializeVariant<SerializeMap>; |
| 573 | |
| 574 | pub struct SerializeVariant<T> { |
| 575 | variant: &'static str, |
| 576 | inner: T, |
| 577 | } |
| 578 | |
| 579 | impl SerializeVariant<SerializeValueArray> { |
| 580 | pub(crate) fn tuple(variant: &'static str, len: usize) -> Self { |
| 581 | Self { |
| 582 | variant, |
| 583 | inner: SerializeValueArray::with_capacity(len), |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | impl SerializeVariant<SerializeMap> { |
| 589 | pub(crate) fn struct_(variant: &'static str, len: usize) -> Self { |
| 590 | Self { |
| 591 | variant, |
| 592 | inner: SerializeMap::table_with_capacity(len), |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | impl serde::ser::SerializeTupleVariant for SerializeVariant<SerializeValueArray> { |
| 598 | type Ok = crate::Value; |
| 599 | type Error = Error; |
| 600 | |
| 601 | fn serialize_field<T>(&mut self, value: &T) -> Result<(), Error> |
| 602 | where |
| 603 | T: serde::ser::Serialize + ?Sized, |
| 604 | { |
| 605 | serde::ser::SerializeSeq::serialize_element(&mut self.inner, value) |
| 606 | } |
| 607 | |
| 608 | fn end(self) -> Result<Self::Ok, Self::Error> { |
| 609 | let inner: Value = serde::ser::SerializeSeq::end(self.inner)?; |
| 610 | let mut items: IndexMap = crate::table::KeyValuePairs::new(); |
| 611 | let value: Item = crate::Item::Value(inner); |
| 612 | items.insert(crate::Key::new(self.variant), value); |
| 613 | Ok(crate::Value::InlineTable(crate::InlineTable::with_pairs( |
| 614 | items, |
| 615 | ))) |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | impl serde::ser::SerializeStructVariant for SerializeVariant<SerializeMap> { |
| 620 | type Ok = crate::Value; |
| 621 | type Error = Error; |
| 622 | |
| 623 | #[inline ] |
| 624 | fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> |
| 625 | where |
| 626 | T: serde::ser::Serialize + ?Sized, |
| 627 | { |
| 628 | serde::ser::SerializeStruct::serialize_field(&mut self.inner, key, value) |
| 629 | } |
| 630 | |
| 631 | #[inline ] |
| 632 | fn end(self) -> Result<Self::Ok, Self::Error> { |
| 633 | let inner: Value = serde::ser::SerializeStruct::end(self.inner)?; |
| 634 | let mut items: IndexMap = crate::table::KeyValuePairs::new(); |
| 635 | let value: Item = crate::Item::Value(inner); |
| 636 | items.insert(crate::Key::new(self.variant), value); |
| 637 | Ok(crate::Value::InlineTable(crate::InlineTable::with_pairs( |
| 638 | items, |
| 639 | ))) |
| 640 | } |
| 641 | } |
| 642 | |