1use serde::de::{DeserializeOwned, SeqAccess, Visitor};
2use serde::ser::SerializeSeq;
3use serde::{Deserialize, Deserializer, Serialize, Serializer};
4use std::collections::HashMap;
5use std::fmt::Formatter;
6use std::hash::Hash;
7use std::marker::PhantomData;
8use std::ops::{Deref, DerefMut, Index, IndexMut};
9
10use crate::common::MapValue;
11
12
13#[derive(Debug, Clone)]
16pub struct Map<Key, Value>
17 where
18 Key: HashKey,
19{
20 inner: HashMap<Key, Value>,
21}
22
23pub trait HashKey: Eq + Hash {}
26
27impl<K, V> Map<K, V>
28 where
29 K: HashKey,
30{
31 pub fn insert(&mut self, value: V) -> Option<V>
32 where
33 K: HashKey,
34 V: MapValue<Key=K>,
35 {
36 let key: K = value.to_key();
37 self.inner.insert(key, value)
38 }
39}
40
41impl<Key, Value> From<HashMap<Key, Value>> for Map<Key, Value>
42 where
43 Key: HashKey,
44{
45 fn from(value: HashMap<Key, Value>) -> Self {
46 Map::new(value)
47 }
48}
49
50impl<Key, Value> Map<Key, Value>
51 where
52 Key: HashKey,
53{
54 pub fn new(map: HashMap<Key, Value>) -> Self {
55 Map { inner: map }
56 }
57}
58
59impl<Key, Value> Default for Map<Key, Value>
60 where
61 Key: HashKey,
62{
63 fn default() -> Self {
64 Map {
65 inner: HashMap::new(),
66 }
67 }
68}
69
70impl<Key, Value> Serialize for Map<Key, Value>
71 where
72 Key: HashKey,
73 Value: Serialize,
74{
75 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
76 where
77 S: Serializer,
78 {
79 let mut map = serializer.serialize_seq(Some(self.len()))?;
80
81 for v in self.deref().values() {
82 map.serialize_element(v)?
83 }
84
85 map.end()
86 }
87}
88
89struct MapVisitor<Key, Value>
90 where
91 Key: HashKey,
92{
93 marker: PhantomData<fn() -> Map<Key, Value>>,
94}
95
96impl<'de, Key, Value> Visitor<'de> for MapVisitor<Key, Value>
97 where
98 Key: HashKey,
99 Value: MapValue<Key=Key> + DeserializeOwned,
100{
101 type Value = Map<Key, Value>;
102
103 fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
104 write!(formatter, "MapVisitor expects to receive a map of <EnumKey, Enum> with untagged Enum variants and EnumKey serializing to Enum variants' names ")
105 }
106
107 fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
108 where
109 A: SeqAccess<'de>,
110 {
111 let map_size = seq.size_hint().unwrap_or(0);
112 let mut map: HashMap<Key, Value> = HashMap::<Key, Value>::with_capacity(map_size);
113
114 while let Some(value) = seq.next_element()? {
115 let variant: Value = value;
116 map.insert(variant.to_key(), variant);
117 }
118
119 Ok(Map::from(map))
120 }
121}
122
123impl<'de, Key, Value> Deserialize<'de> for Map<Key, Value>
124 where
125 Key: HashKey,
126 Value: MapValue<Key=Key> + DeserializeOwned,
127{
128 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
129 where
130 D: Deserializer<'de>,
131 {
132 let visitor = MapVisitor::<Key, Value> {
133 marker: PhantomData,
134 };
135 deserializer.deserialize_seq(visitor)
136 }
137}
138
139impl<Key, Value> Deref for Map<Key, Value>
140 where
141 Key: HashKey,
142{
143 type Target = HashMap<Key, Value>;
144
145 fn deref(&self) -> &Self::Target {
146 &self.inner
147 }
148}
149
150impl<Key, Value> DerefMut for Map<Key, Value>
151 where
152 Key: HashKey,
153{
154 fn deref_mut(&mut self) -> &mut Self::Target {
155 &mut self.inner
156 }
157}
158
159impl<Key, Value> Index<Key> for Map<Key, Value>
160 where
161 Key: HashKey,
162{
163 type Output = Value;
164
165 fn index(&self, index: Key) -> &Self::Output {
166 self.inner.get(&index).unwrap()
167 }
168}
169
170impl<Key, Value> IndexMut<Key> for Map<Key, Value>
171 where
172 Key: HashKey,
173{
174 fn index_mut(&mut self, index: Key) -> &mut Self::Output {
175 self.inner.get_mut(&index).unwrap()
176 }
177}
178
179impl<'a, Key, Value> IntoIterator for &'a Map<Key, Value>
180 where
181 Key: HashKey {
182 type Item = <&'a HashMap<Key, Value> as IntoIterator>::Item;
183 type IntoIter = <&'a HashMap<Key, Value> as IntoIterator>::IntoIter;
184
185 fn into_iter(self) -> Self::IntoIter {
186 (&self.inner).into_iter()
187 }
188}
189
190impl<'a, Key, Value> IntoIterator for &'a mut Map<Key, Value>
191 where
192 Key: HashKey {
193 type Item = <&'a mut HashMap<Key, Value> as IntoIterator>::Item;
194 type IntoIter = <&'a mut HashMap<Key, Value> as IntoIterator>::IntoIter;
195
196 fn into_iter(self) -> Self::IntoIter {
197 (&mut self.inner).into_iter()
198 }
199}