1 | use crate::lib::*; |
2 | |
3 | use crate::de::{ |
4 | Deserialize, Deserializer, EnumAccess, Error, MapAccess, SeqAccess, VariantAccess, Visitor, |
5 | }; |
6 | |
7 | /// An efficient way of discarding data from a deserializer. |
8 | /// |
9 | /// Think of this like `serde_json::Value` in that it can be deserialized from |
10 | /// any type, except that it does not store any information about the data that |
11 | /// gets deserialized. |
12 | /// |
13 | /// ```edition2021 |
14 | /// use serde::de::{ |
15 | /// self, Deserialize, DeserializeSeed, Deserializer, IgnoredAny, SeqAccess, Visitor, |
16 | /// }; |
17 | /// use std::fmt; |
18 | /// use std::marker::PhantomData; |
19 | /// |
20 | /// /// A seed that can be used to deserialize only the `n`th element of a sequence |
21 | /// /// while efficiently discarding elements of any type before or after index `n`. |
22 | /// /// |
23 | /// /// For example to deserialize only the element at index 3: |
24 | /// /// |
25 | /// /// ``` |
26 | /// /// NthElement::new(3).deserialize(deserializer) |
27 | /// /// ``` |
28 | /// pub struct NthElement<T> { |
29 | /// n: usize, |
30 | /// marker: PhantomData<T>, |
31 | /// } |
32 | /// |
33 | /// impl<T> NthElement<T> { |
34 | /// pub fn new(n: usize) -> Self { |
35 | /// NthElement { |
36 | /// n: n, |
37 | /// marker: PhantomData, |
38 | /// } |
39 | /// } |
40 | /// } |
41 | /// |
42 | /// impl<'de, T> Visitor<'de> for NthElement<T> |
43 | /// where |
44 | /// T: Deserialize<'de>, |
45 | /// { |
46 | /// type Value = T; |
47 | /// |
48 | /// fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
49 | /// write!( |
50 | /// formatter, |
51 | /// "a sequence in which we care about element {}" , |
52 | /// self.n |
53 | /// ) |
54 | /// } |
55 | /// |
56 | /// fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
57 | /// where |
58 | /// A: SeqAccess<'de>, |
59 | /// { |
60 | /// // Skip over the first `n` elements. |
61 | /// for i in 0..self.n { |
62 | /// // It is an error if the sequence ends before we get to element `n`. |
63 | /// if seq.next_element::<IgnoredAny>()?.is_none() { |
64 | /// return Err(de::Error::invalid_length(i, &self)); |
65 | /// } |
66 | /// } |
67 | /// |
68 | /// // Deserialize the one we care about. |
69 | /// let nth = match seq.next_element()? { |
70 | /// Some(nth) => nth, |
71 | /// None => { |
72 | /// return Err(de::Error::invalid_length(self.n, &self)); |
73 | /// } |
74 | /// }; |
75 | /// |
76 | /// // Skip over any remaining elements in the sequence after `n`. |
77 | /// while let Some(IgnoredAny) = seq.next_element()? { |
78 | /// // ignore |
79 | /// } |
80 | /// |
81 | /// Ok(nth) |
82 | /// } |
83 | /// } |
84 | /// |
85 | /// impl<'de, T> DeserializeSeed<'de> for NthElement<T> |
86 | /// where |
87 | /// T: Deserialize<'de>, |
88 | /// { |
89 | /// type Value = T; |
90 | /// |
91 | /// fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
92 | /// where |
93 | /// D: Deserializer<'de>, |
94 | /// { |
95 | /// deserializer.deserialize_seq(self) |
96 | /// } |
97 | /// } |
98 | /// |
99 | /// # fn example<'de, D>(deserializer: D) -> Result<(), D::Error> |
100 | /// # where |
101 | /// # D: Deserializer<'de>, |
102 | /// # { |
103 | /// // Deserialize only the sequence element at index 3 from this deserializer. |
104 | /// // The element at index 3 is required to be a string. Elements before and |
105 | /// // after index 3 are allowed to be of any type. |
106 | /// let s: String = NthElement::new(3).deserialize(deserializer)?; |
107 | /// # Ok(()) |
108 | /// # } |
109 | /// ``` |
110 | #[derive (Copy, Clone, Debug, Default, PartialEq)] |
111 | pub struct IgnoredAny; |
112 | |
113 | impl<'de> Visitor<'de> for IgnoredAny { |
114 | type Value = IgnoredAny; |
115 | |
116 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
117 | formatter.write_str("anything at all" ) |
118 | } |
119 | |
120 | #[inline ] |
121 | fn visit_bool<E>(self, x: bool) -> Result<Self::Value, E> { |
122 | let _ = x; |
123 | Ok(IgnoredAny) |
124 | } |
125 | |
126 | #[inline ] |
127 | fn visit_i64<E>(self, x: i64) -> Result<Self::Value, E> { |
128 | let _ = x; |
129 | Ok(IgnoredAny) |
130 | } |
131 | |
132 | #[inline ] |
133 | fn visit_i128<E>(self, x: i128) -> Result<Self::Value, E> { |
134 | let _ = x; |
135 | Ok(IgnoredAny) |
136 | } |
137 | |
138 | #[inline ] |
139 | fn visit_u64<E>(self, x: u64) -> Result<Self::Value, E> { |
140 | let _ = x; |
141 | Ok(IgnoredAny) |
142 | } |
143 | |
144 | #[inline ] |
145 | fn visit_u128<E>(self, x: u128) -> Result<Self::Value, E> { |
146 | let _ = x; |
147 | Ok(IgnoredAny) |
148 | } |
149 | |
150 | #[inline ] |
151 | fn visit_f64<E>(self, x: f64) -> Result<Self::Value, E> { |
152 | let _ = x; |
153 | Ok(IgnoredAny) |
154 | } |
155 | |
156 | #[inline ] |
157 | fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> |
158 | where |
159 | E: Error, |
160 | { |
161 | let _ = s; |
162 | Ok(IgnoredAny) |
163 | } |
164 | |
165 | #[inline ] |
166 | fn visit_none<E>(self) -> Result<Self::Value, E> { |
167 | Ok(IgnoredAny) |
168 | } |
169 | |
170 | #[inline ] |
171 | fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
172 | where |
173 | D: Deserializer<'de>, |
174 | { |
175 | IgnoredAny::deserialize(deserializer) |
176 | } |
177 | |
178 | #[inline ] |
179 | fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
180 | where |
181 | D: Deserializer<'de>, |
182 | { |
183 | IgnoredAny::deserialize(deserializer) |
184 | } |
185 | |
186 | #[inline ] |
187 | fn visit_unit<E>(self) -> Result<Self::Value, E> { |
188 | Ok(IgnoredAny) |
189 | } |
190 | |
191 | #[inline ] |
192 | fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
193 | where |
194 | A: SeqAccess<'de>, |
195 | { |
196 | while let Some(IgnoredAny) = tri!(seq.next_element()) { |
197 | // Gobble |
198 | } |
199 | Ok(IgnoredAny) |
200 | } |
201 | |
202 | #[inline ] |
203 | fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> |
204 | where |
205 | A: MapAccess<'de>, |
206 | { |
207 | while let Some((IgnoredAny, IgnoredAny)) = tri!(map.next_entry()) { |
208 | // Gobble |
209 | } |
210 | Ok(IgnoredAny) |
211 | } |
212 | |
213 | #[inline ] |
214 | fn visit_bytes<E>(self, bytes: &[u8]) -> Result<Self::Value, E> |
215 | where |
216 | E: Error, |
217 | { |
218 | let _ = bytes; |
219 | Ok(IgnoredAny) |
220 | } |
221 | |
222 | fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error> |
223 | where |
224 | A: EnumAccess<'de>, |
225 | { |
226 | tri!(data.variant::<IgnoredAny>()).1.newtype_variant() |
227 | } |
228 | } |
229 | |
230 | impl<'de> Deserialize<'de> for IgnoredAny { |
231 | #[inline ] |
232 | fn deserialize<D>(deserializer: D) -> Result<IgnoredAny, D::Error> |
233 | where |
234 | D: Deserializer<'de>, |
235 | { |
236 | deserializer.deserialize_ignored_any(visitor:IgnoredAny) |
237 | } |
238 | } |
239 | |