summaryrefslogtreecommitdiffstats
path: root/third_party/rust/hashlink/src/serde.rs
blob: f44ebb3a65b09429608ccc55ad587bfef59d41e2 (plain)
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
use std::{
    fmt::{self, Formatter},
    hash::{BuildHasher, Hash},
    marker::PhantomData,
};

use serde::{
    de::{MapAccess, SeqAccess, Visitor},
    ser::{SerializeMap, SerializeSeq},
    Deserialize, Deserializer, Serialize, Serializer,
};

use crate::{LinkedHashMap, LinkedHashSet};

// LinkedHashMap impls

impl<K, V, S> Serialize for LinkedHashMap<K, V, S>
where
    K: Serialize + Eq + Hash,
    V: Serialize,
    S: BuildHasher,
{
    #[inline]
    fn serialize<T: Serializer>(&self, serializer: T) -> Result<T::Ok, T::Error> {
        let mut map_serializer = serializer.serialize_map(Some(self.len()))?;
        for (k, v) in self {
            map_serializer.serialize_key(k)?;
            map_serializer.serialize_value(v)?;
        }
        map_serializer.end()
    }
}

impl<'de, K, V, S> Deserialize<'de> for LinkedHashMap<K, V, S>
where
    K: Deserialize<'de> + Eq + Hash,
    V: Deserialize<'de>,
    S: BuildHasher + Default,
{
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Debug)]
        pub struct LinkedHashMapVisitor<K, V, S> {
            marker: PhantomData<LinkedHashMap<K, V, S>>,
        }

        impl<K, V, S> LinkedHashMapVisitor<K, V, S> {
            fn new() -> Self {
                LinkedHashMapVisitor {
                    marker: PhantomData,
                }
            }
        }

        impl<K, V, S> Default for LinkedHashMapVisitor<K, V, S> {
            fn default() -> Self {
                Self::new()
            }
        }

        impl<'de, K, V, S> Visitor<'de> for LinkedHashMapVisitor<K, V, S>
        where
            K: Deserialize<'de> + Eq + Hash,
            V: Deserialize<'de>,
            S: BuildHasher + Default,
        {
            type Value = LinkedHashMap<K, V, S>;

            fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
                write!(formatter, "a map")
            }

            #[inline]
            fn visit_map<M: MapAccess<'de>>(self, mut map: M) -> Result<Self::Value, M::Error> {
                let mut values = LinkedHashMap::with_capacity_and_hasher(
                    map.size_hint().unwrap_or(0),
                    S::default(),
                );

                while let Some((k, v)) = map.next_entry()? {
                    values.insert(k, v);
                }

                Ok(values)
            }
        }

        deserializer.deserialize_map(LinkedHashMapVisitor::default())
    }
}

// LinkedHashSet impls

impl<T, S> Serialize for LinkedHashSet<T, S>
where
    T: Serialize + Eq + Hash,
    S: BuildHasher,
{
    #[inline]
    fn serialize<U: Serializer>(&self, serializer: U) -> Result<U::Ok, U::Error> {
        let mut seq_serializer = serializer.serialize_seq(Some(self.len()))?;
        for v in self {
            seq_serializer.serialize_element(v)?;
        }
        seq_serializer.end()
    }
}

impl<'de, T, S> Deserialize<'de> for LinkedHashSet<T, S>
where
    T: Deserialize<'de> + Eq + Hash,
    S: BuildHasher + Default,
{
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Debug)]
        pub struct LinkedHashSetVisitor<T, S> {
            marker: PhantomData<LinkedHashSet<T, S>>,
        }

        impl<T, S> LinkedHashSetVisitor<T, S> {
            fn new() -> Self {
                LinkedHashSetVisitor {
                    marker: PhantomData,
                }
            }
        }

        impl<T, S> Default for LinkedHashSetVisitor<T, S> {
            fn default() -> Self {
                Self::new()
            }
        }

        impl<'de, T, S> Visitor<'de> for LinkedHashSetVisitor<T, S>
        where
            T: Deserialize<'de> + Eq + Hash,
            S: BuildHasher + Default,
        {
            type Value = LinkedHashSet<T, S>;

            fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
                write!(formatter, "a sequence")
            }

            #[inline]
            fn visit_seq<SA: SeqAccess<'de>>(self, mut seq: SA) -> Result<Self::Value, SA::Error> {
                let mut values = LinkedHashSet::with_capacity_and_hasher(
                    seq.size_hint().unwrap_or(0),
                    S::default(),
                );

                while let Some(v) = seq.next_element()? {
                    values.insert(v);
                }

                Ok(values)
            }
        }

        deserializer.deserialize_seq(LinkedHashSetVisitor::default())
    }
}