| 1 |
use crate::{ |
| 2 |
content::{ |
| 3 |
de::{Content as DeContent, ContentDeserializer}, |
| 4 |
ser::{Content as SerContent, ContentSerializer}, |
| 5 |
}, |
| 6 |
prelude::*, |
| 7 |
}; |
| 8 |
|
| 9 |
/// Convert `Vec` elements into key-value map entries |
| 10 |
/// |
| 11 |
/// This maps a single struct/tuple/etc. to a map entry. |
| 12 |
/// The map key is converted to a struct field. |
| 13 |
/// The other values will be mapped to the map value. |
| 14 |
/// |
| 15 |
/// The conversion supports structs, tuple structs, tuples, maps, and sequences. |
| 16 |
/// Structs need a field that is named `$key$` to be used as the map key. |
| 17 |
/// This can be done with the `#[serde(rename = "$key$")]` attribute. |
| 18 |
/// Maps similarly need a map-key that is named `$key$`. |
| 19 |
/// For tuples, tuple structs, and sequences the first element is used as the map key. |
| 20 |
/// |
| 21 |
/// # Examples |
| 22 |
/// |
| 23 |
/// ## Struct with String key in JSON |
| 24 |
/// |
| 25 |
/// ```rust |
| 26 |
/// # #[cfg(feature = "macros")] { |
| 27 |
/// # use serde::{Deserialize, Serialize}; |
| 28 |
/// use serde_with::{serde_as, KeyValueMap}; |
| 29 |
/// |
| 30 |
/// # #[derive(Debug, Clone, PartialEq, Eq)] |
| 31 |
/// #[derive(Serialize, Deserialize)] |
| 32 |
/// struct SimpleStruct { |
| 33 |
/// b: bool, |
| 34 |
/// // The field named `$key$` will become the map key |
| 35 |
/// #[serde(rename = "$key$")] |
| 36 |
/// id: String, |
| 37 |
/// i: i32, |
| 38 |
/// } |
| 39 |
/// |
| 40 |
/// #[serde_as] |
| 41 |
/// # #[derive(Debug, Clone, PartialEq, Eq)] |
| 42 |
/// #[derive(Serialize, Deserialize)] |
| 43 |
/// struct KVMap( |
| 44 |
/// #[serde_as(as = "KeyValueMap<_>")] |
| 45 |
/// Vec<SimpleStruct>, |
| 46 |
/// ); |
| 47 |
/// |
| 48 |
/// // --- |
| 49 |
/// |
| 50 |
/// // This will serialize this list of values |
| 51 |
/// let values = KVMap(vec![ |
| 52 |
/// SimpleStruct { |
| 53 |
/// b: false, |
| 54 |
/// id: "id-0000".to_string(), |
| 55 |
/// i: 123, |
| 56 |
/// }, |
| 57 |
/// SimpleStruct { |
| 58 |
/// b: true, |
| 59 |
/// id: "id-0001".to_string(), |
| 60 |
/// i: 555, |
| 61 |
/// }, |
| 62 |
/// SimpleStruct { |
| 63 |
/// b: false, |
| 64 |
/// id: "id-0002".to_string(), |
| 65 |
/// i: 987, |
| 66 |
/// }, |
| 67 |
/// ]); |
| 68 |
/// |
| 69 |
/// // into this JSON map |
| 70 |
/// let expected = |
| 71 |
/// r#"{ |
| 72 |
/// "id-0000": { |
| 73 |
/// "b": false, |
| 74 |
/// "i": 123 |
| 75 |
/// }, |
| 76 |
/// "id-0001": { |
| 77 |
/// "b": true, |
| 78 |
/// "i": 555 |
| 79 |
/// }, |
| 80 |
/// "id-0002": { |
| 81 |
/// "b": false, |
| 82 |
/// "i": 987 |
| 83 |
/// } |
| 84 |
/// }"#; |
| 85 |
/// |
| 86 |
/// // Both serialization and deserialization work flawlessly. |
| 87 |
/// let serialized = serde_json::to_string_pretty(&values).unwrap(); |
| 88 |
/// assert_eq!(expected, serialized); |
| 89 |
/// let deserialized: KVMap = serde_json::from_str(&serialized).unwrap(); |
| 90 |
/// assert_eq!(values, deserialized); |
| 91 |
/// # } |
| 92 |
/// ``` |
| 93 |
/// |
| 94 |
/// ## Tuple struct with complex key in YAML |
| 95 |
/// |
| 96 |
/// ```rust |
| 97 |
/// # #[cfg(feature = "macros")] { |
| 98 |
/// # use serde::{Deserialize, Serialize}; |
| 99 |
/// use serde_with::{serde_as, KeyValueMap}; |
| 100 |
/// use std::net::IpAddr; |
| 101 |
/// # use std::str::FromStr; |
| 102 |
/// |
| 103 |
/// # #[derive(Debug, Clone, PartialEq, Eq)] |
| 104 |
/// #[derive(Serialize, Deserialize)] |
| 105 |
/// struct TupleStruct ( |
| 106 |
/// // The first element in a tuple struct, tuple, or sequence becomes the map key |
| 107 |
/// (IpAddr, u8), |
| 108 |
/// bool, |
| 109 |
/// ); |
| 110 |
/// |
| 111 |
/// #[serde_as] |
| 112 |
/// # #[derive(Debug, Clone, PartialEq, Eq)] |
| 113 |
/// #[derive(Serialize, Deserialize)] |
| 114 |
/// struct KVMap( |
| 115 |
/// #[serde_as(as = "KeyValueMap<_>")] |
| 116 |
/// Vec<TupleStruct>, |
| 117 |
/// ); |
| 118 |
/// |
| 119 |
/// // --- |
| 120 |
/// |
| 121 |
/// // This will serialize this list of values |
| 122 |
/// let values = KVMap(vec![ |
| 123 |
/// TupleStruct( |
| 124 |
/// (IpAddr::from_str("127.0.0.1").unwrap(), 8), |
| 125 |
/// true |
| 126 |
/// ), |
| 127 |
/// TupleStruct( |
| 128 |
/// (IpAddr::from_str("::1").unwrap(), 128), |
| 129 |
/// true |
| 130 |
/// ), |
| 131 |
/// TupleStruct( |
| 132 |
/// (IpAddr::from_str("198.51.100.0").unwrap(), 24), |
| 133 |
/// true |
| 134 |
/// ), |
| 135 |
/// ]); |
| 136 |
/// |
| 137 |
/// // into this YAML |
| 138 |
/// let expected = |
| 139 |
/// r#"? - 127.0.0.1 |
| 140 |
/// - 8 |
| 141 |
/// : - true |
| 142 |
/// ? - ::1 |
| 143 |
/// - 128 |
| 144 |
/// : - true |
| 145 |
/// ? - 198.51.100.0 |
| 146 |
/// - 24 |
| 147 |
/// : - true |
| 148 |
/// "#; |
| 149 |
/// |
| 150 |
/// // Both serialization and deserialization work flawlessly. |
| 151 |
/// let serialized = serde_yaml::to_string(&values).unwrap(); |
| 152 |
/// assert_eq!(expected, serialized); |
| 153 |
/// let deserialized: KVMap = serde_yaml::from_str(&serialized).unwrap(); |
| 154 |
/// assert_eq!(values, deserialized); |
| 155 |
/// # } |
| 156 |
/// ``` |
| 157 |
pub struct KeyValueMap<T>(PhantomData<T>); |
| 158 |
|
| 159 |
impl<T, TAs> SerializeAs<Vec<T>> for KeyValueMap<TAs> |
| 160 |
where |
| 161 |
TAs: SerializeAs<T>, |
| 162 |
{ |
| 163 |
fn serialize_as<S>(source: &Vec<T>, serializer: S) -> Result<S::Ok, S::Error> |
| 164 |
where |
| 165 |
S: Serializer, |
| 166 |
{ |
| 167 |
<Vec<TAs>>::serialize_as(source, SeqAsMapSerializer(serializer)) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
impl<'de, T, TAs> DeserializeAs<'de, Vec<T>> for KeyValueMap<TAs> |
| 172 |
where |
| 173 |
TAs: DeserializeAs<'de, T>, |
| 174 |
{ |
| 175 |
fn deserialize_as<D>(deserializer: D) -> Result<Vec<T>, D::Error> |
| 176 |
where |
| 177 |
D: Deserializer<'de>, |
| 178 |
{ |
| 179 |
struct KeyValueMapVisitor<T, TAs> { |
| 180 |
is_human_readable: bool, |
| 181 |
phantom: PhantomData<(T, TAs)>, |
| 182 |
} |
| 183 |
|
| 184 |
impl<'de, T, TAs> Visitor<'de> for KeyValueMapVisitor<T, TAs> |
| 185 |
where |
| 186 |
TAs: DeserializeAs<'de, T>, |
| 187 |
{ |
| 188 |
type Value = Vec<T>; |
| 189 |
|
| 190 |
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 191 |
formatter.write_str("a map") |
| 192 |
} |
| 193 |
|
| 194 |
fn visit_map<A: MapAccess<'de>>(self, map: A) -> Result<Self::Value, A::Error> { |
| 195 |
<Vec<TAs>>::deserialize_as(SeqDeserializer { |
| 196 |
delegate: map, |
| 197 |
is_human_readable: self.is_human_readable, |
| 198 |
}) |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
let is_human_readable = deserializer.is_human_readable(); |
| 203 |
deserializer.deserialize_map(KeyValueMapVisitor::<T, TAs> { |
| 204 |
is_human_readable, |
| 205 |
phantom: PhantomData, |
| 206 |
}) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
// TODO Replace this with a const generic string once adt_const_params is stable. |
| 211 |
// This will allow something like this. |
| 212 |
// The `"id"` part is the field name, which gets converted to/from the map key. |
| 213 |
// #[serde_as(as = r#"KeyValueMap<"id", _>"#)] |
| 214 |
// Vec<SimpleStruct>, |
| 215 |
static MAP_KEY_IDENTIFIER: &str = "$key$"; |
| 216 |
|
| 217 |
/// Convert a sequence to a map during serialization. |
| 218 |
/// |
| 219 |
/// Only `serialize_seq` is implemented and forwarded to `serialize_map` on the inner `Serializer`. |
| 220 |
/// The elements are serialized with [`SerializeSeqElement`]. |
| 221 |
struct SeqAsMapSerializer<S>(S); |
| 222 |
|
| 223 |
impl<S> Serializer for SeqAsMapSerializer<S> |
| 224 |
where |
| 225 |
S: Serializer, |
| 226 |
{ |
| 227 |
type Ok = S::Ok; |
| 228 |
type Error = S::Error; |
| 229 |
|
| 230 |
type SerializeSeq = SerializeSeqElement<S::SerializeMap>; |
| 231 |
type SerializeTuple = Impossible<S::Ok, S::Error>; |
| 232 |
type SerializeTupleStruct = Impossible<S::Ok, S::Error>; |
| 233 |
type SerializeTupleVariant = Impossible<S::Ok, S::Error>; |
| 234 |
type SerializeMap = Impossible<S::Ok, S::Error>; |
| 235 |
type SerializeStruct = Impossible<S::Ok, S::Error>; |
| 236 |
type SerializeStructVariant = Impossible<S::Ok, S::Error>; |
| 237 |
|
| 238 |
fn is_human_readable(&self) -> bool { |
| 239 |
self.0.is_human_readable() |
| 240 |
} |
| 241 |
|
| 242 |
fn serialize_bool(self, _v: bool) -> Result<Self::Ok, Self::Error> { |
| 243 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 244 |
} |
| 245 |
|
| 246 |
fn serialize_i8(self, _v: i8) -> Result<Self::Ok, Self::Error> { |
| 247 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 248 |
} |
| 249 |
|
| 250 |
fn serialize_i16(self, _v: i16) -> Result<Self::Ok, Self::Error> { |
| 251 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 252 |
} |
| 253 |
|
| 254 |
fn serialize_i32(self, _v: i32) -> Result<Self::Ok, Self::Error> { |
| 255 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 256 |
} |
| 257 |
|
| 258 |
fn serialize_i64(self, _v: i64) -> Result<Self::Ok, Self::Error> { |
| 259 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 260 |
} |
| 261 |
|
| 262 |
fn serialize_i128(self, _v: i128) -> Result<Self::Ok, Self::Error> { |
| 263 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 264 |
} |
| 265 |
|
| 266 |
fn serialize_u8(self, _v: u8) -> Result<Self::Ok, Self::Error> { |
| 267 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 268 |
} |
| 269 |
|
| 270 |
fn serialize_u16(self, _v: u16) -> Result<Self::Ok, Self::Error> { |
| 271 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 272 |
} |
| 273 |
|
| 274 |
fn serialize_u32(self, _v: u32) -> Result<Self::Ok, Self::Error> { |
| 275 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 276 |
} |
| 277 |
|
| 278 |
fn serialize_u64(self, _v: u64) -> Result<Self::Ok, Self::Error> { |
| 279 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 280 |
} |
| 281 |
|
| 282 |
fn serialize_u128(self, _v: u128) -> Result<Self::Ok, Self::Error> { |
| 283 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 284 |
} |
| 285 |
|
| 286 |
fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Self::Error> { |
| 287 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 288 |
} |
| 289 |
|
| 290 |
fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Self::Error> { |
| 291 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 292 |
} |
| 293 |
|
| 294 |
fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> { |
| 295 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 296 |
} |
| 297 |
|
| 298 |
fn serialize_str(self, _v: &str) -> Result<Self::Ok, Self::Error> { |
| 299 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 300 |
} |
| 301 |
|
| 302 |
fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> { |
| 303 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 304 |
} |
| 305 |
|
| 306 |
fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
| 307 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 308 |
} |
| 309 |
|
| 310 |
fn serialize_some<T>(self, _value: &T) -> Result<Self::Ok, Self::Error> |
| 311 |
where |
| 312 |
T: Serialize + ?Sized, |
| 313 |
{ |
| 314 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 315 |
} |
| 316 |
|
| 317 |
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
| 318 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 319 |
} |
| 320 |
|
| 321 |
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> { |
| 322 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 323 |
} |
| 324 |
|
| 325 |
fn serialize_unit_variant( |
| 326 |
self, |
| 327 |
_name: &'static str, |
| 328 |
_variant_index: u32, |
| 329 |
_variant: &'static str, |
| 330 |
) -> Result<Self::Ok, Self::Error> { |
| 331 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 332 |
} |
| 333 |
|
| 334 |
fn serialize_newtype_struct<T>( |
| 335 |
self, |
| 336 |
_name: &'static str, |
| 337 |
_value: &T, |
| 338 |
) -> Result<Self::Ok, Self::Error> |
| 339 |
where |
| 340 |
T: Serialize + ?Sized, |
| 341 |
{ |
| 342 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 343 |
} |
| 344 |
|
| 345 |
fn serialize_newtype_variant<T>( |
| 346 |
self, |
| 347 |
_name: &'static str, |
| 348 |
_variant_index: u32, |
| 349 |
_variant: &'static str, |
| 350 |
_value: &T, |
| 351 |
) -> Result<Self::Ok, Self::Error> |
| 352 |
where |
| 353 |
T: Serialize + ?Sized, |
| 354 |
{ |
| 355 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 356 |
} |
| 357 |
|
| 358 |
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
| 359 |
let is_human_readable = self.0.is_human_readable(); |
| 360 |
self.0 |
| 361 |
.serialize_map(len) |
| 362 |
.map(|delegate| SerializeSeqElement { |
| 363 |
delegate, |
| 364 |
is_human_readable, |
| 365 |
}) |
| 366 |
} |
| 367 |
|
| 368 |
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> { |
| 369 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 370 |
} |
| 371 |
|
| 372 |
fn serialize_tuple_struct( |
| 373 |
self, |
| 374 |
_name: &'static str, |
| 375 |
_len: usize, |
| 376 |
) -> Result<Self::SerializeTupleStruct, Self::Error> { |
| 377 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 378 |
} |
| 379 |
|
| 380 |
fn serialize_tuple_variant( |
| 381 |
self, |
| 382 |
_name: &'static str, |
| 383 |
_variant_index: u32, |
| 384 |
_variant: &'static str, |
| 385 |
_len: usize, |
| 386 |
) -> Result<Self::SerializeTupleVariant, Self::Error> { |
| 387 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 388 |
} |
| 389 |
|
| 390 |
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
| 391 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 392 |
} |
| 393 |
|
| 394 |
fn serialize_struct( |
| 395 |
self, |
| 396 |
_name: &'static str, |
| 397 |
_len: usize, |
| 398 |
) -> Result<Self::SerializeStruct, Self::Error> { |
| 399 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 400 |
} |
| 401 |
|
| 402 |
fn serialize_struct_variant( |
| 403 |
self, |
| 404 |
_name: &'static str, |
| 405 |
_variant_index: u32, |
| 406 |
_variant: &'static str, |
| 407 |
_len: usize, |
| 408 |
) -> Result<Self::SerializeStructVariant, Self::Error> { |
| 409 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
/// Serialize a single element but turn the sequence into a map logic. |
| 414 |
/// |
| 415 |
/// It uses [`ElementAsKeyValueSerializer`] for the map element serialization. |
| 416 |
/// |
| 417 |
/// The [`Serializer`] implementation handles `serialize_struct`, `serialize_map` and `serialize_seq` functions by deferring the work to [`SerializeStruct`], [`SerializeMap`] and [`SerializeSeq`] respectively. |
| 418 |
struct SerializeSeqElement<M> { |
| 419 |
delegate: M, |
| 420 |
is_human_readable: bool, |
| 421 |
} |
| 422 |
|
| 423 |
impl<M> SerializeSeq for SerializeSeqElement<M> |
| 424 |
where |
| 425 |
M: SerializeMap, |
| 426 |
{ |
| 427 |
type Ok = M::Ok; |
| 428 |
type Error = M::Error; |
| 429 |
|
| 430 |
fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error> |
| 431 |
where |
| 432 |
T: Serialize + ?Sized, |
| 433 |
{ |
| 434 |
value.serialize(ElementAsKeyValueSerializer { |
| 435 |
delegate: &mut self.delegate, |
| 436 |
is_human_readable: self.is_human_readable, |
| 437 |
})?; |
| 438 |
Ok(()) |
| 439 |
} |
| 440 |
|
| 441 |
fn end(self) -> Result<Self::Ok, Self::Error> { |
| 442 |
self.delegate.end() |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
struct ElementAsKeyValueSerializer<'a, M> { |
| 447 |
delegate: &'a mut M, |
| 448 |
is_human_readable: bool, |
| 449 |
} |
| 450 |
|
| 451 |
impl<'a, M> Serializer for ElementAsKeyValueSerializer<'a, M> |
| 452 |
where |
| 453 |
M: SerializeMap, |
| 454 |
{ |
| 455 |
type Ok = (); |
| 456 |
type Error = M::Error; |
| 457 |
|
| 458 |
type SerializeSeq = KeyValueSeqSerializer<'a, M>; |
| 459 |
type SerializeTuple = KeyValueTupleSerializer<'a, M>; |
| 460 |
type SerializeTupleStruct = KeyValueTupleStructSerializer<'a, M>; |
| 461 |
type SerializeTupleVariant = Impossible<Self::Ok, Self::Error>; |
| 462 |
type SerializeMap = KeyValueMapSerializer<'a, M>; |
| 463 |
type SerializeStruct = KeyValueStructSerializer<'a, M>; |
| 464 |
type SerializeStructVariant = Impossible<Self::Ok, Self::Error>; |
| 465 |
|
| 466 |
fn is_human_readable(&self) -> bool { |
| 467 |
self.is_human_readable |
| 468 |
} |
| 469 |
|
| 470 |
fn serialize_bool(self, _v: bool) -> Result<Self::Ok, Self::Error> { |
| 471 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 472 |
} |
| 473 |
|
| 474 |
fn serialize_i8(self, _v: i8) -> Result<Self::Ok, Self::Error> { |
| 475 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 476 |
} |
| 477 |
|
| 478 |
fn serialize_i16(self, _v: i16) -> Result<Self::Ok, Self::Error> { |
| 479 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 480 |
} |
| 481 |
|
| 482 |
fn serialize_i32(self, _v: i32) -> Result<Self::Ok, Self::Error> { |
| 483 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 484 |
} |
| 485 |
|
| 486 |
fn serialize_i64(self, _v: i64) -> Result<Self::Ok, Self::Error> { |
| 487 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 488 |
} |
| 489 |
|
| 490 |
fn serialize_i128(self, _v: i128) -> Result<Self::Ok, Self::Error> { |
| 491 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 492 |
} |
| 493 |
|
| 494 |
fn serialize_u8(self, _v: u8) -> Result<Self::Ok, Self::Error> { |
| 495 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 496 |
} |
| 497 |
|
| 498 |
fn serialize_u16(self, _v: u16) -> Result<Self::Ok, Self::Error> { |
| 499 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 500 |
} |
| 501 |
|
| 502 |
fn serialize_u32(self, _v: u32) -> Result<Self::Ok, Self::Error> { |
| 503 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 504 |
} |
| 505 |
|
| 506 |
fn serialize_u64(self, _v: u64) -> Result<Self::Ok, Self::Error> { |
| 507 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 508 |
} |
| 509 |
|
| 510 |
fn serialize_u128(self, _v: u128) -> Result<Self::Ok, Self::Error> { |
| 511 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 512 |
} |
| 513 |
|
| 514 |
fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Self::Error> { |
| 515 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 516 |
} |
| 517 |
|
| 518 |
fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Self::Error> { |
| 519 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 520 |
} |
| 521 |
|
| 522 |
fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> { |
| 523 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 524 |
} |
| 525 |
|
| 526 |
fn serialize_str(self, _v: &str) -> Result<Self::Ok, Self::Error> { |
| 527 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 528 |
} |
| 529 |
|
| 530 |
fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> { |
| 531 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 532 |
} |
| 533 |
|
| 534 |
fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
| 535 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 536 |
} |
| 537 |
|
| 538 |
fn serialize_some<T>(self, _value: &T) -> Result<Self::Ok, Self::Error> |
| 539 |
where |
| 540 |
T: Serialize + ?Sized, |
| 541 |
{ |
| 542 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 543 |
} |
| 544 |
|
| 545 |
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
| 546 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 547 |
} |
| 548 |
|
| 549 |
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> { |
| 550 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 551 |
} |
| 552 |
|
| 553 |
fn serialize_unit_variant( |
| 554 |
self, |
| 555 |
_name: &'static str, |
| 556 |
_variant_index: u32, |
| 557 |
_variant: &'static str, |
| 558 |
) -> Result<Self::Ok, Self::Error> { |
| 559 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 560 |
} |
| 561 |
|
| 562 |
fn serialize_newtype_struct<T>( |
| 563 |
self, |
| 564 |
_name: &'static str, |
| 565 |
_value: &T, |
| 566 |
) -> Result<Self::Ok, Self::Error> |
| 567 |
where |
| 568 |
T: Serialize + ?Sized, |
| 569 |
{ |
| 570 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 571 |
} |
| 572 |
|
| 573 |
fn serialize_newtype_variant<T>( |
| 574 |
self, |
| 575 |
_name: &'static str, |
| 576 |
_variant_index: u32, |
| 577 |
_variant: &'static str, |
| 578 |
_value: &T, |
| 579 |
) -> Result<Self::Ok, Self::Error> |
| 580 |
where |
| 581 |
T: Serialize + ?Sized, |
| 582 |
{ |
| 583 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 584 |
} |
| 585 |
|
| 586 |
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
| 587 |
Ok(KeyValueSeqSerializer { |
| 588 |
delegate: self.delegate, |
| 589 |
is_human_readable: self.is_human_readable, |
| 590 |
content: Vec::with_capacity(len.unwrap_or(17) - 1), |
| 591 |
key: None, |
| 592 |
}) |
| 593 |
} |
| 594 |
|
| 595 |
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> { |
| 596 |
Ok(KeyValueTupleSerializer { |
| 597 |
delegate: self.delegate, |
| 598 |
is_human_readable: self.is_human_readable, |
| 599 |
content: Vec::with_capacity(len - 1), |
| 600 |
key: None, |
| 601 |
}) |
| 602 |
} |
| 603 |
|
| 604 |
fn serialize_tuple_struct( |
| 605 |
self, |
| 606 |
name: &'static str, |
| 607 |
len: usize, |
| 608 |
) -> Result<Self::SerializeTupleStruct, Self::Error> { |
| 609 |
Ok(KeyValueTupleStructSerializer { |
| 610 |
delegate: self.delegate, |
| 611 |
is_human_readable: self.is_human_readable, |
| 612 |
name, |
| 613 |
content: Vec::with_capacity(len - 1), |
| 614 |
key: None, |
| 615 |
}) |
| 616 |
} |
| 617 |
|
| 618 |
fn serialize_tuple_variant( |
| 619 |
self, |
| 620 |
_name: &'static str, |
| 621 |
_variant_index: u32, |
| 622 |
_variant: &'static str, |
| 623 |
_len: usize, |
| 624 |
) -> Result<Self::SerializeTupleVariant, Self::Error> { |
| 625 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 626 |
} |
| 627 |
|
| 628 |
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
| 629 |
Ok(KeyValueMapSerializer { |
| 630 |
delegate: self.delegate, |
| 631 |
is_human_readable: self.is_human_readable, |
| 632 |
content: Vec::with_capacity(len.unwrap_or(17) - 1), |
| 633 |
next_is_magic_key: false, |
| 634 |
key: None, |
| 635 |
tmp: None, |
| 636 |
}) |
| 637 |
} |
| 638 |
|
| 639 |
fn serialize_struct( |
| 640 |
self, |
| 641 |
name: &'static str, |
| 642 |
len: usize, |
| 643 |
) -> Result<Self::SerializeStruct, Self::Error> { |
| 644 |
Ok(KeyValueStructSerializer { |
| 645 |
delegate: self.delegate, |
| 646 |
is_human_readable: self.is_human_readable, |
| 647 |
name, |
| 648 |
content: Vec::with_capacity(len - 1), |
| 649 |
key: None, |
| 650 |
}) |
| 651 |
} |
| 652 |
|
| 653 |
fn serialize_struct_variant( |
| 654 |
self, |
| 655 |
_name: &'static str, |
| 656 |
_variant_index: u32, |
| 657 |
_variant: &'static str, |
| 658 |
_len: usize, |
| 659 |
) -> Result<Self::SerializeStructVariant, Self::Error> { |
| 660 |
Err(SerError::custom("wrong type for KeyValueMap")) |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
/// Serialize a sequence to a key and value pair of a map. |
| 665 |
/// |
| 666 |
/// This requires that the sequence has at least one element. |
| 667 |
struct KeyValueSeqSerializer<'a, M> { |
| 668 |
delegate: &'a mut M, |
| 669 |
is_human_readable: bool, |
| 670 |
content: Vec<SerContent>, |
| 671 |
key: Option<SerContent>, |
| 672 |
} |
| 673 |
|
| 674 |
impl<M> SerializeSeq for KeyValueSeqSerializer<'_, M> |
| 675 |
where |
| 676 |
M: SerializeMap, |
| 677 |
{ |
| 678 |
type Ok = (); |
| 679 |
type Error = M::Error; |
| 680 |
|
| 681 |
fn serialize_element<T>(&mut self, element: &T) -> Result<(), Self::Error> |
| 682 |
where |
| 683 |
T: Serialize + ?Sized, |
| 684 |
{ |
| 685 |
let element: SerContent = |
| 686 |
element.serialize(ContentSerializer::new(self.is_human_readable))?; |
| 687 |
if self.key.is_none() { |
| 688 |
self.key = Some(element); |
| 689 |
return Ok(()); |
| 690 |
} |
| 691 |
self.content.push(element); |
| 692 |
Ok(()) |
| 693 |
} |
| 694 |
|
| 695 |
fn end(self) -> Result<Self::Ok, Self::Error> { |
| 696 |
if let Some(key) = self.key { |
| 697 |
self.delegate |
| 698 |
.serialize_entry(&key, &SerContent::Seq(self.content)) |
| 699 |
} else { |
| 700 |
Err(SerError::custom("missing value for `$key$` field")) |
| 701 |
} |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
/// Serialize a tuple to a key and value pair of a map. |
| 706 |
/// |
| 707 |
/// This requires that the tuple has at least one element. |
| 708 |
struct KeyValueTupleSerializer<'a, M> { |
| 709 |
delegate: &'a mut M, |
| 710 |
is_human_readable: bool, |
| 711 |
content: Vec<SerContent>, |
| 712 |
key: Option<SerContent>, |
| 713 |
} |
| 714 |
|
| 715 |
impl<M> SerializeTuple for KeyValueTupleSerializer<'_, M> |
| 716 |
where |
| 717 |
M: SerializeMap, |
| 718 |
{ |
| 719 |
type Ok = (); |
| 720 |
type Error = M::Error; |
| 721 |
|
| 722 |
fn serialize_element<T>(&mut self, element: &T) -> Result<(), Self::Error> |
| 723 |
where |
| 724 |
T: Serialize + ?Sized, |
| 725 |
{ |
| 726 |
let element: SerContent = |
| 727 |
element.serialize(ContentSerializer::new(self.is_human_readable))?; |
| 728 |
if self.key.is_none() { |
| 729 |
self.key = Some(element); |
| 730 |
return Ok(()); |
| 731 |
} |
| 732 |
self.content.push(element); |
| 733 |
Ok(()) |
| 734 |
} |
| 735 |
|
| 736 |
fn end(self) -> Result<Self::Ok, Self::Error> { |
| 737 |
if let Some(key) = self.key { |
| 738 |
self.delegate |
| 739 |
.serialize_entry(&key, &SerContent::Tuple(self.content)) |
| 740 |
} else { |
| 741 |
Err(SerError::custom("missing value for `$key$` field")) |
| 742 |
} |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
/// Serialize a tuple struct to a key and value pair of a map. |
| 747 |
/// |
| 748 |
/// This requires that the tuple struct has at least one element. |
| 749 |
struct KeyValueTupleStructSerializer<'a, M> { |
| 750 |
delegate: &'a mut M, |
| 751 |
is_human_readable: bool, |
| 752 |
name: &'static str, |
| 753 |
content: Vec<SerContent>, |
| 754 |
key: Option<SerContent>, |
| 755 |
} |
| 756 |
|
| 757 |
impl<M> SerializeTupleStruct for KeyValueTupleStructSerializer<'_, M> |
| 758 |
where |
| 759 |
M: SerializeMap, |
| 760 |
{ |
| 761 |
type Ok = (); |
| 762 |
type Error = M::Error; |
| 763 |
|
| 764 |
fn serialize_field<T>(&mut self, field: &T) -> Result<(), Self::Error> |
| 765 |
where |
| 766 |
T: Serialize + ?Sized, |
| 767 |
{ |
| 768 |
let field: SerContent = field.serialize(ContentSerializer::new(self.is_human_readable))?; |
| 769 |
if self.key.is_none() { |
| 770 |
self.key = Some(field); |
| 771 |
return Ok(()); |
| 772 |
} |
| 773 |
self.content.push(field); |
| 774 |
Ok(()) |
| 775 |
} |
| 776 |
|
| 777 |
fn end(self) -> Result<Self::Ok, Self::Error> { |
| 778 |
if let Some(key) = self.key { |
| 779 |
self.delegate |
| 780 |
.serialize_entry(&key, &SerContent::TupleStruct(self.name, self.content)) |
| 781 |
} else { |
| 782 |
Err(SerError::custom("missing value for `$key$` field")) |
| 783 |
} |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
/// Serialize a map to a key and value pair of a map. |
| 788 |
/// |
| 789 |
/// This requires that the map has one element which serializes using the magic `$key$` key. |
| 790 |
struct KeyValueMapSerializer<'a, M> { |
| 791 |
delegate: &'a mut M, |
| 792 |
is_human_readable: bool, |
| 793 |
content: Vec<(SerContent, SerContent)>, |
| 794 |
next_is_magic_key: bool, |
| 795 |
key: Option<SerContent>, |
| 796 |
tmp: Option<SerContent>, |
| 797 |
} |
| 798 |
|
| 799 |
impl<M> SerializeMap for KeyValueMapSerializer<'_, M> |
| 800 |
where |
| 801 |
M: SerializeMap, |
| 802 |
{ |
| 803 |
type Ok = (); |
| 804 |
type Error = M::Error; |
| 805 |
|
| 806 |
fn serialize_key<T>(&mut self, key: &T) -> Result<(), Self::Error> |
| 807 |
where |
| 808 |
T: Serialize + ?Sized, |
| 809 |
{ |
| 810 |
let key: SerContent = key.serialize(ContentSerializer::new(self.is_human_readable))?; |
| 811 |
if key.as_str() == Some(MAP_KEY_IDENTIFIER) { |
| 812 |
self.next_is_magic_key = true; |
| 813 |
return Ok(()); |
| 814 |
} |
| 815 |
self.tmp = Some(key); |
| 816 |
Ok(()) |
| 817 |
} |
| 818 |
|
| 819 |
fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error> |
| 820 |
where |
| 821 |
T: Serialize + ?Sized, |
| 822 |
{ |
| 823 |
let value: SerContent = value.serialize(ContentSerializer::new(self.is_human_readable))?; |
| 824 |
|
| 825 |
if self.next_is_magic_key { |
| 826 |
self.next_is_magic_key = false; |
| 827 |
self.key = Some(value); |
| 828 |
return Ok(()); |
| 829 |
} |
| 830 |
self.content.push(( |
| 831 |
self.tmp |
| 832 |
.take() |
| 833 |
.expect("serialize_value called before serialize_key"), |
| 834 |
value, |
| 835 |
)); |
| 836 |
Ok(()) |
| 837 |
} |
| 838 |
|
| 839 |
fn end(self) -> Result<Self::Ok, Self::Error> { |
| 840 |
if let Some(key) = self.key { |
| 841 |
self.delegate |
| 842 |
.serialize_entry(&key, &SerContent::Map(self.content)) |
| 843 |
} else { |
| 844 |
Err(SerError::custom("missing value for `$key$` field")) |
| 845 |
} |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
/// Serialize a struct to a key and value pair of a map. |
| 850 |
/// |
| 851 |
/// This requires that the struct has one field named `$key$`. |
| 852 |
struct KeyValueStructSerializer<'a, M> { |
| 853 |
delegate: &'a mut M, |
| 854 |
is_human_readable: bool, |
| 855 |
name: &'static str, |
| 856 |
content: Vec<(&'static str, SerContent)>, |
| 857 |
key: Option<SerContent>, |
| 858 |
} |
| 859 |
|
| 860 |
impl<M> SerializeStruct for KeyValueStructSerializer<'_, M> |
| 861 |
where |
| 862 |
M: SerializeMap, |
| 863 |
{ |
| 864 |
type Ok = (); |
| 865 |
type Error = M::Error; |
| 866 |
|
| 867 |
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> |
| 868 |
where |
| 869 |
T: Serialize + ?Sized, |
| 870 |
{ |
| 871 |
// Serialize to a Content type first |
| 872 |
let value: SerContent = value.serialize(ContentSerializer::new(self.is_human_readable))?; |
| 873 |
|
| 874 |
if key == MAP_KEY_IDENTIFIER { |
| 875 |
self.key = Some(value); |
| 876 |
return Ok(()); |
| 877 |
} |
| 878 |
self.content.push((key, value)); |
| 879 |
Ok(()) |
| 880 |
} |
| 881 |
|
| 882 |
fn end(self) -> Result<Self::Ok, Self::Error> { |
| 883 |
if let Some(key) = self.key { |
| 884 |
self.delegate |
| 885 |
.serialize_entry(&key, &SerContent::Struct(self.name, self.content)) |
| 886 |
} else { |
| 887 |
Err(SerError::custom("missing value for `$key$` field")) |
| 888 |
} |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
// Below is deserialization code |
| 893 |
|
| 894 |
/// Deserialize the sequence of enum instances. |
| 895 |
/// |
| 896 |
/// The main [`Deserializer`] implementation handles the outer sequence (e.g., `Vec`), while the [`SeqAccess`] implementation is responsible for the inner elements. |
| 897 |
struct SeqDeserializer<M> { |
| 898 |
delegate: M, |
| 899 |
is_human_readable: bool, |
| 900 |
} |
| 901 |
|
| 902 |
impl<'de, M> Deserializer<'de> for SeqDeserializer<M> |
| 903 |
where |
| 904 |
M: MapAccess<'de>, |
| 905 |
{ |
| 906 |
type Error = M::Error; |
| 907 |
|
| 908 |
fn is_human_readable(&self) -> bool { |
| 909 |
self.is_human_readable |
| 910 |
} |
| 911 |
|
| 912 |
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
| 913 |
where |
| 914 |
V: Visitor<'de>, |
| 915 |
{ |
| 916 |
visitor.visit_seq(self) |
| 917 |
} |
| 918 |
|
| 919 |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
| 920 |
where |
| 921 |
V: Visitor<'de>, |
| 922 |
{ |
| 923 |
self.deserialize_seq(visitor) |
| 924 |
} |
| 925 |
|
| 926 |
serde::forward_to_deserialize_any! { |
| 927 |
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
| 928 |
bytes byte_buf option unit unit_struct newtype_struct tuple |
| 929 |
tuple_struct map struct enum identifier ignored_any |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
impl<'de, M> SeqAccess<'de> for SeqDeserializer<M> |
| 934 |
where |
| 935 |
M: MapAccess<'de>, |
| 936 |
{ |
| 937 |
type Error = M::Error; |
| 938 |
|
| 939 |
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> |
| 940 |
where |
| 941 |
T: DeserializeSeed<'de>, |
| 942 |
{ |
| 943 |
let key_value: Option<DeContent<'de>> = self.delegate.next_key()?; |
| 944 |
if let Some(key_value) = key_value { |
| 945 |
seed.deserialize(MapKeyDeserializer { |
| 946 |
delegate: &mut self.delegate, |
| 947 |
is_human_readable: self.is_human_readable, |
| 948 |
key_value, |
| 949 |
}) |
| 950 |
.map(Some) |
| 951 |
} else { |
| 952 |
Ok(None) |
| 953 |
} |
| 954 |
} |
| 955 |
|
| 956 |
fn size_hint(&self) -> Option<usize> { |
| 957 |
self.delegate.size_hint() |
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
struct MapKeyDeserializer<'de, M> { |
| 962 |
delegate: M, |
| 963 |
is_human_readable: bool, |
| 964 |
key_value: DeContent<'de>, |
| 965 |
} |
| 966 |
|
| 967 |
impl<'de, M> Deserializer<'de> for MapKeyDeserializer<'de, M> |
| 968 |
where |
| 969 |
M: MapAccess<'de>, |
| 970 |
{ |
| 971 |
type Error = M::Error; |
| 972 |
|
| 973 |
fn is_human_readable(&self) -> bool { |
| 974 |
self.is_human_readable |
| 975 |
} |
| 976 |
|
| 977 |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
| 978 |
where |
| 979 |
V: Visitor<'de>, |
| 980 |
{ |
| 981 |
self.deserialize_map(visitor) |
| 982 |
} |
| 983 |
|
| 984 |
fn deserialize_seq<V>(mut self, visitor: V) -> Result<V::Value, Self::Error> |
| 985 |
where |
| 986 |
V: Visitor<'de>, |
| 987 |
{ |
| 988 |
self.delegate.next_value_seed(KeyValueSeqDeserialize { |
| 989 |
delegate: visitor, |
| 990 |
first: Some(self.key_value), |
| 991 |
}) |
| 992 |
} |
| 993 |
|
| 994 |
fn deserialize_tuple<V>(mut self, len: usize, visitor: V) -> Result<V::Value, Self::Error> |
| 995 |
where |
| 996 |
V: Visitor<'de>, |
| 997 |
{ |
| 998 |
self.delegate.next_value_seed(KeyValueTupleDeserialize { |
| 999 |
delegate: visitor, |
| 1000 |
len, |
| 1001 |
first: Some(self.key_value), |
| 1002 |
}) |
| 1003 |
} |
| 1004 |
|
| 1005 |
fn deserialize_tuple_struct<V>( |
| 1006 |
mut self, |
| 1007 |
name: &'static str, |
| 1008 |
len: usize, |
| 1009 |
visitor: V, |
| 1010 |
) -> Result<V::Value, Self::Error> |
| 1011 |
where |
| 1012 |
V: Visitor<'de>, |
| 1013 |
{ |
| 1014 |
self.delegate |
| 1015 |
.next_value_seed(KeyValueTupleStructDeserialize { |
| 1016 |
delegate: visitor, |
| 1017 |
name, |
| 1018 |
len, |
| 1019 |
first: Some(self.key_value), |
| 1020 |
}) |
| 1021 |
} |
| 1022 |
|
| 1023 |
fn deserialize_map<V>(mut self, visitor: V) -> Result<V::Value, Self::Error> |
| 1024 |
where |
| 1025 |
V: Visitor<'de>, |
| 1026 |
{ |
| 1027 |
self.delegate.next_value_seed(KeyValueMapDeserialize { |
| 1028 |
delegate: visitor, |
| 1029 |
first: Some(self.key_value), |
| 1030 |
}) |
| 1031 |
} |
| 1032 |
|
| 1033 |
fn deserialize_struct<V>( |
| 1034 |
mut self, |
| 1035 |
name: &'static str, |
| 1036 |
fields: &'static [&'static str], |
| 1037 |
visitor: V, |
| 1038 |
) -> Result<V::Value, Self::Error> |
| 1039 |
where |
| 1040 |
V: Visitor<'de>, |
| 1041 |
{ |
| 1042 |
self.delegate.next_value_seed(KeyValueStructDeserialize { |
| 1043 |
delegate: visitor, |
| 1044 |
name, |
| 1045 |
fields, |
| 1046 |
first: Some(self.key_value), |
| 1047 |
}) |
| 1048 |
} |
| 1049 |
|
| 1050 |
serde::forward_to_deserialize_any! { |
| 1051 |
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
| 1052 |
bytes byte_buf option unit unit_struct newtype_struct |
| 1053 |
enum identifier ignored_any |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
struct KeyValueSeqDeserialize<'de, V> { |
| 1058 |
delegate: V, |
| 1059 |
first: Option<DeContent<'de>>, |
| 1060 |
} |
| 1061 |
|
| 1062 |
impl<'de, V> DeserializeSeed<'de> for KeyValueSeqDeserialize<'de, V> |
| 1063 |
where |
| 1064 |
V: Visitor<'de>, |
| 1065 |
{ |
| 1066 |
type Value = V::Value; |
| 1067 |
|
| 1068 |
fn deserialize<D>(mut self, deserializer: D) -> Result<Self::Value, D::Error> |
| 1069 |
where |
| 1070 |
D: Deserializer<'de>, |
| 1071 |
{ |
| 1072 |
let is_human_readable = deserializer.is_human_readable(); |
| 1073 |
deserializer.deserialize_seq(VisitorWrapper { |
| 1074 |
delegate: self.delegate, |
| 1075 |
is_human_readable, |
| 1076 |
first: self.first.take(), |
| 1077 |
}) |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
struct KeyValueTupleDeserialize<'de, V> { |
| 1082 |
delegate: V, |
| 1083 |
len: usize, |
| 1084 |
first: Option<DeContent<'de>>, |
| 1085 |
} |
| 1086 |
|
| 1087 |
impl<'de, V> DeserializeSeed<'de> for KeyValueTupleDeserialize<'de, V> |
| 1088 |
where |
| 1089 |
V: Visitor<'de>, |
| 1090 |
{ |
| 1091 |
type Value = V::Value; |
| 1092 |
|
| 1093 |
fn deserialize<D>(mut self, deserializer: D) -> Result<Self::Value, D::Error> |
| 1094 |
where |
| 1095 |
D: Deserializer<'de>, |
| 1096 |
{ |
| 1097 |
let is_human_readable = deserializer.is_human_readable(); |
| 1098 |
deserializer.deserialize_tuple( |
| 1099 |
self.len, |
| 1100 |
VisitorWrapper { |
| 1101 |
delegate: self.delegate, |
| 1102 |
is_human_readable, |
| 1103 |
first: self.first.take(), |
| 1104 |
}, |
| 1105 |
) |
| 1106 |
} |
| 1107 |
} |
| 1108 |
|
| 1109 |
struct KeyValueTupleStructDeserialize<'de, V> { |
| 1110 |
delegate: V, |
| 1111 |
name: &'static str, |
| 1112 |
len: usize, |
| 1113 |
first: Option<DeContent<'de>>, |
| 1114 |
} |
| 1115 |
|
| 1116 |
impl<'de, V> DeserializeSeed<'de> for KeyValueTupleStructDeserialize<'de, V> |
| 1117 |
where |
| 1118 |
V: Visitor<'de>, |
| 1119 |
{ |
| 1120 |
type Value = V::Value; |
| 1121 |
|
| 1122 |
fn deserialize<D>(mut self, deserializer: D) -> Result<Self::Value, D::Error> |
| 1123 |
where |
| 1124 |
D: Deserializer<'de>, |
| 1125 |
{ |
| 1126 |
let is_human_readable = deserializer.is_human_readable(); |
| 1127 |
deserializer.deserialize_tuple_struct( |
| 1128 |
self.name, |
| 1129 |
self.len, |
| 1130 |
VisitorWrapper { |
| 1131 |
delegate: self.delegate, |
| 1132 |
is_human_readable, |
| 1133 |
first: self.first.take(), |
| 1134 |
}, |
| 1135 |
) |
| 1136 |
} |
| 1137 |
} |
| 1138 |
|
| 1139 |
struct KeyValueMapDeserialize<'de, V> { |
| 1140 |
delegate: V, |
| 1141 |
first: Option<DeContent<'de>>, |
| 1142 |
} |
| 1143 |
|
| 1144 |
impl<'de, V> DeserializeSeed<'de> for KeyValueMapDeserialize<'de, V> |
| 1145 |
where |
| 1146 |
V: Visitor<'de>, |
| 1147 |
{ |
| 1148 |
type Value = V::Value; |
| 1149 |
|
| 1150 |
fn deserialize<D>(mut self, deserializer: D) -> Result<Self::Value, D::Error> |
| 1151 |
where |
| 1152 |
D: Deserializer<'de>, |
| 1153 |
{ |
| 1154 |
let is_human_readable = deserializer.is_human_readable(); |
| 1155 |
deserializer.deserialize_map(VisitorWrapper { |
| 1156 |
delegate: self.delegate, |
| 1157 |
is_human_readable, |
| 1158 |
first: self.first.take(), |
| 1159 |
}) |
| 1160 |
} |
| 1161 |
} |
| 1162 |
|
| 1163 |
struct KeyValueStructDeserialize<'de, V> { |
| 1164 |
delegate: V, |
| 1165 |
name: &'static str, |
| 1166 |
fields: &'static [&'static str], |
| 1167 |
first: Option<DeContent<'de>>, |
| 1168 |
} |
| 1169 |
|
| 1170 |
impl<'de, V> DeserializeSeed<'de> for KeyValueStructDeserialize<'de, V> |
| 1171 |
where |
| 1172 |
V: Visitor<'de>, |
| 1173 |
{ |
| 1174 |
type Value = V::Value; |
| 1175 |
|
| 1176 |
fn deserialize<D>(mut self, deserializer: D) -> Result<Self::Value, D::Error> |
| 1177 |
where |
| 1178 |
D: Deserializer<'de>, |
| 1179 |
{ |
| 1180 |
let is_human_readable = deserializer.is_human_readable(); |
| 1181 |
deserializer.deserialize_struct( |
| 1182 |
self.name, |
| 1183 |
self.fields, |
| 1184 |
VisitorWrapper { |
| 1185 |
delegate: self.delegate, |
| 1186 |
is_human_readable, |
| 1187 |
first: self.first.take(), |
| 1188 |
}, |
| 1189 |
) |
| 1190 |
} |
| 1191 |
} |
| 1192 |
|
| 1193 |
struct VisitorWrapper<'de, V> { |
| 1194 |
delegate: V, |
| 1195 |
is_human_readable: bool, |
| 1196 |
first: Option<DeContent<'de>>, |
| 1197 |
} |
| 1198 |
|
| 1199 |
impl<'de, V> Visitor<'de> for VisitorWrapper<'de, V> |
| 1200 |
where |
| 1201 |
V: Visitor<'de>, |
| 1202 |
{ |
| 1203 |
type Value = V::Value; |
| 1204 |
|
| 1205 |
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 1206 |
self.delegate.expecting(formatter) |
| 1207 |
} |
| 1208 |
|
| 1209 |
fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error> |
| 1210 |
where |
| 1211 |
A: MapAccess<'de>, |
| 1212 |
{ |
| 1213 |
self.delegate.visit_map(MapAccessWrapper { |
| 1214 |
delegate: map, |
| 1215 |
is_human_readable: self.is_human_readable, |
| 1216 |
first: self.first, |
| 1217 |
}) |
| 1218 |
} |
| 1219 |
|
| 1220 |
fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error> |
| 1221 |
where |
| 1222 |
A: SeqAccess<'de>, |
| 1223 |
{ |
| 1224 |
self.delegate.visit_seq(SeqAccessWrapper { |
| 1225 |
delegate: seq, |
| 1226 |
is_human_readable: self.is_human_readable, |
| 1227 |
first: self.first, |
| 1228 |
}) |
| 1229 |
} |
| 1230 |
} |
| 1231 |
|
| 1232 |
struct MapAccessWrapper<'de, M> { |
| 1233 |
delegate: M, |
| 1234 |
is_human_readable: bool, |
| 1235 |
first: Option<DeContent<'de>>, |
| 1236 |
} |
| 1237 |
|
| 1238 |
impl<'de, M> MapAccess<'de> for MapAccessWrapper<'de, M> |
| 1239 |
where |
| 1240 |
M: MapAccess<'de>, |
| 1241 |
{ |
| 1242 |
type Error = M::Error; |
| 1243 |
|
| 1244 |
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error> |
| 1245 |
where |
| 1246 |
K: DeserializeSeed<'de>, |
| 1247 |
{ |
| 1248 |
if self.first.is_some() { |
| 1249 |
seed.deserialize(serde::de::value::StringDeserializer::new( |
| 1250 |
MAP_KEY_IDENTIFIER.to_string(), |
| 1251 |
)) |
| 1252 |
.map(Some) |
| 1253 |
} else { |
| 1254 |
self.delegate.next_key_seed(seed) |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error> |
| 1259 |
where |
| 1260 |
V: DeserializeSeed<'de>, |
| 1261 |
{ |
| 1262 |
if let Some(first) = self.first.take() { |
| 1263 |
seed.deserialize(ContentDeserializer::new(first, self.is_human_readable)) |
| 1264 |
} else { |
| 1265 |
self.delegate.next_value_seed(seed) |
| 1266 |
} |
| 1267 |
} |
| 1268 |
} |
| 1269 |
|
| 1270 |
struct SeqAccessWrapper<'de, M> { |
| 1271 |
delegate: M, |
| 1272 |
is_human_readable: bool, |
| 1273 |
first: Option<DeContent<'de>>, |
| 1274 |
} |
| 1275 |
|
| 1276 |
impl<'de, S> SeqAccess<'de> for SeqAccessWrapper<'de, S> |
| 1277 |
where |
| 1278 |
S: SeqAccess<'de>, |
| 1279 |
{ |
| 1280 |
type Error = S::Error; |
| 1281 |
|
| 1282 |
fn next_element_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error> |
| 1283 |
where |
| 1284 |
K: DeserializeSeed<'de>, |
| 1285 |
{ |
| 1286 |
if let Some(first) = self.first.take() { |
| 1287 |
seed.deserialize(ContentDeserializer::new(first, self.is_human_readable)) |
| 1288 |
.map(Some) |
| 1289 |
} else { |
| 1290 |
self.delegate.next_element_seed(seed) |
| 1291 |
} |
| 1292 |
} |
| 1293 |
} |