|
| 1 | +use alloc::{collections::BTreeMap, format}; |
| 2 | +use core::{ |
| 3 | + fmt::{self, Debug}, |
| 4 | + marker::PhantomData, |
| 5 | + ops::Deref, |
| 6 | +}; |
| 7 | + |
| 8 | +use serde::{ |
| 9 | + de::{self, SeqAccess, Visitor}, |
| 10 | + ser::SerializeSeq, |
| 11 | + Deserialize, Deserializer, Serialize, Serializer, |
| 12 | +}; |
| 13 | + |
| 14 | +/// A wrapper for [`BTreeMap`](`alloc::collections::BTreeMap`) |
| 15 | +/// that allows it to be serialized in a wider range of formats. |
| 16 | +/// |
| 17 | +/// Some serialization formats/implementations (e.g. `serde_asn1_der`) do not support serializing maps. |
| 18 | +/// This implementation serializes maps as sequences of key/value pairs, |
| 19 | +/// and checks for duplicate keys on deserialization. |
| 20 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 21 | +pub(crate) struct SerializableMap<K, V>(BTreeMap<K, V>); |
| 22 | + |
| 23 | +impl<K, V> From<BTreeMap<K, V>> for SerializableMap<K, V> { |
| 24 | + fn from(source: BTreeMap<K, V>) -> Self { |
| 25 | + Self(source) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl<K, V> Deref for SerializableMap<K, V> { |
| 30 | + type Target = BTreeMap<K, V>; |
| 31 | + |
| 32 | + fn deref(&self) -> &Self::Target { |
| 33 | + &self.0 |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl<K, V> Serialize for SerializableMap<K, V> |
| 38 | +where |
| 39 | + K: Serialize, |
| 40 | + V: Serialize, |
| 41 | +{ |
| 42 | + fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { |
| 43 | + // TODO: an error here can be covered by a custom `Serializer`, |
| 44 | + // but that's a lot of extra code to test just one line. |
| 45 | + // Is there an easier way? |
| 46 | + // Alternatively, we wait until `#[coverage]` is stabilized. |
| 47 | + let mut seq = serializer.serialize_seq(Some(self.0.len()))?; |
| 48 | + for e in self.0.iter() { |
| 49 | + seq.serialize_element(&e)?; |
| 50 | + } |
| 51 | + seq.end() |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +struct MapVisitor<K, V>(PhantomData<(K, V)>); |
| 56 | + |
| 57 | +impl<'de, K, V> Visitor<'de> for MapVisitor<K, V> |
| 58 | +where |
| 59 | + K: Debug + Clone + Ord + Deserialize<'de>, |
| 60 | + V: Deserialize<'de>, |
| 61 | +{ |
| 62 | + type Value = SerializableMap<K, V>; |
| 63 | + |
| 64 | + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 65 | + formatter.write_str("A map serialized as a list of pairs") |
| 66 | + } |
| 67 | + |
| 68 | + fn visit_seq<M>(self, mut access: M) -> Result<Self::Value, M::Error> |
| 69 | + where |
| 70 | + M: SeqAccess<'de>, |
| 71 | + { |
| 72 | + let mut map = SerializableMap(BTreeMap::new()); |
| 73 | + |
| 74 | + while let Some((key, value)) = access.next_element::<(K, V)>()? { |
| 75 | + // This clone, and the consequent `Debug` bound on the impl can be removed |
| 76 | + // when `BTreeMap::try_insert()` is stabilized. |
| 77 | + // Or we could call `BTreeMap::contains()` first, but it's more expensive than cloning a key |
| 78 | + // (which will be short). |
| 79 | + let key_clone = key.clone(); |
| 80 | + if map.0.insert(key, value).is_some() { |
| 81 | + return Err(de::Error::custom(format!("Duplicate key: {key_clone:?}"))); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + Ok(map) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl<'de, K, V> Deserialize<'de> for SerializableMap<K, V> |
| 90 | +where |
| 91 | + K: Debug + Clone + Ord + Deserialize<'de>, |
| 92 | + V: Deserialize<'de>, |
| 93 | +{ |
| 94 | + fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { |
| 95 | + deserializer.deserialize_seq(MapVisitor::<K, V>(PhantomData)) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +#[cfg(test)] |
| 100 | +mod tests { |
| 101 | + use alloc::collections::BTreeMap; |
| 102 | + use alloc::string::{String, ToString}; |
| 103 | + use alloc::{vec, vec::Vec}; |
| 104 | + |
| 105 | + use serde::{Deserialize, Serialize}; |
| 106 | + |
| 107 | + use super::SerializableMap; |
| 108 | + |
| 109 | + fn asn1_serialize<T: Serialize>(value: &T) -> Result<Vec<u8>, String> { |
| 110 | + serde_asn1_der::to_vec(value).map_err(|err| err.to_string()) |
| 111 | + } |
| 112 | + |
| 113 | + fn asn1_deserialize<'de, T: Deserialize<'de>>(bytes: &'de [u8]) -> Result<T, String> { |
| 114 | + serde_asn1_der::from_bytes(bytes).map_err(|err| err.to_string()) |
| 115 | + } |
| 116 | + |
| 117 | + fn json_serialize<T: Serialize>(value: &T) -> String { |
| 118 | + serde_json::to_string(value).unwrap() |
| 119 | + } |
| 120 | + |
| 121 | + fn json_deserialize<'de, T: Deserialize<'de>>(string: &'de str) -> Result<T, String> { |
| 122 | + serde_json::from_str::<T>(string).map_err(|err| err.to_string()) |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn roundtrip() { |
| 127 | + let map = SerializableMap::<u8, u8>(BTreeMap::from([(120, 130), (140, 150)])); |
| 128 | + let map_serialized = asn1_serialize(&map).unwrap(); |
| 129 | + let map_back = asn1_deserialize(&map_serialized).unwrap(); |
| 130 | + assert_eq!(map, map_back); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn representation() { |
| 135 | + // Test that the map is represented identically to a vector of tuples in the serialized data. |
| 136 | + let map = SerializableMap::<u8, u8>(BTreeMap::from([(120, 130), (140, 150)])); |
| 137 | + let map_as_vec = vec![(120u8, 130u8), (140, 150)]; |
| 138 | + let map_serialized = asn1_serialize(&map).unwrap(); |
| 139 | + let map_as_vec_serialized = asn1_serialize(&map_as_vec).unwrap(); |
| 140 | + assert_eq!(map_serialized, map_as_vec_serialized); |
| 141 | + } |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn duplicate_key() { |
| 145 | + let map_as_vec = vec![(120u8, 130u8), (120, 150)]; |
| 146 | + let map_serialized = asn1_serialize(&map_as_vec).unwrap(); |
| 147 | + assert_eq!( |
| 148 | + asn1_deserialize::<SerializableMap<u8, u8>>(&map_serialized).unwrap_err(), |
| 149 | + "Serde error: Duplicate key: 120" |
| 150 | + ); |
| 151 | + } |
| 152 | + |
| 153 | + #[test] |
| 154 | + fn serialize_error() { |
| 155 | + // Coverage for possible errors during serialization. |
| 156 | + // ASN.1 cannot serialize BTreeMap, so we will use it to trigger an error. |
| 157 | + let map = SerializableMap(BTreeMap::from([(1u8, BTreeMap::from([(2u8, 3u8)]))])); |
| 158 | + assert!(asn1_serialize(&map) |
| 159 | + .unwrap_err() |
| 160 | + .starts_with("Unsupported Maps variants are not supported by this implementation")); |
| 161 | + } |
| 162 | + |
| 163 | + #[test] |
| 164 | + fn unexpected_sequence_element() { |
| 165 | + // The deserializer will encounter an integer where it expects a tuple. |
| 166 | + let not_map_serialized = asn1_serialize(&[1u64, 2u64]).unwrap(); |
| 167 | + assert!(asn1_deserialize::<SerializableMap<u8, u8>>(¬_map_serialized) |
| 168 | + .unwrap_err() |
| 169 | + .contains("Invalid encoding DER object is not a valid sequence"),); |
| 170 | + } |
| 171 | + |
| 172 | + #[test] |
| 173 | + fn unexpected_type() { |
| 174 | + // Have to use JSON and not ASN1 here because `serde_asn1_der` doesn't seem to trigger `Visitor::expecting()`. |
| 175 | + let not_map_serialized = json_serialize(&1); |
| 176 | + assert_eq!( |
| 177 | + json_deserialize::<SerializableMap<u8, u8>>(¬_map_serialized).unwrap_err(), |
| 178 | + "invalid type: integer `1`, expected A map serialized as a list of pairs at line 1 column 1" |
| 179 | + ); |
| 180 | + } |
| 181 | +} |
0 commit comments