1 | use serde::de::value::BorrowedStrDeserializer; |
2 | use serde::de::IntoDeserializer as _; |
3 | |
4 | use super::Error; |
5 | |
6 | pub(crate) struct SpannedDeserializer<'de, T: serde::de::IntoDeserializer<'de, Error>> { |
7 | phantom_data: std::marker::PhantomData<&'de ()>, |
8 | start: Option<usize>, |
9 | end: Option<usize>, |
10 | value: Option<T>, |
11 | } |
12 | |
13 | impl<'de, T> SpannedDeserializer<'de, T> |
14 | where |
15 | T: serde::de::IntoDeserializer<'de, Error>, |
16 | { |
17 | pub(crate) fn new(value: T, span: std::ops::Range<usize>) -> Self { |
18 | Self { |
19 | phantom_data: Default::default(), |
20 | start: Some(span.start), |
21 | end: Some(span.end), |
22 | value: Some(value), |
23 | } |
24 | } |
25 | } |
26 | |
27 | impl<'de, T> serde::de::MapAccess<'de> for SpannedDeserializer<'de, T> |
28 | where |
29 | T: serde::de::IntoDeserializer<'de, Error>, |
30 | { |
31 | type Error = Error; |
32 | fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error> |
33 | where |
34 | K: serde::de::DeserializeSeed<'de>, |
35 | { |
36 | if self.start.is_some() { |
37 | seed.deserialize(BorrowedStrDeserializer::new( |
38 | serde_spanned::__unstable::START_FIELD, |
39 | )) |
40 | .map(Some) |
41 | } else if self.end.is_some() { |
42 | seed.deserialize(BorrowedStrDeserializer::new( |
43 | serde_spanned::__unstable::END_FIELD, |
44 | )) |
45 | .map(Some) |
46 | } else if self.value.is_some() { |
47 | seed.deserialize(BorrowedStrDeserializer::new( |
48 | serde_spanned::__unstable::VALUE_FIELD, |
49 | )) |
50 | .map(Some) |
51 | } else { |
52 | Ok(None) |
53 | } |
54 | } |
55 | |
56 | fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error> |
57 | where |
58 | V: serde::de::DeserializeSeed<'de>, |
59 | { |
60 | if let Some(start) = self.start.take() { |
61 | seed.deserialize(start.into_deserializer()) |
62 | } else if let Some(end) = self.end.take() { |
63 | seed.deserialize(end.into_deserializer()) |
64 | } else if let Some(value) = self.value.take() { |
65 | seed.deserialize(value.into_deserializer()) |
66 | } else { |
67 | panic!("next_value_seed called before next_key_seed" ) |
68 | } |
69 | } |
70 | } |
71 | |