indexmap_nostd/
serde.rs

1use crate::IndexMap;
2use core::fmt::{self, Formatter};
3use core::marker::PhantomData;
4use serde::de::value::{MapDeserializer, SeqDeserializer};
5use serde::de::{
6    Deserialize, Deserializer, Error, IntoDeserializer, MapAccess, SeqAccess, Visitor,
7};
8use serde::ser::{Serialize, Serializer};
9
10/// Requires crate feature `"serde"`
11impl<K, V> Serialize for IndexMap<K, V>
12where
13    K: Serialize + Ord,
14    V: Serialize,
15{
16    fn serialize<T>(&self, serializer: T) -> Result<T::Ok, T::Error>
17    where
18        T: Serializer,
19    {
20        serializer.collect_map(self)
21    }
22}
23
24struct IndexMapVisitor<K, V>(PhantomData<(K, V)>);
25
26impl<'de, K, V> Visitor<'de> for IndexMapVisitor<K, V>
27where
28    K: Deserialize<'de> + Ord + Clone,
29    V: Deserialize<'de>,
30{
31    type Value = IndexMap<K, V>;
32
33    fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
34        write!(formatter, "a map")
35    }
36
37    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
38    where
39        A: MapAccess<'de>,
40    {
41        let mut values = IndexMap::with_capacity(map.size_hint().unwrap_or(0));
42        while let Some((key, value)) = map.next_entry()? {
43            values.insert(key, value);
44        }
45        Ok(values)
46    }
47}
48
49/// Requires crate feature `"serde"`
50impl<'de, K, V> Deserialize<'de> for IndexMap<K, V>
51where
52    K: Deserialize<'de> + Ord + Clone,
53    V: Deserialize<'de>,
54{
55    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
56    where
57        D: Deserializer<'de>,
58    {
59        deserializer.deserialize_map(IndexMapVisitor(PhantomData))
60    }
61}
62
63impl<'de, K, V, E> IntoDeserializer<'de, E> for IndexMap<K, V>
64where
65    K: IntoDeserializer<'de, E> + Ord,
66    V: IntoDeserializer<'de, E>,
67    E: Error,
68{
69    type Deserializer = MapDeserializer<'de, <Self as IntoIterator>::IntoIter, E>;
70
71    fn into_deserializer(self) -> Self::Deserializer {
72        MapDeserializer::new(self.into_iter())
73    }
74}
75
76use crate::IndexSet;
77
78/// Requires crate feature `"serde"`
79impl<T> Serialize for IndexSet<T>
80where
81    T: Serialize + Ord,
82{
83    fn serialize<Se>(&self, serializer: Se) -> Result<Se::Ok, Se::Error>
84    where
85        Se: Serializer,
86    {
87        serializer.collect_seq(self)
88    }
89}
90
91struct IndexSetVisitor<T>(PhantomData<T>);
92
93impl<'de, T> Visitor<'de> for IndexSetVisitor<T>
94where
95    T: Deserialize<'de> + Ord + Clone,
96{
97    type Value = IndexSet<T>;
98
99    fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
100        write!(formatter, "a set")
101    }
102
103    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
104    where
105        A: SeqAccess<'de>,
106    {
107        let mut values = IndexSet::with_capacity(seq.size_hint().unwrap_or(0));
108        while let Some(value) = seq.next_element()? {
109            values.insert(value);
110        }
111        Ok(values)
112    }
113}
114
115/// Requires crate feature `"serde"`
116impl<'de, T> Deserialize<'de> for IndexSet<T>
117where
118    T: Deserialize<'de> + Ord + Clone,
119{
120    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
121    where
122        D: Deserializer<'de>,
123    {
124        deserializer.deserialize_seq(IndexSetVisitor(PhantomData))
125    }
126}
127
128impl<'de, T, E> IntoDeserializer<'de, E> for IndexSet<T>
129where
130    T: IntoDeserializer<'de, E> + Ord,
131    E: Error,
132{
133    type Deserializer = SeqDeserializer<<Self as IntoIterator>::IntoIter, E>;
134
135    fn into_deserializer(self) -> Self::Deserializer {
136        SeqDeserializer::new(self.into_iter())
137    }
138}