ssi_data_integrity_core/
de.rs

1use std::marker::PhantomData;
2
3use crate::{CryptographicSuite, DataIntegrity, DeserializeCryptographicSuite, Proofs};
4use serde::Deserialize;
5
6impl<'de, T, S> Deserialize<'de> for DataIntegrity<T, S>
7where
8    T: Deserialize<'de>,
9    S: DeserializeCryptographicSuite<'de>,
10{
11    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
12    where
13        D: serde::de::Deserializer<'de>,
14    {
15        deserializer.deserialize_map(Visitor(PhantomData))
16    }
17}
18
19struct Visitor<T, S>(PhantomData<(T, S)>);
20
21impl<'de, T, S> serde::de::Visitor<'de> for Visitor<T, S>
22where
23    T: Deserialize<'de>,
24    S: DeserializeCryptographicSuite<'de>,
25{
26    type Value = DataIntegrity<T, S>;
27
28    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
29        write!(formatter, "data integrity document")
30    }
31
32    fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
33    where
34        A: serde::de::MapAccess<'de>,
35    {
36        let mut deserializer = Deserializer {
37            inner: map,
38            proofs: Proofs::default(),
39            de: PhantomData,
40        };
41
42        let claims = T::deserialize(&mut deserializer)?;
43        let proofs = deserializer.into_proofs()?;
44
45        Ok(DataIntegrity { claims, proofs })
46    }
47}
48
49struct Deserializer<'de, D, S: CryptographicSuite> {
50    inner: D,
51    proofs: Proofs<S>,
52    de: PhantomData<&'de ()>,
53}
54
55impl<'de, D: serde::de::MapAccess<'de>, S: DeserializeCryptographicSuite<'de>>
56    Deserializer<'de, D, S>
57{
58    fn into_proofs(mut self) -> Result<Proofs<S>, D::Error> {
59        while let Some(key) = self.inner.next_key::<String>()? {
60            if key == "proof" {
61                let proofs: Proofs<S> = self.inner.next_value()?;
62                self.proofs.extend(proofs)
63            }
64        }
65
66        Ok(self.proofs)
67    }
68}
69
70impl<'de, D: serde::de::MapAccess<'de>, S> serde::de::MapAccess<'de> for Deserializer<'de, D, S>
71where
72    S: DeserializeCryptographicSuite<'de>,
73{
74    type Error = D::Error;
75
76    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
77    where
78        K: serde::de::DeserializeSeed<'de>,
79    {
80        loop {
81            match self.inner.next_key::<String>()? {
82                Some(key) if key == "proof" => {
83                    let proofs: Proofs<S> = self.inner.next_value()?;
84                    self.proofs.extend(proofs)
85                }
86                Some(key) => {
87                    break seed
88                        .deserialize(serde::de::value::StringDeserializer::new(key))
89                        .map(Some)
90                }
91                None => break Ok(None),
92            }
93        }
94    }
95
96    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
97    where
98        V: serde::de::DeserializeSeed<'de>,
99    {
100        self.inner.next_value_seed(seed)
101    }
102}
103
104impl<'de, D: serde::de::MapAccess<'de>, S> serde::Deserializer<'de> for &mut Deserializer<'de, D, S>
105where
106    S: DeserializeCryptographicSuite<'de>,
107{
108    type Error = D::Error;
109
110    fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
111    where
112        V: serde::de::Visitor<'de>,
113    {
114        visitor.visit_map(self)
115    }
116
117    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
118    where
119        V: serde::de::Visitor<'de>,
120    {
121        visitor.visit_map(self)
122    }
123
124    /// Hint that the `Deserialize` type is expecting a `bool` value.
125    fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
126    where
127        V: serde::de::Visitor<'de>,
128    {
129        Err(serde::de::Error::invalid_type(
130            serde::de::Unexpected::Map,
131            &visitor,
132        ))
133    }
134
135    /// Hint that the `Deserialize` type is expecting an `i8` value.
136    fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
137    where
138        V: serde::de::Visitor<'de>,
139    {
140        Err(serde::de::Error::invalid_type(
141            serde::de::Unexpected::Map,
142            &visitor,
143        ))
144    }
145
146    /// Hint that the `Deserialize` type is expecting an `i16` value.
147    fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
148    where
149        V: serde::de::Visitor<'de>,
150    {
151        Err(serde::de::Error::invalid_type(
152            serde::de::Unexpected::Map,
153            &visitor,
154        ))
155    }
156
157    /// Hint that the `Deserialize` type is expecting an `i32` value.
158    fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
159    where
160        V: serde::de::Visitor<'de>,
161    {
162        Err(serde::de::Error::invalid_type(
163            serde::de::Unexpected::Map,
164            &visitor,
165        ))
166    }
167
168    /// Hint that the `Deserialize` type is expecting an `i64` value.
169    fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
170    where
171        V: serde::de::Visitor<'de>,
172    {
173        Err(serde::de::Error::invalid_type(
174            serde::de::Unexpected::Map,
175            &visitor,
176        ))
177    }
178
179    /// Hint that the `Deserialize` type is expecting an `i128` value.
180    ///
181    /// The default behavior unconditionally returns an error.
182    fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
183    where
184        V: serde::de::Visitor<'de>,
185    {
186        let _ = visitor;
187        Err(serde::de::Error::custom("i128 is not supported"))
188    }
189
190    /// Hint that the `Deserialize` type is expecting a `u8` value.
191    fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
192    where
193        V: serde::de::Visitor<'de>,
194    {
195        Err(serde::de::Error::invalid_type(
196            serde::de::Unexpected::Map,
197            &visitor,
198        ))
199    }
200
201    /// Hint that the `Deserialize` type is expecting a `u16` value.
202    fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
203    where
204        V: serde::de::Visitor<'de>,
205    {
206        Err(serde::de::Error::invalid_type(
207            serde::de::Unexpected::Map,
208            &visitor,
209        ))
210    }
211
212    /// Hint that the `Deserialize` type is expecting a `u32` value.
213    fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
214    where
215        V: serde::de::Visitor<'de>,
216    {
217        Err(serde::de::Error::invalid_type(
218            serde::de::Unexpected::Map,
219            &visitor,
220        ))
221    }
222
223    /// Hint that the `Deserialize` type is expecting a `u64` value.
224    fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
225    where
226        V: serde::de::Visitor<'de>,
227    {
228        Err(serde::de::Error::invalid_type(
229            serde::de::Unexpected::Map,
230            &visitor,
231        ))
232    }
233
234    /// Hint that the `Deserialize` type is expecting an `u128` value.
235    ///
236    /// The default behavior unconditionally returns an error.
237    fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
238    where
239        V: serde::de::Visitor<'de>,
240    {
241        let _ = visitor;
242        Err(serde::de::Error::custom("u128 is not supported"))
243    }
244
245    /// Hint that the `Deserialize` type is expecting a `f32` value.
246    fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
247    where
248        V: serde::de::Visitor<'de>,
249    {
250        Err(serde::de::Error::invalid_type(
251            serde::de::Unexpected::Map,
252            &visitor,
253        ))
254    }
255
256    /// Hint that the `Deserialize` type is expecting a `f64` value.
257    fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
258    where
259        V: serde::de::Visitor<'de>,
260    {
261        Err(serde::de::Error::invalid_type(
262            serde::de::Unexpected::Map,
263            &visitor,
264        ))
265    }
266
267    /// Hint that the `Deserialize` type is expecting a `char` value.
268    fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
269    where
270        V: serde::de::Visitor<'de>,
271    {
272        Err(serde::de::Error::invalid_type(
273            serde::de::Unexpected::Map,
274            &visitor,
275        ))
276    }
277
278    /// Hint that the `Deserialize` type is expecting a string value and does
279    /// not benefit from taking ownership of buffered data owned by the
280    /// `Deserializer`.
281    ///
282    /// If the `Visitor` would benefit from taking ownership of `String` data,
283    /// indicate this to the `Deserializer` by using `deserialize_string`
284    /// instead.
285    fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
286    where
287        V: serde::de::Visitor<'de>,
288    {
289        Err(serde::de::Error::invalid_type(
290            serde::de::Unexpected::Map,
291            &visitor,
292        ))
293    }
294
295    /// Hint that the `Deserialize` type is expecting a string value and would
296    /// benefit from taking ownership of buffered data owned by the
297    /// `Deserializer`.
298    ///
299    /// If the `Visitor` would not benefit from taking ownership of `String`
300    /// data, indicate that to the `Deserializer` by using `deserialize_str`
301    /// instead.
302    fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
303    where
304        V: serde::de::Visitor<'de>,
305    {
306        Err(serde::de::Error::invalid_type(
307            serde::de::Unexpected::Map,
308            &visitor,
309        ))
310    }
311
312    /// Hint that the `Deserialize` type is expecting a byte array and does not
313    /// benefit from taking ownership of buffered data owned by the
314    /// `Deserializer`.
315    ///
316    /// If the `Visitor` would benefit from taking ownership of `Vec<u8>` data,
317    /// indicate this to the `Deserializer` by using `deserialize_byte_buf`
318    /// instead.
319    fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
320    where
321        V: serde::de::Visitor<'de>,
322    {
323        Err(serde::de::Error::invalid_type(
324            serde::de::Unexpected::Map,
325            &visitor,
326        ))
327    }
328
329    /// Hint that the `Deserialize` type is expecting a byte array and would
330    /// benefit from taking ownership of buffered data owned by the
331    /// `Deserializer`.
332    ///
333    /// If the `Visitor` would not benefit from taking ownership of `Vec<u8>`
334    /// data, indicate that to the `Deserializer` by using `deserialize_bytes`
335    /// instead.
336    fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
337    where
338        V: serde::de::Visitor<'de>,
339    {
340        Err(serde::de::Error::invalid_type(
341            serde::de::Unexpected::Map,
342            &visitor,
343        ))
344    }
345
346    /// Hint that the `Deserialize` type is expecting an optional value.
347    ///
348    /// This allows deserializers that encode an optional value as a nullable
349    /// value to convert the null value into `None` and a regular value into
350    /// `Some(value)`.
351    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
352    where
353        V: serde::de::Visitor<'de>,
354    {
355        Err(serde::de::Error::invalid_type(
356            serde::de::Unexpected::Map,
357            &visitor,
358        ))
359    }
360
361    /// Hint that the `Deserialize` type is expecting a unit value.
362    fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
363    where
364        V: serde::de::Visitor<'de>,
365    {
366        visitor.visit_unit()
367    }
368
369    /// Hint that the `Deserialize` type is expecting a unit struct with a
370    /// particular name.
371    fn deserialize_unit_struct<V>(
372        self,
373        _name: &'static str,
374        visitor: V,
375    ) -> Result<V::Value, Self::Error>
376    where
377        V: serde::de::Visitor<'de>,
378    {
379        Err(serde::de::Error::invalid_type(
380            serde::de::Unexpected::Map,
381            &visitor,
382        ))
383    }
384
385    /// Hint that the `Deserialize` type is expecting a newtype struct with a
386    /// particular name.
387    fn deserialize_newtype_struct<V>(
388        self,
389        _name: &'static str,
390        visitor: V,
391    ) -> Result<V::Value, Self::Error>
392    where
393        V: serde::de::Visitor<'de>,
394    {
395        Err(serde::de::Error::invalid_type(
396            serde::de::Unexpected::Map,
397            &visitor,
398        ))
399    }
400
401    /// Hint that the `Deserialize` type is expecting a sequence of values.
402    fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
403    where
404        V: serde::de::Visitor<'de>,
405    {
406        Err(serde::de::Error::invalid_type(
407            serde::de::Unexpected::Map,
408            &visitor,
409        ))
410    }
411
412    /// Hint that the `Deserialize` type is expecting a sequence of values and
413    /// knows how many values there are without looking at the serialized data.
414    fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
415    where
416        V: serde::de::Visitor<'de>,
417    {
418        Err(serde::de::Error::invalid_type(
419            serde::de::Unexpected::Map,
420            &visitor,
421        ))
422    }
423
424    /// Hint that the `Deserialize` type is expecting a tuple struct with a
425    /// particular name and number of fields.
426    fn deserialize_tuple_struct<V>(
427        self,
428        _name: &'static str,
429        _len: usize,
430        visitor: V,
431    ) -> Result<V::Value, Self::Error>
432    where
433        V: serde::de::Visitor<'de>,
434    {
435        Err(serde::de::Error::invalid_type(
436            serde::de::Unexpected::Map,
437            &visitor,
438        ))
439    }
440
441    /// Hint that the `Deserialize` type is expecting a struct with a particular
442    /// name and fields.
443    fn deserialize_struct<V>(
444        self,
445        _name: &'static str,
446        _fields: &'static [&'static str],
447        visitor: V,
448    ) -> Result<V::Value, Self::Error>
449    where
450        V: serde::de::Visitor<'de>,
451    {
452        visitor.visit_map(self)
453    }
454
455    /// Hint that the `Deserialize` type is expecting an enum value with a
456    /// particular name and possible variants.
457    fn deserialize_enum<V>(
458        self,
459        _name: &'static str,
460        _variants: &'static [&'static str],
461        visitor: V,
462    ) -> Result<V::Value, Self::Error>
463    where
464        V: serde::de::Visitor<'de>,
465    {
466        Err(serde::de::Error::invalid_type(
467            serde::de::Unexpected::Map,
468            &visitor,
469        ))
470    }
471
472    /// Hint that the `Deserialize` type is expecting the name of a struct
473    /// field or the discriminant of an enum variant.
474    fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
475    where
476        V: serde::de::Visitor<'de>,
477    {
478        Err(serde::de::Error::invalid_type(
479            serde::de::Unexpected::Map,
480            &visitor,
481        ))
482    }
483
484    /// Hint that the `Deserialize` type needs to deserialize a value whose type
485    /// doesn't matter because it is ignored.
486    ///
487    /// Deserializers for non-self-describing formats may not support this mode.
488    fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
489    where
490        V: serde::de::Visitor<'de>,
491    {
492        Err(serde::de::Error::invalid_type(
493            serde::de::Unexpected::Map,
494            &visitor,
495        ))
496    }
497}