1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//! Implementations of `serde::Serialize` and `serde::Deserialize` for
//! `BiHashMap` and `BiBTreeMap`.
//!
//! You do not need to import anything from this module to use this
//! functionality, simply enable the `serde` feature in your dependency
//! manifest. Note that currently, this requires the `std` feature to also be
//! enabled, and thus cannot be used in `no_std` enviroments.
//!
//! # Examples
//!
//! You can easily serialize and deserialize bimaps with any serde-compatbile
//! serializer or deserializer.
//!
//! Serializing and deserializing a [`BiHashMap`]:
//!
//! ```
//! # use bimap::BiHashMap;
//! // create a new bimap
//! let mut map = BiHashMap::new();
//!
//! // insert some pairs
//! map.insert('A', 1);
//! map.insert('B', 2);
//! map.insert('C', 3);
//!
//! // convert the bimap to json
//! let json = serde_json::to_string(&map).unwrap();
//!
//! // convert the json back into a bimap
//! let map2 = serde_json::from_str(&json).unwrap();
//!
//! // check that the two bimaps are equal
//! assert_eq!(map, map2);
//! ```
//!
//! Serializing and deserializing a [`BiBTreeMap`]:
//! ```
//! # use bimap::BiBTreeMap;
//! // create a new bimap
//! let mut map = BiBTreeMap::new();
//!
//! // insert some pairs
//! map.insert('A', 3);
//! map.insert('B', 2);
//! map.insert('C', 1);
//!
//! // convert the bimap to json
//! let json = serde_json::to_string(&map).unwrap();
//!
//! // convert the json back into a bimap
//! let map2 = serde_json::from_str(&json).unwrap();
//!
//! // check that the two bimaps are equal
//! assert_eq!(map, map2);
//! ```
//!
//! Of course, this is only possible for bimaps where the values also implement
//! `Serialize` and `Deserialize` respectively:
//!
//! ```compile_fail
//! # use bimap::BiHashMap;
//! // this type doesn't implement Serialize or Deserialize!
//! #[derive(PartialEq, Eq, Hash)]
//! enum MyEnum { A, B, C }
//!
//! // create a bimap and add some pairs
//! let mut map = BiHashMap::new();
//! map.insert(MyEnum::A, 1);
//! map.insert(MyEnum::B, 2);
//! map.insert(MyEnum::C, 3);
//!
//! // this line will cause the code to fail to compile
//! let json = serde_json::to_string(&map).unwrap();
//! ```
//!
//! # Implementation details
//!
//! Bimaps are serialized and deserialized as a map data type in serde.
//! Consequentially, it is possible to serialize and deserialize bimaps to/from
//! other types that are represented the same way. *This is considered an
//! implementation detail and should not be relied upon.*
//!
//! For example, a bimap can be deserialized from the serialized form of a
//! standard [`HashMap`]. However, *deserializing a bimap silently overwrites
//! any conflicting pairs*, leading to non-deterministic results.
//! ```
//! # use std::collections::HashMap;
//! # use bimap::BiHashMap;
//! // construct a regular map
//! let mut map = HashMap::new();
//!
//! // insert some entries
//! // note that both 'B' and 'C' are associated with the value 2 here
//! map.insert('A', 1);
//! map.insert('B', 2);
//! map.insert('C', 2);
//!
//! // serialize the map
//! let json = serde_json::to_string(&map).unwrap();
//!
//! // deserialize it into a bimap
//! let bimap: BiHashMap<char, i32> = serde_json::from_str(&json).unwrap();
//!
//! // deserialization succeeds, but the bimap is now in a non-deterministic
//! // state - either ('B', 2) or ('C', 2) will have been overwritten while
//! // deserializing, but this depends on the iteration order of the original
//! // HashMap that was serialized.
//!
//! // we can still demonstrate that certain properties of the bimap are still
//! // in a known state, but this shouldn't be relied upon
//! assert_eq!(bimap.len(), 2);
//! assert_eq!(bimap.get_by_left(&'A'), Some(&1));
//! assert!(bimap.get_by_left(&'B') == Some(&2) || bimap.get_by_left(&'C') == Some(&2))
//! ```
//!
//! The reverse is also possible: bimaps may be serialized and then
//! deserialized as other compatible types, such as a [`HashMap`].
//!
//! ```
//! # use std::collections::HashMap;
//! # use bimap::BiHashMap;
//! // construct a bimap
//! let mut bimap = BiHashMap::new();
//!
//! // insert some pairs
//! bimap.insert('A', 1);
//! bimap.insert('B', 2);
//! bimap.insert('C', 3);
//!
//! // serialize the bimap
//! let json = serde_json::to_string(&bimap).unwrap();
//!
//! // deserialize it as a regular map
//! let map: HashMap<char, i32> = serde_json::from_str(&json).unwrap();
//!
//! // this succeeds and the result is sensible, but this is still an
//! // implementation detail and shouldn't be relied upon.
//! assert_eq!(map.len(), 3);
//! assert_eq!(map[&'A'], 1);
//! assert_eq!(map[&'B'], 2);
//! assert_eq!(map[&'C'], 3);
//! ```
//! [`BiHashMap`]: crate::BiHashMap
//! [`BiBTreeMap`]: crate::BiBTreeMap
//! [`HashMap`]: https://doc.rust-lang.org/std/collections/struct.HashMap.html

use crate::{BiBTreeMap, BiHashMap};
use serde::{
    de::{MapAccess, Visitor},
    Deserialize, Deserializer, Serialize, Serializer,
};
use std::{
    default::Default,
    fmt::{Formatter, Result as FmtResult},
    hash::{BuildHasher, Hash},
    marker::PhantomData,
};

/// Serializer for `BiHashMap`
impl<L, R, LS, RS> Serialize for BiHashMap<L, R, LS, RS>
where
    L: Serialize + Eq + Hash,
    R: Serialize + Eq + Hash,
    LS: BuildHasher + Default,
    RS: BuildHasher + Default,
{
    fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
        ser.collect_map(self.iter())
    }
}

/// Visitor to construct `BiHashMap` from serialized map entries
struct BiHashMapVisitor<L, R, LS, RS> {
    marker: PhantomData<BiHashMap<L, R, LS, RS>>,
}

impl<'de, L, R, LS, RS> Visitor<'de> for BiHashMapVisitor<L, R, LS, RS>
where
    L: Deserialize<'de> + Eq + Hash,
    R: Deserialize<'de> + Eq + Hash,
    LS: BuildHasher + Default,
    RS: BuildHasher + Default,
{
    fn expecting(&self, f: &mut Formatter) -> FmtResult {
        write!(f, "a map")
    }

    type Value = BiHashMap<L, R, LS, RS>;
    fn visit_map<A: MapAccess<'de>>(self, mut entries: A) -> Result<Self::Value, A::Error> {
        let mut map = match entries.size_hint() {
            Some(s) => BiHashMap::<L, R, LS, RS>::with_capacity_and_hashers(
                s,
                LS::default(),
                RS::default(),
            ),
            None => BiHashMap::<L, R, LS, RS>::with_hashers(LS::default(), RS::default()),
        };
        while let Some((l, r)) = entries.next_entry()? {
            map.insert(l, r);
        }
        Ok(map)
    }
}

/// Deserializer for `BiHashMap`
impl<'de, L, R, LS, RS> Deserialize<'de> for BiHashMap<L, R, LS, RS>
where
    L: Deserialize<'de> + Eq + Hash,
    R: Deserialize<'de> + Eq + Hash,
    LS: BuildHasher + Default,
    RS: BuildHasher + Default,
{
    fn deserialize<D: Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
        de.deserialize_map(BiHashMapVisitor::<L, R, LS, RS> {
            marker: PhantomData::default(),
        })
    }
}

/// Serializer for `BiBTreeMap`
impl<L, R> Serialize for BiBTreeMap<L, R>
where
    L: Serialize + Ord,
    R: Serialize + Ord,
{
    fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
        ser.collect_map(self.iter())
    }
}

/// Visitor to construct `BiBTreeMap` from serialized map entries
struct BiBTreeMapVisitor<L, R> {
    marker: PhantomData<BiBTreeMap<L, R>>,
}

impl<'de, L, R> Visitor<'de> for BiBTreeMapVisitor<L, R>
where
    L: Deserialize<'de> + Ord,
    R: Deserialize<'de> + Ord,
{
    fn expecting(&self, f: &mut Formatter) -> FmtResult {
        write!(f, "a map")
    }

    type Value = BiBTreeMap<L, R>;
    fn visit_map<A: MapAccess<'de>>(self, mut entries: A) -> Result<Self::Value, A::Error> {
        let mut map = BiBTreeMap::new();
        while let Some((l, r)) = entries.next_entry()? {
            map.insert(l, r);
        }
        Ok(map)
    }
}

/// Deserializer for `BiBTreeMap`
impl<'de, L, R> Deserialize<'de> for BiBTreeMap<L, R>
where
    L: Deserialize<'de> + Ord,
    R: Deserialize<'de> + Ord,
{
    fn deserialize<D: Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
        de.deserialize_map(BiBTreeMapVisitor {
            marker: PhantomData::default(),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::de::value::Error;
    use std::hash::BuildHasherDefault;

    #[test]
    fn serde_hash() {
        let mut bimap = BiHashMap::new();
        bimap.insert('a', 1);
        bimap.insert('b', 2);
        bimap.insert('c', 3);

        let json = serde_json::to_string(&bimap).unwrap();
        let bimap2 = serde_json::from_str(&json).unwrap();

        assert_eq!(bimap, bimap2);
    }

    #[test]
    fn serde_hash_w_fnv_hasher() {
        let hasher_builder = BuildHasherDefault::<fnv::FnvHasher>::default();
        let mut bimap = BiHashMap::<
            char,
            u8,
            BuildHasherDefault<fnv::FnvHasher>,
            BuildHasherDefault<fnv::FnvHasher>,
        >::with_capacity_and_hashers(
            4, hasher_builder.clone(), hasher_builder.clone()
        );
        bimap.insert('f', 1);
        bimap.insert('g', 2);
        bimap.insert('h', 3);

        let json = serde_json::to_string(&bimap).unwrap();
        let bimap2 = serde_json::from_str(&json).unwrap();

        assert_eq!(bimap, bimap2);
    }

    #[test]
    fn serde_hash_w_hashbrown_hasher() {
        let hasher_builder = hashbrown::hash_map::DefaultHashBuilder::default();
        let mut bimap = BiHashMap::<
            char,
            u8,
            hashbrown::hash_map::DefaultHashBuilder,
            hashbrown::hash_map::DefaultHashBuilder,
        >::with_capacity_and_hashers(
            4, hasher_builder.clone(), hasher_builder.clone()
        );
        bimap.insert('x', 1);
        bimap.insert('y', 2);
        bimap.insert('z', 3);

        let json = serde_json::to_string(&bimap).unwrap();
        let bimap2 = serde_json::from_str(&json).unwrap();

        assert_eq!(bimap, bimap2);
    }

    #[test]
    fn serde_btree() {
        let mut bimap = BiBTreeMap::new();
        bimap.insert('a', 1);
        bimap.insert('b', 2);
        bimap.insert('c', 3);

        let json = serde_json::to_string(&bimap).unwrap();
        let bimap2 = serde_json::from_str(&json).unwrap();

        assert_eq!(bimap, bimap2);
    }

    #[test]
    fn expecting_btree() {
        let visitor = BiBTreeMapVisitor {
            marker: PhantomData::<BiBTreeMap<char, i32>>,
        };
        let error_str = format!("{:?}", visitor.visit_bool::<Error>(true));
        let expected = "Err(Error(\"invalid type: boolean `true`, expected a map\"))";
        assert_eq!(error_str, expected);
    }

    #[test]
    fn expecting_hash() {
        let visitor = BiHashMapVisitor {
            marker: PhantomData::<BiHashMap<char, i32>>,
        };
        let error_str = format!("{:?}", visitor.visit_bool::<Error>(true));
        let expected = "Err(Error(\"invalid type: boolean `true`, expected a map\"))";
        assert_eq!(error_str, expected);
    }
}