1 use std::error::Error as StdError;
2 use std::fmt;
3 use std::iter;
4 use std::num;
5 use std::str;
6
7 use serde::de::value::BorrowedBytesDeserializer;
8 use serde::de::{
9 Deserialize, DeserializeSeed, Deserializer, EnumAccess,
10 Error as SerdeError, IntoDeserializer, MapAccess, SeqAccess, Unexpected,
11 VariantAccess, Visitor,
12 };
13 use serde::serde_if_integer128;
14
15 use crate::byte_record::{ByteRecord, ByteRecordIter};
16 use crate::error::{Error, ErrorKind};
17 use crate::string_record::{StringRecord, StringRecordIter};
18
19 use self::DeserializeErrorKind as DEK;
20
deserialize_string_record<'de, D: Deserialize<'de>>( record: &'de StringRecord, headers: Option<&'de StringRecord>, ) -> Result<D, Error>21 pub fn deserialize_string_record<'de, D: Deserialize<'de>>(
22 record: &'de StringRecord,
23 headers: Option<&'de StringRecord>,
24 ) -> Result<D, Error> {
25 let mut deser = DeRecordWrap(DeStringRecord {
26 it: record.iter().peekable(),
27 headers: headers.map(|r| r.iter()),
28 field: 0,
29 });
30 D::deserialize(&mut deser).map_err(|err| {
31 Error::new(ErrorKind::Deserialize {
32 pos: record.position().map(Clone::clone),
33 err: err,
34 })
35 })
36 }
37
deserialize_byte_record<'de, D: Deserialize<'de>>( record: &'de ByteRecord, headers: Option<&'de ByteRecord>, ) -> Result<D, Error>38 pub fn deserialize_byte_record<'de, D: Deserialize<'de>>(
39 record: &'de ByteRecord,
40 headers: Option<&'de ByteRecord>,
41 ) -> Result<D, Error> {
42 let mut deser = DeRecordWrap(DeByteRecord {
43 it: record.iter().peekable(),
44 headers: headers.map(|r| r.iter()),
45 field: 0,
46 });
47 D::deserialize(&mut deser).map_err(|err| {
48 Error::new(ErrorKind::Deserialize {
49 pos: record.position().map(Clone::clone),
50 err: err,
51 })
52 })
53 }
54
55 /// An over-engineered internal trait that permits writing a single Serde
56 /// deserializer that works on both ByteRecord and StringRecord.
57 ///
58 /// We *could* implement a single deserializer on `ByteRecord` and simply
59 /// convert `StringRecord`s to `ByteRecord`s, but then the implementation
60 /// would be required to redo UTF-8 validation checks in certain places.
61 ///
62 /// How does this work? We create a new `DeRecordWrap` type that wraps
63 /// either a `StringRecord` or a `ByteRecord`. We then implement
64 /// `DeRecord` for `DeRecordWrap<ByteRecord>` and `DeRecordWrap<StringRecord>`.
65 /// Finally, we impl `serde::Deserialize` for `DeRecordWrap<T>` where
66 /// `T: DeRecord`. That is, the `DeRecord` type corresponds to the differences
67 /// between deserializing into a `ByteRecord` and deserializing into a
68 /// `StringRecord`.
69 ///
70 /// The lifetime `'r` refers to the lifetime of the underlying record.
71 trait DeRecord<'r> {
72 /// Returns true if and only if this deserialize has access to headers.
has_headers(&self) -> bool73 fn has_headers(&self) -> bool;
74
75 /// Extracts the next string header value from the underlying record.
next_header(&mut self) -> Result<Option<&'r str>, DeserializeError>76 fn next_header(&mut self) -> Result<Option<&'r str>, DeserializeError>;
77
78 /// Extracts the next raw byte header value from the underlying record.
next_header_bytes( &mut self, ) -> Result<Option<&'r [u8]>, DeserializeError>79 fn next_header_bytes(
80 &mut self,
81 ) -> Result<Option<&'r [u8]>, DeserializeError>;
82
83 /// Extracts the next string field from the underlying record.
next_field(&mut self) -> Result<&'r str, DeserializeError>84 fn next_field(&mut self) -> Result<&'r str, DeserializeError>;
85
86 /// Extracts the next raw byte field from the underlying record.
next_field_bytes(&mut self) -> Result<&'r [u8], DeserializeError>87 fn next_field_bytes(&mut self) -> Result<&'r [u8], DeserializeError>;
88
89 /// Peeks at the next field from the underlying record.
peek_field(&mut self) -> Option<&'r [u8]>90 fn peek_field(&mut self) -> Option<&'r [u8]>;
91
92 /// Returns an error corresponding to the most recently extracted field.
error(&self, kind: DeserializeErrorKind) -> DeserializeError93 fn error(&self, kind: DeserializeErrorKind) -> DeserializeError;
94
95 /// Infer the type of the next field and deserialize it.
infer_deserialize<'de, V: Visitor<'de>>( &mut self, visitor: V, ) -> Result<V::Value, DeserializeError>96 fn infer_deserialize<'de, V: Visitor<'de>>(
97 &mut self,
98 visitor: V,
99 ) -> Result<V::Value, DeserializeError>;
100 }
101
102 struct DeRecordWrap<T>(T);
103
104 impl<'r, T: DeRecord<'r>> DeRecord<'r> for DeRecordWrap<T> {
105 #[inline]
has_headers(&self) -> bool106 fn has_headers(&self) -> bool {
107 self.0.has_headers()
108 }
109
110 #[inline]
next_header(&mut self) -> Result<Option<&'r str>, DeserializeError>111 fn next_header(&mut self) -> Result<Option<&'r str>, DeserializeError> {
112 self.0.next_header()
113 }
114
115 #[inline]
next_header_bytes( &mut self, ) -> Result<Option<&'r [u8]>, DeserializeError>116 fn next_header_bytes(
117 &mut self,
118 ) -> Result<Option<&'r [u8]>, DeserializeError> {
119 self.0.next_header_bytes()
120 }
121
122 #[inline]
next_field(&mut self) -> Result<&'r str, DeserializeError>123 fn next_field(&mut self) -> Result<&'r str, DeserializeError> {
124 self.0.next_field()
125 }
126
127 #[inline]
next_field_bytes(&mut self) -> Result<&'r [u8], DeserializeError>128 fn next_field_bytes(&mut self) -> Result<&'r [u8], DeserializeError> {
129 self.0.next_field_bytes()
130 }
131
132 #[inline]
peek_field(&mut self) -> Option<&'r [u8]>133 fn peek_field(&mut self) -> Option<&'r [u8]> {
134 self.0.peek_field()
135 }
136
137 #[inline]
error(&self, kind: DeserializeErrorKind) -> DeserializeError138 fn error(&self, kind: DeserializeErrorKind) -> DeserializeError {
139 self.0.error(kind)
140 }
141
142 #[inline]
infer_deserialize<'de, V: Visitor<'de>>( &mut self, visitor: V, ) -> Result<V::Value, DeserializeError>143 fn infer_deserialize<'de, V: Visitor<'de>>(
144 &mut self,
145 visitor: V,
146 ) -> Result<V::Value, DeserializeError> {
147 self.0.infer_deserialize(visitor)
148 }
149 }
150
151 struct DeStringRecord<'r> {
152 it: iter::Peekable<StringRecordIter<'r>>,
153 headers: Option<StringRecordIter<'r>>,
154 field: u64,
155 }
156
157 impl<'r> DeRecord<'r> for DeStringRecord<'r> {
158 #[inline]
has_headers(&self) -> bool159 fn has_headers(&self) -> bool {
160 self.headers.is_some()
161 }
162
163 #[inline]
next_header(&mut self) -> Result<Option<&'r str>, DeserializeError>164 fn next_header(&mut self) -> Result<Option<&'r str>, DeserializeError> {
165 Ok(self.headers.as_mut().and_then(|it| it.next()))
166 }
167
168 #[inline]
next_header_bytes( &mut self, ) -> Result<Option<&'r [u8]>, DeserializeError>169 fn next_header_bytes(
170 &mut self,
171 ) -> Result<Option<&'r [u8]>, DeserializeError> {
172 Ok(self.next_header()?.map(|s| s.as_bytes()))
173 }
174
175 #[inline]
next_field(&mut self) -> Result<&'r str, DeserializeError>176 fn next_field(&mut self) -> Result<&'r str, DeserializeError> {
177 match self.it.next() {
178 Some(field) => {
179 self.field += 1;
180 Ok(field)
181 }
182 None => Err(DeserializeError {
183 field: None,
184 kind: DEK::UnexpectedEndOfRow,
185 }),
186 }
187 }
188
189 #[inline]
next_field_bytes(&mut self) -> Result<&'r [u8], DeserializeError>190 fn next_field_bytes(&mut self) -> Result<&'r [u8], DeserializeError> {
191 self.next_field().map(|s| s.as_bytes())
192 }
193
194 #[inline]
peek_field(&mut self) -> Option<&'r [u8]>195 fn peek_field(&mut self) -> Option<&'r [u8]> {
196 self.it.peek().map(|s| s.as_bytes())
197 }
198
error(&self, kind: DeserializeErrorKind) -> DeserializeError199 fn error(&self, kind: DeserializeErrorKind) -> DeserializeError {
200 DeserializeError {
201 field: Some(self.field.saturating_sub(1)),
202 kind: kind,
203 }
204 }
205
infer_deserialize<'de, V: Visitor<'de>>( &mut self, visitor: V, ) -> Result<V::Value, DeserializeError>206 fn infer_deserialize<'de, V: Visitor<'de>>(
207 &mut self,
208 visitor: V,
209 ) -> Result<V::Value, DeserializeError> {
210 let x = self.next_field()?;
211 if x == "true" {
212 return visitor.visit_bool(true);
213 } else if x == "false" {
214 return visitor.visit_bool(false);
215 } else if let Some(n) = try_positive_integer64(x) {
216 return visitor.visit_u64(n);
217 } else if let Some(n) = try_negative_integer64(x) {
218 return visitor.visit_i64(n);
219 }
220 serde_if_integer128! {
221 if let Some(n) = try_positive_integer128(x) {
222 return visitor.visit_u128(n);
223 } else if let Some(n) = try_negative_integer128(x) {
224 return visitor.visit_i128(n);
225 }
226 }
227 if let Some(n) = try_float(x) {
228 visitor.visit_f64(n)
229 } else {
230 visitor.visit_str(x)
231 }
232 }
233 }
234
235 struct DeByteRecord<'r> {
236 it: iter::Peekable<ByteRecordIter<'r>>,
237 headers: Option<ByteRecordIter<'r>>,
238 field: u64,
239 }
240
241 impl<'r> DeRecord<'r> for DeByteRecord<'r> {
242 #[inline]
has_headers(&self) -> bool243 fn has_headers(&self) -> bool {
244 self.headers.is_some()
245 }
246
247 #[inline]
next_header(&mut self) -> Result<Option<&'r str>, DeserializeError>248 fn next_header(&mut self) -> Result<Option<&'r str>, DeserializeError> {
249 match self.next_header_bytes() {
250 Ok(Some(field)) => Ok(Some(
251 str::from_utf8(field)
252 .map_err(|err| self.error(DEK::InvalidUtf8(err)))?,
253 )),
254 Ok(None) => Ok(None),
255 Err(err) => Err(err),
256 }
257 }
258
259 #[inline]
next_header_bytes( &mut self, ) -> Result<Option<&'r [u8]>, DeserializeError>260 fn next_header_bytes(
261 &mut self,
262 ) -> Result<Option<&'r [u8]>, DeserializeError> {
263 Ok(self.headers.as_mut().and_then(|it| it.next()))
264 }
265
266 #[inline]
next_field(&mut self) -> Result<&'r str, DeserializeError>267 fn next_field(&mut self) -> Result<&'r str, DeserializeError> {
268 self.next_field_bytes().and_then(|field| {
269 str::from_utf8(field)
270 .map_err(|err| self.error(DEK::InvalidUtf8(err)))
271 })
272 }
273
274 #[inline]
next_field_bytes(&mut self) -> Result<&'r [u8], DeserializeError>275 fn next_field_bytes(&mut self) -> Result<&'r [u8], DeserializeError> {
276 match self.it.next() {
277 Some(field) => {
278 self.field += 1;
279 Ok(field)
280 }
281 None => Err(DeserializeError {
282 field: None,
283 kind: DEK::UnexpectedEndOfRow,
284 }),
285 }
286 }
287
288 #[inline]
peek_field(&mut self) -> Option<&'r [u8]>289 fn peek_field(&mut self) -> Option<&'r [u8]> {
290 self.it.peek().map(|s| *s)
291 }
292
error(&self, kind: DeserializeErrorKind) -> DeserializeError293 fn error(&self, kind: DeserializeErrorKind) -> DeserializeError {
294 DeserializeError {
295 field: Some(self.field.saturating_sub(1)),
296 kind: kind,
297 }
298 }
299
infer_deserialize<'de, V: Visitor<'de>>( &mut self, visitor: V, ) -> Result<V::Value, DeserializeError>300 fn infer_deserialize<'de, V: Visitor<'de>>(
301 &mut self,
302 visitor: V,
303 ) -> Result<V::Value, DeserializeError> {
304 let x = self.next_field_bytes()?;
305 if x == b"true" {
306 return visitor.visit_bool(true);
307 } else if x == b"false" {
308 return visitor.visit_bool(false);
309 } else if let Some(n) = try_positive_integer64_bytes(x) {
310 return visitor.visit_u64(n);
311 } else if let Some(n) = try_negative_integer64_bytes(x) {
312 return visitor.visit_i64(n);
313 }
314 serde_if_integer128! {
315 if let Some(n) = try_positive_integer128_bytes(x) {
316 return visitor.visit_u128(n);
317 } else if let Some(n) = try_negative_integer128_bytes(x) {
318 return visitor.visit_i128(n);
319 }
320 }
321 if let Some(n) = try_float_bytes(x) {
322 visitor.visit_f64(n)
323 } else if let Ok(s) = str::from_utf8(x) {
324 visitor.visit_str(s)
325 } else {
326 visitor.visit_bytes(x)
327 }
328 }
329 }
330
331 macro_rules! deserialize_int {
332 ($method:ident, $visit:ident, $inttype:ty) => {
333 fn $method<V: Visitor<'de>>(
334 self,
335 visitor: V,
336 ) -> Result<V::Value, Self::Error> {
337 let field = self.next_field()?;
338 let num =
339 if field.starts_with("0x") {
340 <$inttype>::from_str_radix(&field[2..], 16)
341 } else {
342 field.parse()
343 };
344 visitor.$visit(num.map_err(|err| self.error(DEK::ParseInt(err)))?)
345 }
346 }
347 }
348
349 impl<'a, 'de: 'a, T: DeRecord<'de>> Deserializer<'de>
350 for &'a mut DeRecordWrap<T>
351 {
352 type Error = DeserializeError;
353
deserialize_any<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>354 fn deserialize_any<V: Visitor<'de>>(
355 self,
356 visitor: V,
357 ) -> Result<V::Value, Self::Error> {
358 self.infer_deserialize(visitor)
359 }
360
deserialize_bool<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>361 fn deserialize_bool<V: Visitor<'de>>(
362 self,
363 visitor: V,
364 ) -> Result<V::Value, Self::Error> {
365 visitor.visit_bool(
366 self.next_field()?
367 .parse()
368 .map_err(|err| self.error(DEK::ParseBool(err)))?,
369 )
370 }
371
372 deserialize_int!(deserialize_u8, visit_u8, u8);
373 deserialize_int!(deserialize_u16, visit_u16, u16);
374 deserialize_int!(deserialize_u32, visit_u32, u32);
375 deserialize_int!(deserialize_u64, visit_u64, u64);
376 serde_if_integer128! {
377 deserialize_int!(deserialize_u128, visit_u128, u128);
378 }
379 deserialize_int!(deserialize_i8, visit_i8, i8);
380 deserialize_int!(deserialize_i16, visit_i16, i16);
381 deserialize_int!(deserialize_i32, visit_i32, i32);
382 deserialize_int!(deserialize_i64, visit_i64, i64);
383 serde_if_integer128! {
384 deserialize_int!(deserialize_i128, visit_i128, i128);
385 }
386
deserialize_f32<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>387 fn deserialize_f32<V: Visitor<'de>>(
388 self,
389 visitor: V,
390 ) -> Result<V::Value, Self::Error> {
391 visitor.visit_f32(
392 self.next_field()?
393 .parse()
394 .map_err(|err| self.error(DEK::ParseFloat(err)))?,
395 )
396 }
397
deserialize_f64<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>398 fn deserialize_f64<V: Visitor<'de>>(
399 self,
400 visitor: V,
401 ) -> Result<V::Value, Self::Error> {
402 visitor.visit_f64(
403 self.next_field()?
404 .parse()
405 .map_err(|err| self.error(DEK::ParseFloat(err)))?,
406 )
407 }
408
deserialize_char<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>409 fn deserialize_char<V: Visitor<'de>>(
410 self,
411 visitor: V,
412 ) -> Result<V::Value, Self::Error> {
413 let field = self.next_field()?;
414 let len = field.chars().count();
415 if len != 1 {
416 return Err(self.error(DEK::Message(format!(
417 "expected single character but got {} characters in '{}'",
418 len, field
419 ))));
420 }
421 visitor.visit_char(field.chars().next().unwrap())
422 }
423
deserialize_str<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>424 fn deserialize_str<V: Visitor<'de>>(
425 self,
426 visitor: V,
427 ) -> Result<V::Value, Self::Error> {
428 self.next_field().and_then(|f| visitor.visit_borrowed_str(f))
429 }
430
deserialize_string<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>431 fn deserialize_string<V: Visitor<'de>>(
432 self,
433 visitor: V,
434 ) -> Result<V::Value, Self::Error> {
435 self.next_field().and_then(|f| visitor.visit_str(f.into()))
436 }
437
deserialize_bytes<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>438 fn deserialize_bytes<V: Visitor<'de>>(
439 self,
440 visitor: V,
441 ) -> Result<V::Value, Self::Error> {
442 self.next_field_bytes().and_then(|f| visitor.visit_borrowed_bytes(f))
443 }
444
deserialize_byte_buf<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>445 fn deserialize_byte_buf<V: Visitor<'de>>(
446 self,
447 visitor: V,
448 ) -> Result<V::Value, Self::Error> {
449 self.next_field_bytes()
450 .and_then(|f| visitor.visit_byte_buf(f.to_vec()))
451 }
452
deserialize_option<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>453 fn deserialize_option<V: Visitor<'de>>(
454 self,
455 visitor: V,
456 ) -> Result<V::Value, Self::Error> {
457 match self.peek_field() {
458 None => visitor.visit_none(),
459 Some(f) if f.is_empty() => {
460 self.next_field().expect("empty field");
461 visitor.visit_none()
462 }
463 Some(_) => visitor.visit_some(self),
464 }
465 }
466
deserialize_unit<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>467 fn deserialize_unit<V: Visitor<'de>>(
468 self,
469 visitor: V,
470 ) -> Result<V::Value, Self::Error> {
471 visitor.visit_unit()
472 }
473
deserialize_unit_struct<V: Visitor<'de>>( self, _name: &'static str, visitor: V, ) -> Result<V::Value, Self::Error>474 fn deserialize_unit_struct<V: Visitor<'de>>(
475 self,
476 _name: &'static str,
477 visitor: V,
478 ) -> Result<V::Value, Self::Error> {
479 visitor.visit_unit()
480 }
481
deserialize_newtype_struct<V: Visitor<'de>>( self, _name: &'static str, visitor: V, ) -> Result<V::Value, Self::Error>482 fn deserialize_newtype_struct<V: Visitor<'de>>(
483 self,
484 _name: &'static str,
485 visitor: V,
486 ) -> Result<V::Value, Self::Error> {
487 visitor.visit_newtype_struct(self)
488 }
489
deserialize_seq<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>490 fn deserialize_seq<V: Visitor<'de>>(
491 self,
492 visitor: V,
493 ) -> Result<V::Value, Self::Error> {
494 visitor.visit_seq(self)
495 }
496
deserialize_tuple<V: Visitor<'de>>( self, _len: usize, visitor: V, ) -> Result<V::Value, Self::Error>497 fn deserialize_tuple<V: Visitor<'de>>(
498 self,
499 _len: usize,
500 visitor: V,
501 ) -> Result<V::Value, Self::Error> {
502 visitor.visit_seq(self)
503 }
504
deserialize_tuple_struct<V: Visitor<'de>>( self, _name: &'static str, _len: usize, visitor: V, ) -> Result<V::Value, Self::Error>505 fn deserialize_tuple_struct<V: Visitor<'de>>(
506 self,
507 _name: &'static str,
508 _len: usize,
509 visitor: V,
510 ) -> Result<V::Value, Self::Error> {
511 visitor.visit_seq(self)
512 }
513
deserialize_map<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>514 fn deserialize_map<V: Visitor<'de>>(
515 self,
516 visitor: V,
517 ) -> Result<V::Value, Self::Error> {
518 if !self.has_headers() {
519 visitor.visit_seq(self)
520 } else {
521 visitor.visit_map(self)
522 }
523 }
524
deserialize_struct<V: Visitor<'de>>( self, _name: &'static str, _fields: &'static [&'static str], visitor: V, ) -> Result<V::Value, Self::Error>525 fn deserialize_struct<V: Visitor<'de>>(
526 self,
527 _name: &'static str,
528 _fields: &'static [&'static str],
529 visitor: V,
530 ) -> Result<V::Value, Self::Error> {
531 if !self.has_headers() {
532 visitor.visit_seq(self)
533 } else {
534 visitor.visit_map(self)
535 }
536 }
537
deserialize_identifier<V: Visitor<'de>>( self, _visitor: V, ) -> Result<V::Value, Self::Error>538 fn deserialize_identifier<V: Visitor<'de>>(
539 self,
540 _visitor: V,
541 ) -> Result<V::Value, Self::Error> {
542 Err(self.error(DEK::Unsupported("deserialize_identifier".into())))
543 }
544
deserialize_enum<V: Visitor<'de>>( self, _name: &'static str, _variants: &'static [&'static str], visitor: V, ) -> Result<V::Value, Self::Error>545 fn deserialize_enum<V: Visitor<'de>>(
546 self,
547 _name: &'static str,
548 _variants: &'static [&'static str],
549 visitor: V,
550 ) -> Result<V::Value, Self::Error> {
551 visitor.visit_enum(self)
552 }
553
deserialize_ignored_any<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>554 fn deserialize_ignored_any<V: Visitor<'de>>(
555 self,
556 visitor: V,
557 ) -> Result<V::Value, Self::Error> {
558 // Read and drop the next field.
559 // This code is reached, e.g., when trying to deserialize a header
560 // that doesn't exist in the destination struct.
561 let _ = self.next_field()?;
562 visitor.visit_unit()
563 }
564 }
565
566 impl<'a, 'de: 'a, T: DeRecord<'de>> EnumAccess<'de>
567 for &'a mut DeRecordWrap<T>
568 {
569 type Error = DeserializeError;
570 type Variant = Self;
571
variant_seed<V: DeserializeSeed<'de>>( self, seed: V, ) -> Result<(V::Value, Self::Variant), Self::Error>572 fn variant_seed<V: DeserializeSeed<'de>>(
573 self,
574 seed: V,
575 ) -> Result<(V::Value, Self::Variant), Self::Error> {
576 let variant_name = self.next_field()?;
577 seed.deserialize(variant_name.into_deserializer()).map(|v| (v, self))
578 }
579 }
580
581 impl<'a, 'de: 'a, T: DeRecord<'de>> VariantAccess<'de>
582 for &'a mut DeRecordWrap<T>
583 {
584 type Error = DeserializeError;
585
unit_variant(self) -> Result<(), Self::Error>586 fn unit_variant(self) -> Result<(), Self::Error> {
587 Ok(())
588 }
589
newtype_variant_seed<U: DeserializeSeed<'de>>( self, _seed: U, ) -> Result<U::Value, Self::Error>590 fn newtype_variant_seed<U: DeserializeSeed<'de>>(
591 self,
592 _seed: U,
593 ) -> Result<U::Value, Self::Error> {
594 let unexp = Unexpected::UnitVariant;
595 Err(DeserializeError::invalid_type(unexp, &"newtype variant"))
596 }
597
tuple_variant<V: Visitor<'de>>( self, _len: usize, _visitor: V, ) -> Result<V::Value, Self::Error>598 fn tuple_variant<V: Visitor<'de>>(
599 self,
600 _len: usize,
601 _visitor: V,
602 ) -> Result<V::Value, Self::Error> {
603 let unexp = Unexpected::UnitVariant;
604 Err(DeserializeError::invalid_type(unexp, &"tuple variant"))
605 }
606
struct_variant<V: Visitor<'de>>( self, _fields: &'static [&'static str], _visitor: V, ) -> Result<V::Value, Self::Error>607 fn struct_variant<V: Visitor<'de>>(
608 self,
609 _fields: &'static [&'static str],
610 _visitor: V,
611 ) -> Result<V::Value, Self::Error> {
612 let unexp = Unexpected::UnitVariant;
613 Err(DeserializeError::invalid_type(unexp, &"struct variant"))
614 }
615 }
616
617 impl<'a, 'de: 'a, T: DeRecord<'de>> SeqAccess<'de>
618 for &'a mut DeRecordWrap<T>
619 {
620 type Error = DeserializeError;
621
next_element_seed<U: DeserializeSeed<'de>>( &mut self, seed: U, ) -> Result<Option<U::Value>, Self::Error>622 fn next_element_seed<U: DeserializeSeed<'de>>(
623 &mut self,
624 seed: U,
625 ) -> Result<Option<U::Value>, Self::Error> {
626 if self.peek_field().is_none() {
627 Ok(None)
628 } else {
629 seed.deserialize(&mut **self).map(Some)
630 }
631 }
632 }
633
634 impl<'a, 'de: 'a, T: DeRecord<'de>> MapAccess<'de>
635 for &'a mut DeRecordWrap<T>
636 {
637 type Error = DeserializeError;
638
next_key_seed<K: DeserializeSeed<'de>>( &mut self, seed: K, ) -> Result<Option<K::Value>, Self::Error>639 fn next_key_seed<K: DeserializeSeed<'de>>(
640 &mut self,
641 seed: K,
642 ) -> Result<Option<K::Value>, Self::Error> {
643 assert!(self.has_headers());
644 let field = match self.next_header_bytes()? {
645 None => return Ok(None),
646 Some(field) => field,
647 };
648 seed.deserialize(BorrowedBytesDeserializer::new(field)).map(Some)
649 }
650
next_value_seed<K: DeserializeSeed<'de>>( &mut self, seed: K, ) -> Result<K::Value, Self::Error>651 fn next_value_seed<K: DeserializeSeed<'de>>(
652 &mut self,
653 seed: K,
654 ) -> Result<K::Value, Self::Error> {
655 seed.deserialize(&mut **self)
656 }
657 }
658
659 /// An Serde deserialization error.
660 #[derive(Clone, Debug, Eq, PartialEq)]
661 pub struct DeserializeError {
662 field: Option<u64>,
663 kind: DeserializeErrorKind,
664 }
665
666 /// The type of a Serde deserialization error.
667 #[derive(Clone, Debug, Eq, PartialEq)]
668 pub enum DeserializeErrorKind {
669 /// A generic Serde deserialization error.
670 Message(String),
671 /// A generic Serde unsupported error.
672 Unsupported(String),
673 /// This error occurs when a Rust type expects to decode another field
674 /// from a row, but no more fields exist.
675 UnexpectedEndOfRow,
676 /// This error occurs when UTF-8 validation on a field fails. UTF-8
677 /// validation is only performed when the Rust type requires it (e.g.,
678 /// a `String` or `&str` type).
679 InvalidUtf8(str::Utf8Error),
680 /// This error occurs when a boolean value fails to parse.
681 ParseBool(str::ParseBoolError),
682 /// This error occurs when an integer value fails to parse.
683 ParseInt(num::ParseIntError),
684 /// This error occurs when a float value fails to parse.
685 ParseFloat(num::ParseFloatError),
686 }
687
688 impl SerdeError for DeserializeError {
custom<T: fmt::Display>(msg: T) -> DeserializeError689 fn custom<T: fmt::Display>(msg: T) -> DeserializeError {
690 DeserializeError { field: None, kind: DEK::Message(msg.to_string()) }
691 }
692 }
693
694 impl StdError for DeserializeError {
description(&self) -> &str695 fn description(&self) -> &str {
696 self.kind.description()
697 }
698 }
699
700 impl fmt::Display for DeserializeError {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result701 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
702 if let Some(field) = self.field {
703 write!(f, "field {}: {}", field, self.kind)
704 } else {
705 write!(f, "{}", self.kind)
706 }
707 }
708 }
709
710 impl fmt::Display for DeserializeErrorKind {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result711 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
712 use self::DeserializeErrorKind::*;
713
714 match *self {
715 Message(ref msg) => write!(f, "{}", msg),
716 Unsupported(ref which) => {
717 write!(f, "unsupported deserializer method: {}", which)
718 }
719 UnexpectedEndOfRow => write!(f, "{}", self.description()),
720 InvalidUtf8(ref err) => err.fmt(f),
721 ParseBool(ref err) => err.fmt(f),
722 ParseInt(ref err) => err.fmt(f),
723 ParseFloat(ref err) => err.fmt(f),
724 }
725 }
726 }
727
728 impl DeserializeError {
729 /// Return the field index (starting at 0) of this error, if available.
field(&self) -> Option<u64>730 pub fn field(&self) -> Option<u64> {
731 self.field
732 }
733
734 /// Return the underlying error kind.
kind(&self) -> &DeserializeErrorKind735 pub fn kind(&self) -> &DeserializeErrorKind {
736 &self.kind
737 }
738 }
739
740 impl DeserializeErrorKind {
741 #[allow(deprecated)]
description(&self) -> &str742 fn description(&self) -> &str {
743 use self::DeserializeErrorKind::*;
744
745 match *self {
746 Message(_) => "deserialization error",
747 Unsupported(_) => "unsupported deserializer method",
748 UnexpectedEndOfRow => "expected field, but got end of row",
749 InvalidUtf8(ref err) => err.description(),
750 ParseBool(ref err) => err.description(),
751 ParseInt(ref err) => err.description(),
752 ParseFloat(ref err) => err.description(),
753 }
754 }
755 }
756
757 serde_if_integer128! {
758 fn try_positive_integer128(s: &str) -> Option<u128> {
759 s.parse().ok()
760 }
761
762 fn try_negative_integer128(s: &str) -> Option<i128> {
763 s.parse().ok()
764 }
765 }
766
try_positive_integer64(s: &str) -> Option<u64>767 fn try_positive_integer64(s: &str) -> Option<u64> {
768 s.parse().ok()
769 }
770
try_negative_integer64(s: &str) -> Option<i64>771 fn try_negative_integer64(s: &str) -> Option<i64> {
772 s.parse().ok()
773 }
774
try_float(s: &str) -> Option<f64>775 fn try_float(s: &str) -> Option<f64> {
776 s.parse().ok()
777 }
778
try_positive_integer64_bytes(s: &[u8]) -> Option<u64>779 fn try_positive_integer64_bytes(s: &[u8]) -> Option<u64> {
780 str::from_utf8(s).ok().and_then(|s| s.parse().ok())
781 }
782
try_negative_integer64_bytes(s: &[u8]) -> Option<i64>783 fn try_negative_integer64_bytes(s: &[u8]) -> Option<i64> {
784 str::from_utf8(s).ok().and_then(|s| s.parse().ok())
785 }
786
787 serde_if_integer128! {
788 fn try_positive_integer128_bytes(s: &[u8]) -> Option<u128> {
789 str::from_utf8(s).ok().and_then(|s| s.parse().ok())
790 }
791
792 fn try_negative_integer128_bytes(s: &[u8]) -> Option<i128> {
793 str::from_utf8(s).ok().and_then(|s| s.parse().ok())
794 }
795 }
796
try_float_bytes(s: &[u8]) -> Option<f64>797 fn try_float_bytes(s: &[u8]) -> Option<f64> {
798 str::from_utf8(s).ok().and_then(|s| s.parse().ok())
799 }
800
801 #[cfg(test)]
802 mod tests {
803 use std::collections::HashMap;
804
805 use bstr::BString;
806 use serde::{de::DeserializeOwned, serde_if_integer128, Deserialize};
807
808 use super::{deserialize_byte_record, deserialize_string_record};
809 use crate::byte_record::ByteRecord;
810 use crate::error::Error;
811 use crate::string_record::StringRecord;
812
de<D: DeserializeOwned>(fields: &[&str]) -> Result<D, Error>813 fn de<D: DeserializeOwned>(fields: &[&str]) -> Result<D, Error> {
814 let record = StringRecord::from(fields);
815 deserialize_string_record(&record, None)
816 }
817
de_headers<D: DeserializeOwned>( headers: &[&str], fields: &[&str], ) -> Result<D, Error>818 fn de_headers<D: DeserializeOwned>(
819 headers: &[&str],
820 fields: &[&str],
821 ) -> Result<D, Error> {
822 let headers = StringRecord::from(headers);
823 let record = StringRecord::from(fields);
824 deserialize_string_record(&record, Some(&headers))
825 }
826
b<'a, T: AsRef<[u8]> + ?Sized>(bytes: &'a T) -> &'a [u8]827 fn b<'a, T: AsRef<[u8]> + ?Sized>(bytes: &'a T) -> &'a [u8] {
828 bytes.as_ref()
829 }
830
831 #[test]
with_header()832 fn with_header() {
833 #[derive(Deserialize, Debug, PartialEq)]
834 struct Foo {
835 z: f64,
836 y: i32,
837 x: String,
838 }
839
840 let got: Foo =
841 de_headers(&["x", "y", "z"], &["hi", "42", "1.3"]).unwrap();
842 assert_eq!(got, Foo { x: "hi".into(), y: 42, z: 1.3 });
843 }
844
845 #[test]
with_header_unknown()846 fn with_header_unknown() {
847 #[derive(Deserialize, Debug, PartialEq)]
848 #[serde(deny_unknown_fields)]
849 struct Foo {
850 z: f64,
851 y: i32,
852 x: String,
853 }
854 assert!(de_headers::<Foo>(
855 &["a", "x", "y", "z"],
856 &["foo", "hi", "42", "1.3"],
857 )
858 .is_err());
859 }
860
861 #[test]
with_header_missing()862 fn with_header_missing() {
863 #[derive(Deserialize, Debug, PartialEq)]
864 struct Foo {
865 z: f64,
866 y: i32,
867 x: String,
868 }
869 assert!(de_headers::<Foo>(&["y", "z"], &["42", "1.3"],).is_err());
870 }
871
872 #[test]
with_header_missing_ok()873 fn with_header_missing_ok() {
874 #[derive(Deserialize, Debug, PartialEq)]
875 struct Foo {
876 z: f64,
877 y: i32,
878 x: Option<String>,
879 }
880
881 let got: Foo = de_headers(&["y", "z"], &["42", "1.3"]).unwrap();
882 assert_eq!(got, Foo { x: None, y: 42, z: 1.3 });
883 }
884
885 #[test]
with_header_no_fields()886 fn with_header_no_fields() {
887 #[derive(Deserialize, Debug, PartialEq)]
888 struct Foo {
889 z: f64,
890 y: i32,
891 x: Option<String>,
892 }
893
894 let got = de_headers::<Foo>(&["y", "z"], &[]);
895 assert!(got.is_err());
896 }
897
898 #[test]
with_header_empty()899 fn with_header_empty() {
900 #[derive(Deserialize, Debug, PartialEq)]
901 struct Foo {
902 z: f64,
903 y: i32,
904 x: Option<String>,
905 }
906
907 let got = de_headers::<Foo>(&[], &[]);
908 assert!(got.is_err());
909 }
910
911 #[test]
with_header_empty_ok()912 fn with_header_empty_ok() {
913 #[derive(Deserialize, Debug, PartialEq)]
914 struct Foo;
915
916 #[derive(Deserialize, Debug, PartialEq)]
917 struct Bar {};
918
919 let got = de_headers::<Foo>(&[], &[]);
920 assert_eq!(got.unwrap(), Foo);
921
922 let got = de_headers::<Bar>(&[], &[]);
923 assert_eq!(got.unwrap(), Bar {});
924
925 let got = de_headers::<()>(&[], &[]);
926 assert_eq!(got.unwrap(), ());
927 }
928
929 #[test]
without_header()930 fn without_header() {
931 #[derive(Deserialize, Debug, PartialEq)]
932 struct Foo {
933 z: f64,
934 y: i32,
935 x: String,
936 }
937
938 let got: Foo = de(&["1.3", "42", "hi"]).unwrap();
939 assert_eq!(got, Foo { x: "hi".into(), y: 42, z: 1.3 });
940 }
941
942 #[test]
no_fields()943 fn no_fields() {
944 assert!(de::<String>(&[]).is_err());
945 }
946
947 #[test]
one_field()948 fn one_field() {
949 let got: i32 = de(&["42"]).unwrap();
950 assert_eq!(got, 42);
951 }
952
953 serde_if_integer128! {
954 #[test]
955 fn one_field_128() {
956 let got: i128 = de(&["2010223372036854775808"]).unwrap();
957 assert_eq!(got, 2010223372036854775808);
958 }
959 }
960
961 #[test]
two_fields()962 fn two_fields() {
963 let got: (i32, bool) = de(&["42", "true"]).unwrap();
964 assert_eq!(got, (42, true));
965
966 #[derive(Deserialize, Debug, PartialEq)]
967 struct Foo(i32, bool);
968
969 let got: Foo = de(&["42", "true"]).unwrap();
970 assert_eq!(got, Foo(42, true));
971 }
972
973 #[test]
two_fields_too_many()974 fn two_fields_too_many() {
975 let got: (i32, bool) = de(&["42", "true", "z", "z"]).unwrap();
976 assert_eq!(got, (42, true));
977 }
978
979 #[test]
two_fields_too_few()980 fn two_fields_too_few() {
981 assert!(de::<(i32, bool)>(&["42"]).is_err());
982 }
983
984 #[test]
one_char()985 fn one_char() {
986 let got: char = de(&["a"]).unwrap();
987 assert_eq!(got, 'a');
988 }
989
990 #[test]
no_chars()991 fn no_chars() {
992 assert!(de::<char>(&[""]).is_err());
993 }
994
995 #[test]
too_many_chars()996 fn too_many_chars() {
997 assert!(de::<char>(&["ab"]).is_err());
998 }
999
1000 #[test]
simple_seq()1001 fn simple_seq() {
1002 let got: Vec<i32> = de(&["1", "5", "10"]).unwrap();
1003 assert_eq!(got, vec![1, 5, 10]);
1004 }
1005
1006 #[test]
simple_hex_seq()1007 fn simple_hex_seq() {
1008 let got: Vec<i32> = de(&["0x7F", "0xA9", "0x10"]).unwrap();
1009 assert_eq!(got, vec![0x7F, 0xA9, 0x10]);
1010 }
1011
1012 #[test]
mixed_hex_seq()1013 fn mixed_hex_seq() {
1014 let got: Vec<i32> = de(&["0x7F", "0xA9", "10"]).unwrap();
1015 assert_eq!(got, vec![0x7F, 0xA9, 10]);
1016 }
1017
1018 #[test]
bad_hex_seq()1019 fn bad_hex_seq() {
1020 assert!(de::<Vec<u8>>(&["7F", "0xA9", "10"]).is_err());
1021 }
1022
1023 #[test]
seq_in_struct()1024 fn seq_in_struct() {
1025 #[derive(Deserialize, Debug, PartialEq)]
1026 struct Foo {
1027 xs: Vec<i32>,
1028 }
1029 let got: Foo = de(&["1", "5", "10"]).unwrap();
1030 assert_eq!(got, Foo { xs: vec![1, 5, 10] });
1031 }
1032
1033 #[test]
seq_in_struct_tail()1034 fn seq_in_struct_tail() {
1035 #[derive(Deserialize, Debug, PartialEq)]
1036 struct Foo {
1037 label: String,
1038 xs: Vec<i32>,
1039 }
1040 let got: Foo = de(&["foo", "1", "5", "10"]).unwrap();
1041 assert_eq!(got, Foo { label: "foo".into(), xs: vec![1, 5, 10] });
1042 }
1043
1044 #[test]
map_headers()1045 fn map_headers() {
1046 let got: HashMap<String, i32> =
1047 de_headers(&["a", "b", "c"], &["1", "5", "10"]).unwrap();
1048 assert_eq!(got.len(), 3);
1049 assert_eq!(got["a"], 1);
1050 assert_eq!(got["b"], 5);
1051 assert_eq!(got["c"], 10);
1052 }
1053
1054 #[test]
map_no_headers()1055 fn map_no_headers() {
1056 let got = de::<HashMap<String, i32>>(&["1", "5", "10"]);
1057 assert!(got.is_err());
1058 }
1059
1060 #[test]
bytes()1061 fn bytes() {
1062 let got: Vec<u8> = de::<BString>(&["foobar"]).unwrap().into();
1063 assert_eq!(got, b"foobar".to_vec());
1064 }
1065
1066 #[test]
adjacent_fixed_arrays()1067 fn adjacent_fixed_arrays() {
1068 let got: ([u32; 2], [u32; 2]) = de(&["1", "5", "10", "15"]).unwrap();
1069 assert_eq!(got, ([1, 5], [10, 15]));
1070 }
1071
1072 #[test]
enum_label_simple_tagged()1073 fn enum_label_simple_tagged() {
1074 #[derive(Deserialize, Debug, PartialEq)]
1075 struct Row {
1076 label: Label,
1077 x: f64,
1078 }
1079
1080 #[derive(Deserialize, Debug, PartialEq)]
1081 #[serde(rename_all = "snake_case")]
1082 enum Label {
1083 Foo,
1084 Bar,
1085 Baz,
1086 }
1087
1088 let got: Row = de_headers(&["label", "x"], &["bar", "5"]).unwrap();
1089 assert_eq!(got, Row { label: Label::Bar, x: 5.0 });
1090 }
1091
1092 #[test]
enum_untagged()1093 fn enum_untagged() {
1094 #[derive(Deserialize, Debug, PartialEq)]
1095 struct Row {
1096 x: Boolish,
1097 y: Boolish,
1098 z: Boolish,
1099 }
1100
1101 #[derive(Deserialize, Debug, PartialEq)]
1102 #[serde(rename_all = "snake_case")]
1103 #[serde(untagged)]
1104 enum Boolish {
1105 Bool(bool),
1106 Number(i64),
1107 String(String),
1108 }
1109
1110 let got: Row =
1111 de_headers(&["x", "y", "z"], &["true", "null", "1"]).unwrap();
1112 assert_eq!(
1113 got,
1114 Row {
1115 x: Boolish::Bool(true),
1116 y: Boolish::String("null".into()),
1117 z: Boolish::Number(1),
1118 }
1119 );
1120 }
1121
1122 #[test]
option_empty_field()1123 fn option_empty_field() {
1124 #[derive(Deserialize, Debug, PartialEq)]
1125 struct Foo {
1126 a: Option<i32>,
1127 b: String,
1128 c: Option<i32>,
1129 }
1130
1131 let got: Foo =
1132 de_headers(&["a", "b", "c"], &["", "foo", "5"]).unwrap();
1133 assert_eq!(got, Foo { a: None, b: "foo".into(), c: Some(5) });
1134 }
1135
1136 #[test]
option_invalid_field()1137 fn option_invalid_field() {
1138 #[derive(Deserialize, Debug, PartialEq)]
1139 struct Foo {
1140 #[serde(deserialize_with = "crate::invalid_option")]
1141 a: Option<i32>,
1142 #[serde(deserialize_with = "crate::invalid_option")]
1143 b: Option<i32>,
1144 #[serde(deserialize_with = "crate::invalid_option")]
1145 c: Option<i32>,
1146 }
1147
1148 let got: Foo =
1149 de_headers(&["a", "b", "c"], &["xyz", "", "5"]).unwrap();
1150 assert_eq!(got, Foo { a: None, b: None, c: Some(5) });
1151 }
1152
1153 #[test]
borrowed()1154 fn borrowed() {
1155 #[derive(Deserialize, Debug, PartialEq)]
1156 struct Foo<'a, 'c> {
1157 a: &'a str,
1158 b: i32,
1159 c: &'c str,
1160 }
1161
1162 let headers = StringRecord::from(vec!["a", "b", "c"]);
1163 let record = StringRecord::from(vec!["foo", "5", "bar"]);
1164 let got: Foo =
1165 deserialize_string_record(&record, Some(&headers)).unwrap();
1166 assert_eq!(got, Foo { a: "foo", b: 5, c: "bar" });
1167 }
1168
1169 #[test]
borrowed_map()1170 fn borrowed_map() {
1171 use std::collections::HashMap;
1172
1173 let headers = StringRecord::from(vec!["a", "b", "c"]);
1174 let record = StringRecord::from(vec!["aardvark", "bee", "cat"]);
1175 let got: HashMap<&str, &str> =
1176 deserialize_string_record(&record, Some(&headers)).unwrap();
1177
1178 let expected: HashMap<&str, &str> =
1179 headers.iter().zip(&record).collect();
1180 assert_eq!(got, expected);
1181 }
1182
1183 #[test]
borrowed_map_bytes()1184 fn borrowed_map_bytes() {
1185 use std::collections::HashMap;
1186
1187 let headers = ByteRecord::from(vec![b"a", b"\xFF", b"c"]);
1188 let record = ByteRecord::from(vec!["aardvark", "bee", "cat"]);
1189 let got: HashMap<&[u8], &[u8]> =
1190 deserialize_byte_record(&record, Some(&headers)).unwrap();
1191
1192 let expected: HashMap<&[u8], &[u8]> =
1193 headers.iter().zip(&record).collect();
1194 assert_eq!(got, expected);
1195 }
1196
1197 #[test]
flatten()1198 fn flatten() {
1199 #[derive(Deserialize, Debug, PartialEq)]
1200 struct Input {
1201 x: f64,
1202 y: f64,
1203 }
1204
1205 #[derive(Deserialize, Debug, PartialEq)]
1206 struct Properties {
1207 prop1: f64,
1208 prop2: f64,
1209 }
1210
1211 #[derive(Deserialize, Debug, PartialEq)]
1212 struct Row {
1213 #[serde(flatten)]
1214 input: Input,
1215 #[serde(flatten)]
1216 properties: Properties,
1217 }
1218
1219 let header = StringRecord::from(vec!["x", "y", "prop1", "prop2"]);
1220 let record = StringRecord::from(vec!["1", "2", "3", "4"]);
1221 let got: Row = record.deserialize(Some(&header)).unwrap();
1222 assert_eq!(
1223 got,
1224 Row {
1225 input: Input { x: 1.0, y: 2.0 },
1226 properties: Properties { prop1: 3.0, prop2: 4.0 },
1227 }
1228 );
1229 }
1230
1231 #[test]
partially_invalid_utf8()1232 fn partially_invalid_utf8() {
1233 #[derive(Debug, Deserialize, PartialEq)]
1234 struct Row {
1235 h1: String,
1236 h2: BString,
1237 h3: String,
1238 }
1239
1240 let headers = ByteRecord::from(vec![b"h1", b"h2", b"h3"]);
1241 let record =
1242 ByteRecord::from(vec![b(b"baz"), b(b"foo\xFFbar"), b(b"quux")]);
1243 let got: Row =
1244 deserialize_byte_record(&record, Some(&headers)).unwrap();
1245 assert_eq!(
1246 got,
1247 Row {
1248 h1: "baz".to_string(),
1249 h2: BString::from(b"foo\xFFbar".to_vec()),
1250 h3: "quux".to_string(),
1251 }
1252 );
1253 }
1254 }
1255