diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /third_party/rust/enum-map/src/serde.rs | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/enum-map/src/serde.rs')
-rw-r--r-- | third_party/rust/enum-map/src/serde.rs | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/third_party/rust/enum-map/src/serde.rs b/third_party/rust/enum-map/src/serde.rs new file mode 100644 index 0000000000..9ac434d5d0 --- /dev/null +++ b/third_party/rust/enum-map/src/serde.rs @@ -0,0 +1,98 @@ +// SPDX-FileCopyrightText: 2017 - 2023 Kamila Borowska <kamila@borowska.pw> +// SPDX-FileCopyrightText: 2021 Bruno CorrĂȘa Zimmermann <brunoczim@gmail.com> +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +use crate::{enum_map, EnumArray, EnumMap}; +use core::fmt; +use core::marker::PhantomData; +use serde::de::{self, Deserialize, Deserializer, Error, MapAccess, SeqAccess}; +use serde::ser::{Serialize, SerializeTuple, Serializer}; + +/// Requires crate feature `"serde"` +impl<K: EnumArray<V> + Serialize, V: Serialize> Serialize for EnumMap<K, V> { + fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { + if serializer.is_human_readable() { + serializer.collect_map(self) + } else { + let mut tup = serializer.serialize_tuple(self.len())?; + for value in self.values() { + tup.serialize_element(value)?; + } + tup.end() + } + } +} + +/// Requires crate feature `"serde"` +impl<'de, K, V> Deserialize<'de> for EnumMap<K, V> +where + K: EnumArray<V> + EnumArray<Option<V>> + Deserialize<'de>, + V: Deserialize<'de>, +{ + fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { + if deserializer.is_human_readable() { + deserializer.deserialize_map(HumanReadableVisitor(PhantomData)) + } else { + deserializer.deserialize_tuple(K::LENGTH, CompactVisitor(PhantomData)) + } + } +} + +struct HumanReadableVisitor<K, V>(PhantomData<(K, V)>); + +impl<'de, K, V> de::Visitor<'de> for HumanReadableVisitor<K, V> +where + K: EnumArray<V> + EnumArray<Option<V>> + Deserialize<'de>, + V: Deserialize<'de>, +{ + type Value = EnumMap<K, V>; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "a map") + } + + fn visit_map<M: MapAccess<'de>>(self, mut access: M) -> Result<Self::Value, M::Error> { + let mut entries = EnumMap::default(); + while let Some((key, value)) = access.next_entry()? { + entries[key] = Some(value); + } + for value in entries.values() { + value + .as_ref() + .ok_or_else(|| M::Error::custom("key not specified"))?; + } + Ok(enum_map! { key => entries[key].take().unwrap() }) + } +} + +struct CompactVisitor<K, V>(PhantomData<(K, V)>); + +impl<'de, K, V> de::Visitor<'de> for CompactVisitor<K, V> +where + K: EnumArray<V> + EnumArray<Option<V>> + Deserialize<'de>, + V: Deserialize<'de>, +{ + type Value = EnumMap<K, V>; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "a sequence") + } + + fn visit_seq<M: SeqAccess<'de>>(self, mut access: M) -> Result<Self::Value, M::Error> { + let mut entries = EnumMap::default(); + let len = entries.len(); + { + let mut iter = entries.values_mut(); + while let Some(place) = iter.next() { + *place = Some(access.next_element()?.ok_or_else(|| { + M::Error::invalid_length( + len - iter.len() - 1, + &"a sequence with as many elements as there are variants", + ) + })?); + } + } + Ok(enum_map! { key => entries[key].take().unwrap() }) + } +} |