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 = if field.starts_with("0x") {
339 <$inttype>::from_str_radix(&field[2..], 16)
340 } else {
341 field.parse()
342 };
343 visitor.$visit(num.map_err(|err| self.error(DEK::ParseInt(err)))?)
344 }
345 };
346 }
347
348 impl<'a, 'de: 'a, T: DeRecord<'de>> Deserializer<'de>
349 for &'a mut DeRecordWrap<T>
350 {
351 type Error = DeserializeError;
352
deserialize_any<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>353 fn deserialize_any<V: Visitor<'de>>(
354 self,
355 visitor: V,
356 ) -> Result<V::Value, Self::Error> {
357 self.infer_deserialize(visitor)
358 }
359
deserialize_bool<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>360 fn deserialize_bool<V: Visitor<'de>>(
361 self,
362 visitor: V,
363 ) -> Result<V::Value, Self::Error> {
364 visitor.visit_bool(
365 self.next_field()?
366 .parse()
367 .map_err(|err| self.error(DEK::ParseBool(err)))?,
368 )
369 }
370
371 deserialize_int!(deserialize_u8, visit_u8, u8);
372 deserialize_int!(deserialize_u16, visit_u16, u16);
373 deserialize_int!(deserialize_u32, visit_u32, u32);
374 deserialize_int!(deserialize_u64, visit_u64, u64);
375 serde_if_integer128! {
376 deserialize_int!(deserialize_u128, visit_u128, u128);
377 }
378 deserialize_int!(deserialize_i8, visit_i8, i8);
379 deserialize_int!(deserialize_i16, visit_i16, i16);
380 deserialize_int!(deserialize_i32, visit_i32, i32);
381 deserialize_int!(deserialize_i64, visit_i64, i64);
382 serde_if_integer128! {
383 deserialize_int!(deserialize_i128, visit_i128, i128);
384 }
385
deserialize_f32<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>386 fn deserialize_f32<V: Visitor<'de>>(
387 self,
388 visitor: V,
389 ) -> Result<V::Value, Self::Error> {
390 visitor.visit_f32(
391 self.next_field()?
392 .parse()
393 .map_err(|err| self.error(DEK::ParseFloat(err)))?,
394 )
395 }
396
deserialize_f64<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>397 fn deserialize_f64<V: Visitor<'de>>(
398 self,
399 visitor: V,
400 ) -> Result<V::Value, Self::Error> {
401 visitor.visit_f64(
402 self.next_field()?
403 .parse()
404 .map_err(|err| self.error(DEK::ParseFloat(err)))?,
405 )
406 }
407
deserialize_char<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>408 fn deserialize_char<V: Visitor<'de>>(
409 self,
410 visitor: V,
411 ) -> Result<V::Value, Self::Error> {
412 let field = self.next_field()?;
413 let len = field.chars().count();
414 if len != 1 {
415 return Err(self.error(DEK::Message(format!(
416 "expected single character but got {} characters in '{}'",
417 len, field
418 ))));
419 }
420 visitor.visit_char(field.chars().next().unwrap())
421 }
422
deserialize_str<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>423 fn deserialize_str<V: Visitor<'de>>(
424 self,
425 visitor: V,
426 ) -> Result<V::Value, Self::Error> {
427 self.next_field().and_then(|f| visitor.visit_borrowed_str(f))
428 }
429
deserialize_string<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>430 fn deserialize_string<V: Visitor<'de>>(
431 self,
432 visitor: V,
433 ) -> Result<V::Value, Self::Error> {
434 self.next_field().and_then(|f| visitor.visit_str(f.into()))
435 }
436
deserialize_bytes<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>437 fn deserialize_bytes<V: Visitor<'de>>(
438 self,
439 visitor: V,
440 ) -> Result<V::Value, Self::Error> {
441 self.next_field_bytes().and_then(|f| visitor.visit_borrowed_bytes(f))
442 }
443
deserialize_byte_buf<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>444 fn deserialize_byte_buf<V: Visitor<'de>>(
445 self,
446 visitor: V,
447 ) -> Result<V::Value, Self::Error> {
448 self.next_field_bytes()
449 .and_then(|f| visitor.visit_byte_buf(f.to_vec()))
450 }
451
deserialize_option<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>452 fn deserialize_option<V: Visitor<'de>>(
453 self,
454 visitor: V,
455 ) -> Result<V::Value, Self::Error> {
456 match self.peek_field() {
457 None => visitor.visit_none(),
458 Some(f) if f.is_empty() => {
459 self.next_field().expect("empty field");
460 visitor.visit_none()
461 }
462 Some(_) => visitor.visit_some(self),
463 }
464 }
465
deserialize_unit<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>466 fn deserialize_unit<V: Visitor<'de>>(
467 self,
468 visitor: V,
469 ) -> Result<V::Value, Self::Error> {
470 visitor.visit_unit()
471 }
472
deserialize_unit_struct<V: Visitor<'de>>( self, _name: &'static str, visitor: V, ) -> Result<V::Value, Self::Error>473 fn deserialize_unit_struct<V: Visitor<'de>>(
474 self,
475 _name: &'static str,
476 visitor: V,
477 ) -> Result<V::Value, Self::Error> {
478 visitor.visit_unit()
479 }
480
deserialize_newtype_struct<V: Visitor<'de>>( self, _name: &'static str, visitor: V, ) -> Result<V::Value, Self::Error>481 fn deserialize_newtype_struct<V: Visitor<'de>>(
482 self,
483 _name: &'static str,
484 visitor: V,
485 ) -> Result<V::Value, Self::Error> {
486 visitor.visit_newtype_struct(self)
487 }
488
deserialize_seq<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>489 fn deserialize_seq<V: Visitor<'de>>(
490 self,
491 visitor: V,
492 ) -> Result<V::Value, Self::Error> {
493 visitor.visit_seq(self)
494 }
495
deserialize_tuple<V: Visitor<'de>>( self, _len: usize, visitor: V, ) -> Result<V::Value, Self::Error>496 fn deserialize_tuple<V: Visitor<'de>>(
497 self,
498 _len: usize,
499 visitor: V,
500 ) -> Result<V::Value, Self::Error> {
501 visitor.visit_seq(self)
502 }
503
deserialize_tuple_struct<V: Visitor<'de>>( self, _name: &'static str, _len: usize, visitor: V, ) -> Result<V::Value, Self::Error>504 fn deserialize_tuple_struct<V: Visitor<'de>>(
505 self,
506 _name: &'static str,
507 _len: usize,
508 visitor: V,
509 ) -> Result<V::Value, Self::Error> {
510 visitor.visit_seq(self)
511 }
512
deserialize_map<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>513 fn deserialize_map<V: Visitor<'de>>(
514 self,
515 visitor: V,
516 ) -> Result<V::Value, Self::Error> {
517 if !self.has_headers() {
518 visitor.visit_seq(self)
519 } else {
520 visitor.visit_map(self)
521 }
522 }
523
deserialize_struct<V: Visitor<'de>>( self, _name: &'static str, _fields: &'static [&'static str], visitor: V, ) -> Result<V::Value, Self::Error>524 fn deserialize_struct<V: Visitor<'de>>(
525 self,
526 _name: &'static str,
527 _fields: &'static [&'static str],
528 visitor: V,
529 ) -> Result<V::Value, Self::Error> {
530 if !self.has_headers() {
531 visitor.visit_seq(self)
532 } else {
533 visitor.visit_map(self)
534 }
535 }
536
deserialize_identifier<V: Visitor<'de>>( self, _visitor: V, ) -> Result<V::Value, Self::Error>537 fn deserialize_identifier<V: Visitor<'de>>(
538 self,
539 _visitor: V,
540 ) -> Result<V::Value, Self::Error> {
541 Err(self.error(DEK::Unsupported("deserialize_identifier".into())))
542 }
543
deserialize_enum<V: Visitor<'de>>( self, _name: &'static str, _variants: &'static [&'static str], visitor: V, ) -> Result<V::Value, Self::Error>544 fn deserialize_enum<V: Visitor<'de>>(
545 self,
546 _name: &'static str,
547 _variants: &'static [&'static str],
548 visitor: V,
549 ) -> Result<V::Value, Self::Error> {
550 visitor.visit_enum(self)
551 }
552
deserialize_ignored_any<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value, Self::Error>553 fn deserialize_ignored_any<V: Visitor<'de>>(
554 self,
555 visitor: V,
556 ) -> Result<V::Value, Self::Error> {
557 // Read and drop the next field.
558 // This code is reached, e.g., when trying to deserialize a header
559 // that doesn't exist in the destination struct.
560 let _ = self.next_field_bytes()?;
561 visitor.visit_unit()
562 }
563 }
564
565 impl<'a, 'de: 'a, T: DeRecord<'de>> EnumAccess<'de>
566 for &'a mut DeRecordWrap<T>
567 {
568 type Error = DeserializeError;
569 type Variant = Self;
570
variant_seed<V: DeserializeSeed<'de>>( self, seed: V, ) -> Result<(V::Value, Self::Variant), Self::Error>571 fn variant_seed<V: DeserializeSeed<'de>>(
572 self,
573 seed: V,
574 ) -> Result<(V::Value, Self::Variant), Self::Error> {
575 let variant_name = self.next_field()?;
576 seed.deserialize(variant_name.into_deserializer()).map(|v| (v, self))
577 }
578 }
579
580 impl<'a, 'de: 'a, T: DeRecord<'de>> VariantAccess<'de>
581 for &'a mut DeRecordWrap<T>
582 {
583 type Error = DeserializeError;
584
unit_variant(self) -> Result<(), Self::Error>585 fn unit_variant(self) -> Result<(), Self::Error> {
586 Ok(())
587 }
588
newtype_variant_seed<U: DeserializeSeed<'de>>( self, _seed: U, ) -> Result<U::Value, Self::Error>589 fn newtype_variant_seed<U: DeserializeSeed<'de>>(
590 self,
591 _seed: U,
592 ) -> Result<U::Value, Self::Error> {
593 let unexp = Unexpected::UnitVariant;
594 Err(DeserializeError::invalid_type(unexp, &"newtype variant"))
595 }
596
tuple_variant<V: Visitor<'de>>( self, _len: usize, _visitor: V, ) -> Result<V::Value, Self::Error>597 fn tuple_variant<V: Visitor<'de>>(
598 self,
599 _len: usize,
600 _visitor: V,
601 ) -> Result<V::Value, Self::Error> {
602 let unexp = Unexpected::UnitVariant;
603 Err(DeserializeError::invalid_type(unexp, &"tuple variant"))
604 }
605
struct_variant<V: Visitor<'de>>( self, _fields: &'static [&'static str], _visitor: V, ) -> Result<V::Value, Self::Error>606 fn struct_variant<V: Visitor<'de>>(
607 self,
608 _fields: &'static [&'static str],
609 _visitor: V,
610 ) -> Result<V::Value, Self::Error> {
611 let unexp = Unexpected::UnitVariant;
612 Err(DeserializeError::invalid_type(unexp, &"struct variant"))
613 }
614 }
615
616 impl<'a, 'de: 'a, T: DeRecord<'de>> SeqAccess<'de>
617 for &'a mut DeRecordWrap<T>
618 {
619 type Error = DeserializeError;
620
next_element_seed<U: DeserializeSeed<'de>>( &mut self, seed: U, ) -> Result<Option<U::Value>, Self::Error>621 fn next_element_seed<U: DeserializeSeed<'de>>(
622 &mut self,
623 seed: U,
624 ) -> Result<Option<U::Value>, Self::Error> {
625 if self.peek_field().is_none() {
626 Ok(None)
627 } else {
628 seed.deserialize(&mut **self).map(Some)
629 }
630 }
631 }
632
633 impl<'a, 'de: 'a, T: DeRecord<'de>> MapAccess<'de>
634 for &'a mut DeRecordWrap<T>
635 {
636 type Error = DeserializeError;
637
next_key_seed<K: DeserializeSeed<'de>>( &mut self, seed: K, ) -> Result<Option<K::Value>, Self::Error>638 fn next_key_seed<K: DeserializeSeed<'de>>(
639 &mut self,
640 seed: K,
641 ) -> Result<Option<K::Value>, Self::Error> {
642 assert!(self.has_headers());
643 let field = match self.next_header_bytes()? {
644 None => return Ok(None),
645 Some(field) => field,
646 };
647 seed.deserialize(BorrowedBytesDeserializer::new(field)).map(Some)
648 }
649
next_value_seed<K: DeserializeSeed<'de>>( &mut self, seed: K, ) -> Result<K::Value, Self::Error>650 fn next_value_seed<K: DeserializeSeed<'de>>(
651 &mut self,
652 seed: K,
653 ) -> Result<K::Value, Self::Error> {
654 seed.deserialize(&mut **self)
655 }
656 }
657
658 /// An Serde deserialization error.
659 #[derive(Clone, Debug, Eq, PartialEq)]
660 pub struct DeserializeError {
661 field: Option<u64>,
662 kind: DeserializeErrorKind,
663 }
664
665 /// The type of a Serde deserialization error.
666 #[derive(Clone, Debug, Eq, PartialEq)]
667 pub enum DeserializeErrorKind {
668 /// A generic Serde deserialization error.
669 Message(String),
670 /// A generic Serde unsupported error.
671 Unsupported(String),
672 /// This error occurs when a Rust type expects to decode another field
673 /// from a row, but no more fields exist.
674 UnexpectedEndOfRow,
675 /// This error occurs when UTF-8 validation on a field fails. UTF-8
676 /// validation is only performed when the Rust type requires it (e.g.,
677 /// a `String` or `&str` type).
678 InvalidUtf8(str::Utf8Error),
679 /// This error occurs when a boolean value fails to parse.
680 ParseBool(str::ParseBoolError),
681 /// This error occurs when an integer value fails to parse.
682 ParseInt(num::ParseIntError),
683 /// This error occurs when a float value fails to parse.
684 ParseFloat(num::ParseFloatError),
685 }
686
687 impl SerdeError for DeserializeError {
custom<T: fmt::Display>(msg: T) -> DeserializeError688 fn custom<T: fmt::Display>(msg: T) -> DeserializeError {
689 DeserializeError { field: None, kind: DEK::Message(msg.to_string()) }
690 }
691 }
692
693 impl StdError for DeserializeError {
description(&self) -> &str694 fn description(&self) -> &str {
695 self.kind.description()
696 }
697 }
698
699 impl fmt::Display for DeserializeError {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result700 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
701 if let Some(field) = self.field {
702 write!(f, "field {}: {}", field, self.kind)
703 } else {
704 write!(f, "{}", self.kind)
705 }
706 }
707 }
708
709 impl fmt::Display for DeserializeErrorKind {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result710 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
711 use self::DeserializeErrorKind::*;
712
713 match *self {
714 Message(ref msg) => write!(f, "{}", msg),
715 Unsupported(ref which) => {
716 write!(f, "unsupported deserializer method: {}", which)
717 }
718 UnexpectedEndOfRow => write!(f, "{}", self.description()),
719 InvalidUtf8(ref err) => err.fmt(f),
720 ParseBool(ref err) => err.fmt(f),
721 ParseInt(ref err) => err.fmt(f),
722 ParseFloat(ref err) => err.fmt(f),
723 }
724 }
725 }
726
727 impl DeserializeError {
728 /// Return the field index (starting at 0) of this error, if available.
field(&self) -> Option<u64>729 pub fn field(&self) -> Option<u64> {
730 self.field
731 }
732
733 /// Return the underlying error kind.
kind(&self) -> &DeserializeErrorKind734 pub fn kind(&self) -> &DeserializeErrorKind {
735 &self.kind
736 }
737 }
738
739 impl DeserializeErrorKind {
740 #[allow(deprecated)]
description(&self) -> &str741 fn description(&self) -> &str {
742 use self::DeserializeErrorKind::*;
743
744 match *self {
745 Message(_) => "deserialization error",
746 Unsupported(_) => "unsupported deserializer method",
747 UnexpectedEndOfRow => "expected field, but got end of row",
748 InvalidUtf8(ref err) => err.description(),
749 ParseBool(ref err) => err.description(),
750 ParseInt(ref err) => err.description(),
751 ParseFloat(ref err) => err.description(),
752 }
753 }
754 }
755
756 serde_if_integer128! {
757 fn try_positive_integer128(s: &str) -> Option<u128> {
758 s.parse().ok()
759 }
760
761 fn try_negative_integer128(s: &str) -> Option<i128> {
762 s.parse().ok()
763 }
764 }
765
try_positive_integer64(s: &str) -> Option<u64>766 fn try_positive_integer64(s: &str) -> Option<u64> {
767 s.parse().ok()
768 }
769
try_negative_integer64(s: &str) -> Option<i64>770 fn try_negative_integer64(s: &str) -> Option<i64> {
771 s.parse().ok()
772 }
773
try_float(s: &str) -> Option<f64>774 fn try_float(s: &str) -> Option<f64> {
775 s.parse().ok()
776 }
777
try_positive_integer64_bytes(s: &[u8]) -> Option<u64>778 fn try_positive_integer64_bytes(s: &[u8]) -> Option<u64> {
779 str::from_utf8(s).ok().and_then(|s| s.parse().ok())
780 }
781
try_negative_integer64_bytes(s: &[u8]) -> Option<i64>782 fn try_negative_integer64_bytes(s: &[u8]) -> Option<i64> {
783 str::from_utf8(s).ok().and_then(|s| s.parse().ok())
784 }
785
786 serde_if_integer128! {
787 fn try_positive_integer128_bytes(s: &[u8]) -> Option<u128> {
788 str::from_utf8(s).ok().and_then(|s| s.parse().ok())
789 }
790
791 fn try_negative_integer128_bytes(s: &[u8]) -> Option<i128> {
792 str::from_utf8(s).ok().and_then(|s| s.parse().ok())
793 }
794 }
795
try_float_bytes(s: &[u8]) -> Option<f64>796 fn try_float_bytes(s: &[u8]) -> Option<f64> {
797 str::from_utf8(s).ok().and_then(|s| s.parse().ok())
798 }
799
800 #[cfg(test)]
801 mod tests {
802 use std::collections::HashMap;
803
804 use bstr::BString;
805 use serde::{de::DeserializeOwned, serde_if_integer128, Deserialize};
806
807 use super::{deserialize_byte_record, deserialize_string_record};
808 use crate::byte_record::ByteRecord;
809 use crate::error::Error;
810 use crate::string_record::StringRecord;
811
de<D: DeserializeOwned>(fields: &[&str]) -> Result<D, Error>812 fn de<D: DeserializeOwned>(fields: &[&str]) -> Result<D, Error> {
813 let record = StringRecord::from(fields);
814 deserialize_string_record(&record, None)
815 }
816
de_headers<D: DeserializeOwned>( headers: &[&str], fields: &[&str], ) -> Result<D, Error>817 fn de_headers<D: DeserializeOwned>(
818 headers: &[&str],
819 fields: &[&str],
820 ) -> Result<D, Error> {
821 let headers = StringRecord::from(headers);
822 let record = StringRecord::from(fields);
823 deserialize_string_record(&record, Some(&headers))
824 }
825
b<'a, T: AsRef<[u8]> + ?Sized>(bytes: &'a T) -> &'a [u8]826 fn b<'a, T: AsRef<[u8]> + ?Sized>(bytes: &'a T) -> &'a [u8] {
827 bytes.as_ref()
828 }
829
830 #[test]
with_header()831 fn with_header() {
832 #[derive(Deserialize, Debug, PartialEq)]
833 struct Foo {
834 z: f64,
835 y: i32,
836 x: String,
837 }
838
839 let got: Foo =
840 de_headers(&["x", "y", "z"], &["hi", "42", "1.3"]).unwrap();
841 assert_eq!(got, Foo { x: "hi".into(), y: 42, z: 1.3 });
842 }
843
844 #[test]
with_header_unknown()845 fn with_header_unknown() {
846 #[derive(Deserialize, Debug, PartialEq)]
847 #[serde(deny_unknown_fields)]
848 struct Foo {
849 z: f64,
850 y: i32,
851 x: String,
852 }
853 assert!(de_headers::<Foo>(
854 &["a", "x", "y", "z"],
855 &["foo", "hi", "42", "1.3"],
856 )
857 .is_err());
858 }
859
860 #[test]
with_header_missing()861 fn with_header_missing() {
862 #[derive(Deserialize, Debug, PartialEq)]
863 struct Foo {
864 z: f64,
865 y: i32,
866 x: String,
867 }
868 assert!(de_headers::<Foo>(&["y", "z"], &["42", "1.3"],).is_err());
869 }
870
871 #[test]
with_header_missing_ok()872 fn with_header_missing_ok() {
873 #[derive(Deserialize, Debug, PartialEq)]
874 struct Foo {
875 z: f64,
876 y: i32,
877 x: Option<String>,
878 }
879
880 let got: Foo = de_headers(&["y", "z"], &["42", "1.3"]).unwrap();
881 assert_eq!(got, Foo { x: None, y: 42, z: 1.3 });
882 }
883
884 #[test]
with_header_no_fields()885 fn with_header_no_fields() {
886 #[derive(Deserialize, Debug, PartialEq)]
887 struct Foo {
888 z: f64,
889 y: i32,
890 x: Option<String>,
891 }
892
893 let got = de_headers::<Foo>(&["y", "z"], &[]);
894 assert!(got.is_err());
895 }
896
897 #[test]
with_header_empty()898 fn with_header_empty() {
899 #[derive(Deserialize, Debug, PartialEq)]
900 struct Foo {
901 z: f64,
902 y: i32,
903 x: Option<String>,
904 }
905
906 let got = de_headers::<Foo>(&[], &[]);
907 assert!(got.is_err());
908 }
909
910 #[test]
with_header_empty_ok()911 fn with_header_empty_ok() {
912 #[derive(Deserialize, Debug, PartialEq)]
913 struct Foo;
914
915 #[derive(Deserialize, Debug, PartialEq)]
916 struct Bar {};
917
918 let got = de_headers::<Foo>(&[], &[]);
919 assert_eq!(got.unwrap(), Foo);
920
921 let got = de_headers::<Bar>(&[], &[]);
922 assert_eq!(got.unwrap(), Bar {});
923
924 let got = de_headers::<()>(&[], &[]);
925 assert_eq!(got.unwrap(), ());
926 }
927
928 #[test]
without_header()929 fn without_header() {
930 #[derive(Deserialize, Debug, PartialEq)]
931 struct Foo {
932 z: f64,
933 y: i32,
934 x: String,
935 }
936
937 let got: Foo = de(&["1.3", "42", "hi"]).unwrap();
938 assert_eq!(got, Foo { x: "hi".into(), y: 42, z: 1.3 });
939 }
940
941 #[test]
no_fields()942 fn no_fields() {
943 assert!(de::<String>(&[]).is_err());
944 }
945
946 #[test]
one_field()947 fn one_field() {
948 let got: i32 = de(&["42"]).unwrap();
949 assert_eq!(got, 42);
950 }
951
952 serde_if_integer128! {
953 #[test]
954 fn one_field_128() {
955 let got: i128 = de(&["2010223372036854775808"]).unwrap();
956 assert_eq!(got, 2010223372036854775808);
957 }
958 }
959
960 #[test]
two_fields()961 fn two_fields() {
962 let got: (i32, bool) = de(&["42", "true"]).unwrap();
963 assert_eq!(got, (42, true));
964
965 #[derive(Deserialize, Debug, PartialEq)]
966 struct Foo(i32, bool);
967
968 let got: Foo = de(&["42", "true"]).unwrap();
969 assert_eq!(got, Foo(42, true));
970 }
971
972 #[test]
two_fields_too_many()973 fn two_fields_too_many() {
974 let got: (i32, bool) = de(&["42", "true", "z", "z"]).unwrap();
975 assert_eq!(got, (42, true));
976 }
977
978 #[test]
two_fields_too_few()979 fn two_fields_too_few() {
980 assert!(de::<(i32, bool)>(&["42"]).is_err());
981 }
982
983 #[test]
one_char()984 fn one_char() {
985 let got: char = de(&["a"]).unwrap();
986 assert_eq!(got, 'a');
987 }
988
989 #[test]
no_chars()990 fn no_chars() {
991 assert!(de::<char>(&[""]).is_err());
992 }
993
994 #[test]
too_many_chars()995 fn too_many_chars() {
996 assert!(de::<char>(&["ab"]).is_err());
997 }
998
999 #[test]
simple_seq()1000 fn simple_seq() {
1001 let got: Vec<i32> = de(&["1", "5", "10"]).unwrap();
1002 assert_eq!(got, vec![1, 5, 10]);
1003 }
1004
1005 #[test]
simple_hex_seq()1006 fn simple_hex_seq() {
1007 let got: Vec<i32> = de(&["0x7F", "0xA9", "0x10"]).unwrap();
1008 assert_eq!(got, vec![0x7F, 0xA9, 0x10]);
1009 }
1010
1011 #[test]
mixed_hex_seq()1012 fn mixed_hex_seq() {
1013 let got: Vec<i32> = de(&["0x7F", "0xA9", "10"]).unwrap();
1014 assert_eq!(got, vec![0x7F, 0xA9, 10]);
1015 }
1016
1017 #[test]
bad_hex_seq()1018 fn bad_hex_seq() {
1019 assert!(de::<Vec<u8>>(&["7F", "0xA9", "10"]).is_err());
1020 }
1021
1022 #[test]
seq_in_struct()1023 fn seq_in_struct() {
1024 #[derive(Deserialize, Debug, PartialEq)]
1025 struct Foo {
1026 xs: Vec<i32>,
1027 }
1028 let got: Foo = de(&["1", "5", "10"]).unwrap();
1029 assert_eq!(got, Foo { xs: vec![1, 5, 10] });
1030 }
1031
1032 #[test]
seq_in_struct_tail()1033 fn seq_in_struct_tail() {
1034 #[derive(Deserialize, Debug, PartialEq)]
1035 struct Foo {
1036 label: String,
1037 xs: Vec<i32>,
1038 }
1039 let got: Foo = de(&["foo", "1", "5", "10"]).unwrap();
1040 assert_eq!(got, Foo { label: "foo".into(), xs: vec![1, 5, 10] });
1041 }
1042
1043 #[test]
map_headers()1044 fn map_headers() {
1045 let got: HashMap<String, i32> =
1046 de_headers(&["a", "b", "c"], &["1", "5", "10"]).unwrap();
1047 assert_eq!(got.len(), 3);
1048 assert_eq!(got["a"], 1);
1049 assert_eq!(got["b"], 5);
1050 assert_eq!(got["c"], 10);
1051 }
1052
1053 #[test]
map_no_headers()1054 fn map_no_headers() {
1055 let got = de::<HashMap<String, i32>>(&["1", "5", "10"]);
1056 assert!(got.is_err());
1057 }
1058
1059 #[test]
bytes()1060 fn bytes() {
1061 let got: Vec<u8> = de::<BString>(&["foobar"]).unwrap().into();
1062 assert_eq!(got, b"foobar".to_vec());
1063 }
1064
1065 #[test]
adjacent_fixed_arrays()1066 fn adjacent_fixed_arrays() {
1067 let got: ([u32; 2], [u32; 2]) = de(&["1", "5", "10", "15"]).unwrap();
1068 assert_eq!(got, ([1, 5], [10, 15]));
1069 }
1070
1071 #[test]
enum_label_simple_tagged()1072 fn enum_label_simple_tagged() {
1073 #[derive(Deserialize, Debug, PartialEq)]
1074 struct Row {
1075 label: Label,
1076 x: f64,
1077 }
1078
1079 #[derive(Deserialize, Debug, PartialEq)]
1080 #[serde(rename_all = "snake_case")]
1081 enum Label {
1082 Foo,
1083 Bar,
1084 Baz,
1085 }
1086
1087 let got: Row = de_headers(&["label", "x"], &["bar", "5"]).unwrap();
1088 assert_eq!(got, Row { label: Label::Bar, x: 5.0 });
1089 }
1090
1091 #[test]
enum_untagged()1092 fn enum_untagged() {
1093 #[derive(Deserialize, Debug, PartialEq)]
1094 struct Row {
1095 x: Boolish,
1096 y: Boolish,
1097 z: Boolish,
1098 }
1099
1100 #[derive(Deserialize, Debug, PartialEq)]
1101 #[serde(rename_all = "snake_case")]
1102 #[serde(untagged)]
1103 enum Boolish {
1104 Bool(bool),
1105 Number(i64),
1106 String(String),
1107 }
1108
1109 let got: Row =
1110 de_headers(&["x", "y", "z"], &["true", "null", "1"]).unwrap();
1111 assert_eq!(
1112 got,
1113 Row {
1114 x: Boolish::Bool(true),
1115 y: Boolish::String("null".into()),
1116 z: Boolish::Number(1),
1117 }
1118 );
1119 }
1120
1121 #[test]
option_empty_field()1122 fn option_empty_field() {
1123 #[derive(Deserialize, Debug, PartialEq)]
1124 struct Foo {
1125 a: Option<i32>,
1126 b: String,
1127 c: Option<i32>,
1128 }
1129
1130 let got: Foo =
1131 de_headers(&["a", "b", "c"], &["", "foo", "5"]).unwrap();
1132 assert_eq!(got, Foo { a: None, b: "foo".into(), c: Some(5) });
1133 }
1134
1135 #[test]
option_invalid_field()1136 fn option_invalid_field() {
1137 #[derive(Deserialize, Debug, PartialEq)]
1138 struct Foo {
1139 #[serde(deserialize_with = "crate::invalid_option")]
1140 a: Option<i32>,
1141 #[serde(deserialize_with = "crate::invalid_option")]
1142 b: Option<i32>,
1143 #[serde(deserialize_with = "crate::invalid_option")]
1144 c: Option<i32>,
1145 }
1146
1147 let got: Foo =
1148 de_headers(&["a", "b", "c"], &["xyz", "", "5"]).unwrap();
1149 assert_eq!(got, Foo { a: None, b: None, c: Some(5) });
1150 }
1151
1152 #[test]
borrowed()1153 fn borrowed() {
1154 #[derive(Deserialize, Debug, PartialEq)]
1155 struct Foo<'a, 'c> {
1156 a: &'a str,
1157 b: i32,
1158 c: &'c str,
1159 }
1160
1161 let headers = StringRecord::from(vec!["a", "b", "c"]);
1162 let record = StringRecord::from(vec!["foo", "5", "bar"]);
1163 let got: Foo =
1164 deserialize_string_record(&record, Some(&headers)).unwrap();
1165 assert_eq!(got, Foo { a: "foo", b: 5, c: "bar" });
1166 }
1167
1168 #[test]
borrowed_map()1169 fn borrowed_map() {
1170 use std::collections::HashMap;
1171
1172 let headers = StringRecord::from(vec!["a", "b", "c"]);
1173 let record = StringRecord::from(vec!["aardvark", "bee", "cat"]);
1174 let got: HashMap<&str, &str> =
1175 deserialize_string_record(&record, Some(&headers)).unwrap();
1176
1177 let expected: HashMap<&str, &str> =
1178 headers.iter().zip(&record).collect();
1179 assert_eq!(got, expected);
1180 }
1181
1182 #[test]
borrowed_map_bytes()1183 fn borrowed_map_bytes() {
1184 use std::collections::HashMap;
1185
1186 let headers = ByteRecord::from(vec![b"a", b"\xFF", b"c"]);
1187 let record = ByteRecord::from(vec!["aardvark", "bee", "cat"]);
1188 let got: HashMap<&[u8], &[u8]> =
1189 deserialize_byte_record(&record, Some(&headers)).unwrap();
1190
1191 let expected: HashMap<&[u8], &[u8]> =
1192 headers.iter().zip(&record).collect();
1193 assert_eq!(got, expected);
1194 }
1195
1196 #[test]
flatten()1197 fn flatten() {
1198 #[derive(Deserialize, Debug, PartialEq)]
1199 struct Input {
1200 x: f64,
1201 y: f64,
1202 }
1203
1204 #[derive(Deserialize, Debug, PartialEq)]
1205 struct Properties {
1206 prop1: f64,
1207 prop2: f64,
1208 }
1209
1210 #[derive(Deserialize, Debug, PartialEq)]
1211 struct Row {
1212 #[serde(flatten)]
1213 input: Input,
1214 #[serde(flatten)]
1215 properties: Properties,
1216 }
1217
1218 let header = StringRecord::from(vec!["x", "y", "prop1", "prop2"]);
1219 let record = StringRecord::from(vec!["1", "2", "3", "4"]);
1220 let got: Row = record.deserialize(Some(&header)).unwrap();
1221 assert_eq!(
1222 got,
1223 Row {
1224 input: Input { x: 1.0, y: 2.0 },
1225 properties: Properties { prop1: 3.0, prop2: 4.0 },
1226 }
1227 );
1228 }
1229
1230 #[test]
partially_invalid_utf8()1231 fn partially_invalid_utf8() {
1232 #[derive(Debug, Deserialize, PartialEq)]
1233 struct Row {
1234 h1: String,
1235 h2: BString,
1236 h3: String,
1237 }
1238
1239 let headers = ByteRecord::from(vec![b"h1", b"h2", b"h3"]);
1240 let record =
1241 ByteRecord::from(vec![b(b"baz"), b(b"foo\xFFbar"), b(b"quux")]);
1242 let got: Row =
1243 deserialize_byte_record(&record, Some(&headers)).unwrap();
1244 assert_eq!(
1245 got,
1246 Row {
1247 h1: "baz".to_string(),
1248 h2: BString::from(b"foo\xFFbar".to_vec()),
1249 h3: "quux".to_string(),
1250 }
1251 );
1252 }
1253 }
1254