1use crate::{Error, bytes::DE_BYTES_BORROWED, error::Details, types::Value};
20use serde::{
21 Deserialize,
22 de::{self, DeserializeSeed, Deserializer as _, Visitor},
23 forward_to_deserialize_any,
24};
25use std::{
26 collections::{
27 HashMap,
28 hash_map::{Keys, Values},
29 },
30 slice::Iter,
31};
32
33pub struct Deserializer<'de> {
34 input: &'de Value,
35}
36
37struct SeqDeserializer<'de> {
38 input: Iter<'de, Value>,
39}
40
41struct MapDeserializer<'de> {
42 input_keys: Keys<'de, String, Value>,
43 input_values: Values<'de, String, Value>,
44}
45
46struct RecordDeserializer<'de> {
47 input: Iter<'de, (String, Value)>,
48 value: Option<&'de Value>,
49}
50
51pub struct EnumUnitDeserializer<'a> {
52 input: &'a str,
53}
54
55pub struct EnumDeserializer<'de> {
56 input: &'de [(String, Value)],
57}
58
59struct UnionDeserializer<'de> {
71 input: &'static str,
72 value: &'de Value,
73}
74
75impl<'de> Deserializer<'de> {
76 pub fn new(input: &'de Value) -> Self {
77 Deserializer { input }
78 }
79}
80
81impl<'de> SeqDeserializer<'de> {
82 pub fn new(input: &'de [Value]) -> Self {
83 SeqDeserializer {
84 input: input.iter(),
85 }
86 }
87}
88
89impl<'de> MapDeserializer<'de> {
90 pub fn new(input: &'de HashMap<String, Value>) -> Self {
91 MapDeserializer {
92 input_keys: input.keys(),
93 input_values: input.values(),
94 }
95 }
96}
97
98impl<'de> RecordDeserializer<'de> {
99 pub fn new(input: &'de [(String, Value)]) -> Self {
100 RecordDeserializer {
101 input: input.iter(),
102 value: None,
103 }
104 }
105}
106
107impl<'a> EnumUnitDeserializer<'a> {
108 pub fn new(input: &'a str) -> Self {
109 EnumUnitDeserializer { input }
110 }
111}
112
113impl<'de> EnumDeserializer<'de> {
114 pub fn new(input: &'de [(String, Value)]) -> Self {
115 EnumDeserializer { input }
116 }
117}
118
119impl<'de> UnionDeserializer<'de> {
120 pub fn new(input: &'static str, value: &'de Value) -> Self {
121 UnionDeserializer { input, value }
122 }
123}
124
125impl<'de> de::EnumAccess<'de> for EnumUnitDeserializer<'de> {
126 type Error = Error;
127 type Variant = Self;
128
129 fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
130 where
131 V: DeserializeSeed<'de>,
132 {
133 Ok((
134 seed.deserialize(StringDeserializer {
135 input: self.input.to_owned(),
136 })?,
137 self,
138 ))
139 }
140}
141
142impl<'de> de::VariantAccess<'de> for EnumUnitDeserializer<'de> {
143 type Error = Error;
144
145 fn unit_variant(self) -> Result<(), Error> {
146 Ok(())
147 }
148
149 fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value, Error>
150 where
151 T: DeserializeSeed<'de>,
152 {
153 Err(de::Error::custom("Unexpected Newtype variant"))
154 }
155
156 fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Error>
157 where
158 V: Visitor<'de>,
159 {
160 Err(de::Error::custom("Unexpected tuple variant"))
161 }
162
163 fn struct_variant<V>(
164 self,
165 _fields: &'static [&'static str],
166 _visitor: V,
167 ) -> Result<V::Value, Error>
168 where
169 V: Visitor<'de>,
170 {
171 Err(de::Error::custom("Unexpected struct variant"))
172 }
173}
174
175impl<'de> de::EnumAccess<'de> for EnumDeserializer<'de> {
176 type Error = Error;
177 type Variant = Self;
178
179 fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
180 where
181 V: DeserializeSeed<'de>,
182 {
183 self.input.first().map_or(
184 Err(de::Error::custom("A record must have a least one field")),
185 |item| match (item.0.as_ref(), &item.1) {
186 ("type", Value::String(x)) | ("type", Value::Enum(_, x)) => Ok((
187 seed.deserialize(StringDeserializer {
188 input: x.to_owned(),
189 })?,
190 self,
191 )),
192 (field, Value::String(_)) => Err(de::Error::custom(format!(
193 "Expected first field named 'type': got '{field}' instead"
194 ))),
195 (_, _) => Err(de::Error::custom(
196 "Expected first field of type String or Enum for the type name".to_string(),
197 )),
198 },
199 )
200 }
201}
202
203impl<'de> de::VariantAccess<'de> for EnumDeserializer<'de> {
204 type Error = Error;
205
206 fn unit_variant(self) -> Result<(), Error> {
207 Ok(())
208 }
209
210 fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Error>
211 where
212 T: DeserializeSeed<'de>,
213 {
214 self.input.get(1).map_or(
215 Err(de::Error::custom(
216 "Expected a newtype variant, got nothing instead.",
217 )),
218 |item| seed.deserialize(&Deserializer::new(&item.1)),
219 )
220 }
221
222 fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Error>
223 where
224 V: Visitor<'de>,
225 {
226 self.input.get(1).map_or(
227 Err(de::Error::custom(
228 "Expected a tuple variant, got nothing instead.",
229 )),
230 |item| de::Deserializer::deserialize_seq(&Deserializer::new(&item.1), visitor),
231 )
232 }
233
234 fn struct_variant<V>(
235 self,
236 fields: &'static [&'static str],
237 visitor: V,
238 ) -> Result<V::Value, Error>
239 where
240 V: Visitor<'de>,
241 {
242 self.input.get(1).map_or(
243 Err(de::Error::custom("Expected a struct variant, got nothing")),
244 |item| {
245 de::Deserializer::deserialize_struct(
246 &Deserializer::new(&item.1),
247 "",
248 fields,
249 visitor,
250 )
251 },
252 )
253 }
254}
255
256impl<'de> de::EnumAccess<'de> for UnionDeserializer<'de> {
257 type Error = Error;
258 type Variant = Self;
259
260 fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self), Self::Error>
262 where
263 V: DeserializeSeed<'de>,
264 {
265 Ok((
266 seed.deserialize(StringDeserializer {
267 input: String::from(self.input),
268 })?,
269 self,
270 ))
271 }
272}
273
274impl<'de> de::VariantAccess<'de> for UnionDeserializer<'de> {
275 type Error = Error;
276
277 fn unit_variant(self) -> Result<(), Self::Error> {
278 match self.value {
279 Value::Null => Ok(()),
280 _ => Err(Details::GetNull(self.value.clone()).into()),
281 }
282 }
283
284 fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
285 where
286 T: DeserializeSeed<'de>,
287 {
288 seed.deserialize(&Deserializer::new(self.value))
289 }
290
291 fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
292 where
293 V: Visitor<'de>,
294 {
295 Deserializer::new(self.value).deserialize_tuple(len, visitor)
296 }
297
298 fn struct_variant<V>(
299 self,
300 fields: &'static [&'static str],
301 visitor: V,
302 ) -> Result<V::Value, Self::Error>
303 where
304 V: Visitor<'de>,
305 {
306 let des = Deserializer::new(self.value);
307 des.deserialize_struct(self.input, fields, visitor)
308 }
309}
310
311impl<'de> de::Deserializer<'de> for &Deserializer<'de> {
312 type Error = Error;
313
314 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
315 where
316 V: Visitor<'de>,
317 {
318 match self.input {
319 Value::Null => visitor.visit_unit(),
320 &Value::Boolean(b) => visitor.visit_bool(b),
321 Value::Int(i) | Value::Date(i) | Value::TimeMillis(i) => visitor.visit_i32(*i),
322 Value::Long(i)
323 | Value::TimeMicros(i)
324 | Value::TimestampMillis(i)
325 | Value::TimestampMicros(i)
326 | Value::TimestampNanos(i)
327 | Value::LocalTimestampMillis(i)
328 | Value::LocalTimestampMicros(i)
329 | Value::LocalTimestampNanos(i) => visitor.visit_i64(*i),
330 &Value::Float(f) => visitor.visit_f32(f),
331 &Value::Double(d) => visitor.visit_f64(d),
332 Value::Union(_i, u) => match **u {
333 Value::Null => visitor.visit_unit(),
334 Value::Boolean(b) => visitor.visit_bool(b),
335 Value::Int(i) | Value::Date(i) | Value::TimeMillis(i) => visitor.visit_i32(i),
336 Value::Long(i)
337 | Value::TimeMicros(i)
338 | Value::TimestampMillis(i)
339 | Value::TimestampMicros(i)
340 | Value::TimestampNanos(i)
341 | Value::LocalTimestampMillis(i)
342 | Value::LocalTimestampMicros(i)
343 | Value::LocalTimestampNanos(i) => visitor.visit_i64(i),
344 Value::Float(f) => visitor.visit_f32(f),
345 Value::Double(d) => visitor.visit_f64(d),
346 Value::Record(ref fields) => visitor.visit_map(RecordDeserializer::new(fields)),
347 Value::Array(ref fields) => visitor.visit_seq(SeqDeserializer::new(fields)),
348 Value::String(ref s) => visitor.visit_borrowed_str(s),
349 Value::Uuid(uuid) => visitor.visit_str(&uuid.to_string()),
350 Value::Map(ref items) => visitor.visit_map(MapDeserializer::new(items)),
351 Value::Bytes(ref bytes) | Value::Fixed(_, ref bytes) => visitor.visit_bytes(bytes),
352 Value::Decimal(ref d) => visitor.visit_bytes(&d.to_vec()?),
353 Value::Enum(_, ref s) => visitor.visit_borrowed_str(s),
354 _ => Err(de::Error::custom(format!(
355 "unsupported union: {:?}",
356 self.input
357 ))),
358 },
359 Value::Record(fields) => visitor.visit_map(RecordDeserializer::new(fields)),
360 Value::Array(fields) => visitor.visit_seq(SeqDeserializer::new(fields)),
361 Value::String(s) => visitor.visit_borrowed_str(s),
362 Value::Uuid(uuid) => visitor.visit_str(&uuid.to_string()),
363 Value::Map(items) => visitor.visit_map(MapDeserializer::new(items)),
364 Value::Bytes(bytes) | Value::Fixed(_, bytes) => visitor.visit_bytes(bytes),
365 Value::Decimal(d) => visitor.visit_bytes(&d.to_vec()?),
366 Value::Enum(_, s) => visitor.visit_borrowed_str(s),
367 value => Err(de::Error::custom(format!(
368 "incorrect value of type: {:?}",
369 crate::schema::SchemaKind::from(value)
370 ))),
371 }
372 }
373
374 forward_to_deserialize_any! {
375 bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64
376 }
377
378 fn deserialize_char<V>(self, _: V) -> Result<V::Value, Self::Error>
379 where
380 V: Visitor<'de>,
381 {
382 Err(de::Error::custom("avro does not support char"))
383 }
384
385 fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
386 where
387 V: Visitor<'de>,
388 {
389 match *self.input {
390 Value::String(ref s) => visitor.visit_borrowed_str(s),
391 Value::Bytes(ref bytes) | Value::Fixed(_, ref bytes) => ::std::str::from_utf8(bytes)
392 .map_err(|e| de::Error::custom(e.to_string()))
393 .and_then(|s| visitor.visit_borrowed_str(s)),
394 Value::Uuid(ref u) => visitor.visit_str(&u.to_string()),
395 _ => Err(de::Error::custom(format!(
396 "Expected a String|Bytes|Fixed|Uuid, but got {:?}",
397 self.input
398 ))),
399 }
400 }
401
402 fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
403 where
404 V: Visitor<'de>,
405 {
406 match *self.input {
407 Value::Enum(_, ref s) | Value::String(ref s) => visitor.visit_borrowed_str(s),
408 Value::Bytes(ref bytes) | Value::Fixed(_, ref bytes) => {
409 String::from_utf8(bytes.to_owned())
410 .map_err(|e| de::Error::custom(e.to_string()))
411 .and_then(|s| visitor.visit_string(s))
412 }
413 Value::Uuid(ref u) => visitor.visit_str(&u.to_string()),
414 Value::Union(_i, ref x) => match **x {
415 Value::String(ref s) => visitor.visit_borrowed_str(s),
416 Value::Bytes(ref bytes) | Value::Fixed(_, ref bytes) => {
417 String::from_utf8(bytes.to_owned())
418 .map_err(|e| de::Error::custom(e.to_string()))
419 .and_then(|s| visitor.visit_string(s))
420 }
421 Value::Uuid(ref u) => visitor.visit_str(&u.to_string()),
422 _ => Err(de::Error::custom(format!(
423 "Expected a String|Bytes|Fixed|Uuid, but got {x:?}"
424 ))),
425 },
426 _ => Err(de::Error::custom(format!(
427 "Expected a String|Bytes|Fixed|Uuid|Union|Enum, but got {:?}",
428 self.input
429 ))),
430 }
431 }
432
433 fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
434 where
435 V: Visitor<'de>,
436 {
437 match *self.input {
438 Value::String(ref s) => visitor.visit_bytes(s.as_bytes()),
439 Value::Bytes(ref bytes) | Value::Fixed(_, ref bytes) => {
440 if DE_BYTES_BORROWED.get() {
441 visitor.visit_borrowed_bytes(bytes)
442 } else {
443 visitor.visit_bytes(bytes)
444 }
445 }
446 Value::Uuid(ref u) => visitor.visit_bytes(u.as_bytes()),
447 Value::Decimal(ref d) => visitor.visit_bytes(&d.to_vec()?),
448 _ => Err(de::Error::custom(format!(
449 "Expected a String|Bytes|Fixed|Uuid|Decimal, but got {:?}",
450 self.input
451 ))),
452 }
453 }
454
455 fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
456 where
457 V: Visitor<'de>,
458 {
459 match *self.input {
460 Value::String(ref s) => visitor.visit_byte_buf(s.clone().into_bytes()),
461 Value::Bytes(ref bytes) | Value::Fixed(_, ref bytes) => {
462 visitor.visit_byte_buf(bytes.to_owned())
463 }
464 _ => Err(de::Error::custom(format!(
465 "Expected a String|Bytes|Fixed, but got {:?}",
466 self.input
467 ))),
468 }
469 }
470
471 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
472 where
473 V: Visitor<'de>,
474 {
475 match *self.input {
476 Value::Union(_i, ref inner) if inner.as_ref() == &Value::Null => visitor.visit_none(),
477 Value::Union(_i, ref inner) => visitor.visit_some(&Deserializer::new(inner)),
478 _ => Err(de::Error::custom(format!(
479 "Expected a Union, but got {:?}",
480 self.input
481 ))),
482 }
483 }
484
485 fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
486 where
487 V: Visitor<'de>,
488 {
489 match *self.input {
490 Value::Null => visitor.visit_unit(),
491 Value::Union(_i, ref x) => match **x {
492 Value::Null => visitor.visit_unit(),
493 _ => Err(de::Error::custom(format!(
494 "Expected a Null, but got {:?}",
495 self.input
496 ))),
497 },
498 _ => Err(de::Error::custom(format!(
499 "Expected a Null|Union, but got {:?}",
500 self.input
501 ))),
502 }
503 }
504
505 fn deserialize_unit_struct<V>(
506 self,
507 _struct_name: &'static str,
508 visitor: V,
509 ) -> Result<V::Value, Self::Error>
510 where
511 V: Visitor<'de>,
512 {
513 self.deserialize_unit(visitor)
514 }
515
516 fn deserialize_newtype_struct<V>(
517 self,
518 _struct_name: &'static str,
519 visitor: V,
520 ) -> Result<V::Value, Self::Error>
521 where
522 V: Visitor<'de>,
523 {
524 visitor.visit_newtype_struct(self)
525 }
526
527 fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
528 where
529 V: Visitor<'de>,
530 {
531 match *self.input {
532 Value::Array(ref items) => visitor.visit_seq(SeqDeserializer::new(items)),
533 Value::Union(_i, ref inner) => match **inner {
534 Value::Array(ref items) => visitor.visit_seq(SeqDeserializer::new(items)),
535 Value::Null => visitor.visit_seq(SeqDeserializer::new(&[])),
536 _ => Err(de::Error::custom(format!(
537 "Expected an Array or Null, but got: {inner:?}"
538 ))),
539 },
540 _ => Err(de::Error::custom(format!(
541 "Expected an Array or Union, but got: {:?}",
542 self.input
543 ))),
544 }
545 }
546
547 fn deserialize_tuple<V>(self, _: usize, visitor: V) -> Result<V::Value, Self::Error>
548 where
549 V: Visitor<'de>,
550 {
551 self.deserialize_seq(visitor)
552 }
553
554 fn deserialize_tuple_struct<V>(
555 self,
556 _struct_name: &'static str,
557 _len: usize,
558 visitor: V,
559 ) -> Result<V::Value, Self::Error>
560 where
561 V: Visitor<'de>,
562 {
563 self.deserialize_seq(visitor)
564 }
565
566 fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
567 where
568 V: Visitor<'de>,
569 {
570 match *self.input {
571 Value::Map(ref items) => visitor.visit_map(MapDeserializer::new(items)),
572 Value::Record(ref fields) => visitor.visit_map(RecordDeserializer::new(fields)),
573 _ => Err(de::Error::custom(format_args!(
574 "Expected a record or a map. Got: {:?}",
575 &self.input
576 ))),
577 }
578 }
579
580 fn deserialize_struct<V>(
581 self,
582 _struct_name: &'static str,
583 _fields: &'static [&'static str],
584 visitor: V,
585 ) -> Result<V::Value, Self::Error>
586 where
587 V: Visitor<'de>,
588 {
589 match *self.input {
590 Value::Record(ref fields) => visitor.visit_map(RecordDeserializer::new(fields)),
591 Value::Union(_i, ref inner) => match **inner {
592 Value::Record(ref fields) => visitor.visit_map(RecordDeserializer::new(fields)),
593 Value::Null => visitor.visit_map(RecordDeserializer::new(&[])),
594 _ => Err(de::Error::custom(format!(
595 "Expected a Record or Null, got: {inner:?}"
596 ))),
597 },
598 _ => Err(de::Error::custom(format!(
599 "Expected a Record or Union, got: {:?}",
600 self.input
601 ))),
602 }
603 }
604
605 fn deserialize_enum<V>(
606 self,
607 _enum_name: &'static str,
608 variants: &'static [&'static str],
609 visitor: V,
610 ) -> Result<V::Value, Self::Error>
611 where
612 V: Visitor<'de>,
613 {
614 match *self.input {
615 Value::Record(ref fields) => visitor.visit_enum(EnumDeserializer::new(fields)),
617 Value::String(ref field) => visitor.visit_enum(EnumUnitDeserializer::new(field)),
618 Value::Union(idx, ref inner) => {
619 if (idx as usize) < variants.len() {
620 visitor.visit_enum(UnionDeserializer::new(
621 variants[idx as usize],
622 inner.as_ref(),
623 ))
624 } else {
625 Err(Details::GetUnionVariant {
626 index: idx as i64,
627 num_variants: variants.len(),
628 }
629 .into())
630 }
631 }
632 Value::Enum(_index, ref field) => visitor.visit_enum(EnumUnitDeserializer::new(field)),
634 _ => Err(de::Error::custom(format!(
635 "Expected a Record|Enum, but got {:?}",
636 self.input
637 ))),
638 }
639 }
640
641 fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
642 where
643 V: Visitor<'de>,
644 {
645 self.deserialize_str(visitor)
646 }
647
648 fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
649 where
650 V: Visitor<'de>,
651 {
652 self.deserialize_any(visitor)
653 }
654
655 fn is_human_readable(&self) -> bool {
656 crate::util::is_human_readable()
657 }
658}
659
660impl<'de> de::SeqAccess<'de> for SeqDeserializer<'de> {
661 type Error = Error;
662
663 fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
664 where
665 T: DeserializeSeed<'de>,
666 {
667 match self.input.next() {
668 Some(item) => seed.deserialize(&Deserializer::new(item)).map(Some),
669 None => Ok(None),
670 }
671 }
672}
673
674impl<'de> de::MapAccess<'de> for MapDeserializer<'de> {
675 type Error = Error;
676
677 fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
678 where
679 K: DeserializeSeed<'de>,
680 {
681 match self.input_keys.next() {
682 Some(key) => seed
683 .deserialize(StringDeserializer {
684 input: (*key).clone(),
685 })
686 .map(Some),
687 None => Ok(None),
688 }
689 }
690
691 fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
692 where
693 V: DeserializeSeed<'de>,
694 {
695 match self.input_values.next() {
696 Some(value) => seed.deserialize(&Deserializer::new(value)),
697 None => Err(de::Error::custom("should not happen - too many values")),
698 }
699 }
700}
701
702impl<'de> de::MapAccess<'de> for RecordDeserializer<'de> {
703 type Error = Error;
704
705 fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
706 where
707 K: DeserializeSeed<'de>,
708 {
709 match self.input.next() {
710 Some(item) => {
711 let (ref field, ref value) = *item;
712 self.value = Some(value);
713 seed.deserialize(StringDeserializer {
714 input: field.clone(),
715 })
716 .map(Some)
717 }
718 None => Ok(None),
719 }
720 }
721
722 fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
723 where
724 V: DeserializeSeed<'de>,
725 {
726 match self.value.take() {
727 Some(value) => seed.deserialize(&Deserializer::new(value)),
728 None => Err(de::Error::custom("should not happen - too many values")),
729 }
730 }
731}
732
733#[derive(Clone)]
734struct StringDeserializer {
735 input: String,
736}
737
738impl<'de> de::Deserializer<'de> for StringDeserializer {
739 type Error = Error;
740
741 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
742 where
743 V: Visitor<'de>,
744 {
745 visitor.visit_string(self.input)
746 }
747
748 forward_to_deserialize_any! {
749 bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
750 seq bytes byte_buf map unit_struct newtype_struct
751 tuple_struct struct tuple enum identifier ignored_any
752 }
753}
754
755pub fn from_value<'de, D: Deserialize<'de>>(value: &'de Value) -> Result<D, Error> {
760 let de = Deserializer::new(value);
761 D::deserialize(&de)
762}
763
764#[cfg(test)]
765mod tests {
766 use num_bigint::BigInt;
767 use pretty_assertions::assert_eq;
768 use serde::{Deserialize, Serialize};
769 use uuid::Uuid;
770
771 use apache_avro_test_helper::TestResult;
772
773 use super::*;
774 use crate::Decimal;
775
776 #[derive(PartialEq, Eq, Serialize, Deserialize, Debug)]
777 pub struct StringEnum {
778 pub source: String,
779 }
780
781 #[test]
782 fn avro_3955_decode_enum() -> TestResult {
783 let schema_content = r#"
784{
785 "name": "AccessLog",
786 "namespace": "com.clevercloud.accesslogs.common.avro",
787 "type": "record",
788 "fields": [
789 {
790 "name": "source",
791 "type": {
792 "type": "enum",
793 "name": "SourceType",
794 "items": "string",
795 "symbols": ["SOZU", "HAPROXY", "HAPROXY_TCP"]
796 }
797 }
798 ]
799}
800"#;
801
802 let schema = crate::Schema::parse_str(schema_content)?;
803 let data = StringEnum {
804 source: "SOZU".to_string(),
805 };
806
807 let value = crate::to_value(&data)?;
809
810 let mut buf = std::io::Cursor::new(crate::to_avro_datum(&schema, value)?);
811
812 let value = crate::from_avro_datum(&schema, &mut buf, None)?;
814
815 let decoded_data: StringEnum = crate::from_value(&value)?;
816
817 assert_eq!(decoded_data, data);
818
819 Ok(())
820 }
821
822 #[test]
823 fn avro_3955_encode_enum_data_with_wrong_content() -> TestResult {
824 let schema_content = r#"
825{
826 "name": "AccessLog",
827 "namespace": "com.clevercloud.accesslogs.common.avro",
828 "type": "record",
829 "fields": [
830 {
831 "name": "source",
832 "type": {
833 "type": "enum",
834 "name": "SourceType",
835 "items": "string",
836 "symbols": ["SOZU", "HAPROXY", "HAPROXY_TCP"]
837 }
838 }
839 ]
840}
841"#;
842
843 let schema = crate::Schema::parse_str(schema_content)?;
844 let data = StringEnum {
845 source: "WRONG_ITEM".to_string(),
846 };
847
848 let value = crate::to_value(data)?;
850
851 let encoded_data = crate::to_avro_datum(&schema, value);
853
854 assert!(encoded_data.is_err());
855
856 Ok(())
857 }
858
859 #[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
860 struct Test {
861 a: i64,
862 b: String,
863 c: Decimal,
864 }
865
866 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
867 struct TestInner {
868 a: Test,
869 b: i32,
870 }
871
872 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
873 struct TestUnitExternalEnum {
874 a: UnitExternalEnum,
875 }
876
877 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
878 enum UnitExternalEnum {
879 Val1,
880 Val2,
881 }
882
883 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
884 struct TestUnitInternalEnum {
885 a: UnitInternalEnum,
886 }
887
888 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
889 #[serde(tag = "t")]
890 enum UnitInternalEnum {
891 Val1,
892 Val2,
893 }
894
895 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
896 struct TestUnitAdjacentEnum {
897 a: UnitAdjacentEnum,
898 }
899
900 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
901 #[serde(tag = "t", content = "v")]
902 enum UnitAdjacentEnum {
903 Val1,
904 Val2,
905 }
906
907 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
908 struct TestUnitUntaggedEnum {
909 a: UnitUntaggedEnum,
910 }
911
912 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
913 #[serde(untagged)]
914 enum UnitUntaggedEnum {
915 Val1,
916 Val2,
917 }
918
919 #[derive(Debug, Serialize, Deserialize, PartialEq)]
920 struct TestSingleValueExternalEnum {
921 a: SingleValueExternalEnum,
922 }
923
924 #[derive(Debug, Serialize, Deserialize, PartialEq)]
925 enum SingleValueExternalEnum {
926 Double(f64),
927 String(String),
928 }
929
930 #[derive(Debug, Serialize, Deserialize, PartialEq)]
931 struct TestStructExternalEnum {
932 a: StructExternalEnum,
933 }
934
935 #[derive(Debug, Serialize, Deserialize, PartialEq)]
936 enum StructExternalEnum {
937 Val1 { x: f32, y: f32 },
938 Val2 { x: f32, y: f32 },
939 }
940
941 #[derive(Debug, Serialize, Deserialize, PartialEq)]
942 struct TestTupleExternalEnum {
943 a: TupleExternalEnum,
944 }
945
946 #[derive(Debug, Serialize, Deserialize, PartialEq)]
947 enum TupleExternalEnum {
948 Val1(f32, f32),
949 Val2(f32, f32, f32),
950 }
951
952 #[test]
953 fn test_from_value() -> TestResult {
954 let test = Value::Record(vec![
955 ("a".to_owned(), Value::Long(27)),
956 ("b".to_owned(), Value::String("foo".to_owned())),
957 ("c".to_owned(), Value::Decimal(Decimal::from(vec![1, 24]))),
958 ]);
959 let expected = Test {
960 a: 27,
961 b: "foo".to_owned(),
962 c: Decimal::from(vec![1, 24]),
963 };
964 let final_value: Test = from_value(&test)?;
965 assert_eq!(final_value, expected);
966
967 let test_inner = Value::Record(vec![
968 (
969 "a".to_owned(),
970 Value::Record(vec![
971 ("a".to_owned(), Value::Long(27)),
972 ("b".to_owned(), Value::String("foo".to_owned())),
973 ("c".to_owned(), Value::Decimal(Decimal::from(vec![1, 24]))),
974 ]),
975 ),
976 ("b".to_owned(), Value::Int(35)),
977 ]);
978
979 let expected_inner = TestInner { a: expected, b: 35 };
980 let final_value: TestInner = from_value(&test_inner)?;
981 assert_eq!(final_value, expected_inner);
982
983 Ok(())
984 }
985
986 #[test]
987 fn test_from_value_unit_enum() -> TestResult {
988 let expected = TestUnitExternalEnum {
989 a: UnitExternalEnum::Val1,
990 };
991
992 let test = Value::Record(vec![("a".to_owned(), Value::Enum(0, "Val1".to_owned()))]);
993 let final_value: TestUnitExternalEnum = from_value(&test)?;
994 assert_eq!(
995 final_value, expected,
996 "Error deserializing unit external enum"
997 );
998
999 let expected = TestUnitInternalEnum {
1000 a: UnitInternalEnum::Val1,
1001 };
1002
1003 let test = Value::Record(vec![(
1004 "a".to_owned(),
1005 Value::Record(vec![("t".to_owned(), Value::String("Val1".to_owned()))]),
1006 )]);
1007 let final_value: TestUnitInternalEnum = from_value(&test)?;
1008 assert_eq!(
1009 final_value, expected,
1010 "Error deserializing unit internal enum"
1011 );
1012 let expected = TestUnitAdjacentEnum {
1013 a: UnitAdjacentEnum::Val1,
1014 };
1015
1016 let test = Value::Record(vec![(
1017 "a".to_owned(),
1018 Value::Record(vec![("t".to_owned(), Value::String("Val1".to_owned()))]),
1019 )]);
1020 let final_value: TestUnitAdjacentEnum = from_value(&test)?;
1021 assert_eq!(
1022 final_value, expected,
1023 "Error deserializing unit adjacent enum"
1024 );
1025 let expected = TestUnitUntaggedEnum {
1026 a: UnitUntaggedEnum::Val1,
1027 };
1028
1029 let test = Value::Record(vec![("a".to_owned(), Value::Null)]);
1030 let final_value: TestUnitUntaggedEnum = from_value(&test)?;
1031 assert_eq!(
1032 final_value, expected,
1033 "Error deserializing unit untagged enum"
1034 );
1035 Ok(())
1036 }
1037
1038 #[test]
1039 fn avro_3645_3646_test_from_value_enum() -> TestResult {
1040 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
1041 struct TestNullExternalEnum {
1042 a: NullExternalEnum,
1043 }
1044
1045 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
1046 enum NullExternalEnum {
1047 Val1,
1048 Val2(),
1049 Val3(()),
1050 Val4(u64),
1051 }
1052
1053 let data = vec![
1054 (
1055 TestNullExternalEnum {
1056 a: NullExternalEnum::Val1,
1057 },
1058 Value::Record(vec![("a".to_owned(), Value::Enum(0, "Val1".to_owned()))]),
1059 ),
1060 (
1061 TestNullExternalEnum {
1062 a: NullExternalEnum::Val2(),
1063 },
1064 Value::Record(vec![(
1065 "a".to_owned(),
1066 Value::Record(vec![
1067 ("type".to_owned(), Value::Enum(1, "Val2".to_owned())),
1068 ("value".to_owned(), Value::Union(1, Box::new(Value::Null))),
1069 ]),
1070 )]),
1071 ),
1072 (
1073 TestNullExternalEnum {
1074 a: NullExternalEnum::Val2(),
1075 },
1076 Value::Record(vec![(
1077 "a".to_owned(),
1078 Value::Record(vec![
1079 ("type".to_owned(), Value::Enum(1, "Val2".to_owned())),
1080 ("value".to_owned(), Value::Array(vec![])),
1081 ]),
1082 )]),
1083 ),
1084 (
1085 TestNullExternalEnum {
1086 a: NullExternalEnum::Val3(()),
1087 },
1088 Value::Record(vec![(
1089 "a".to_owned(),
1090 Value::Record(vec![
1091 ("type".to_owned(), Value::Enum(2, "Val3".to_owned())),
1092 ("value".to_owned(), Value::Union(2, Box::new(Value::Null))),
1093 ]),
1094 )]),
1095 ),
1096 (
1097 TestNullExternalEnum {
1098 a: NullExternalEnum::Val4(123),
1099 },
1100 Value::Record(vec![(
1101 "a".to_owned(),
1102 Value::Record(vec![
1103 ("type".to_owned(), Value::Enum(3, "Val4".to_owned())),
1104 ("value".to_owned(), Value::Union(3, Value::Long(123).into())),
1105 ]),
1106 )]),
1107 ),
1108 ];
1109
1110 for (expected, test) in data.iter() {
1111 let actual: TestNullExternalEnum = from_value(test)?;
1112 assert_eq!(actual, *expected);
1113 }
1114
1115 Ok(())
1116 }
1117
1118 #[test]
1119 fn test_from_value_single_value_enum() -> TestResult {
1120 let expected = TestSingleValueExternalEnum {
1121 a: SingleValueExternalEnum::Double(64.0),
1122 };
1123
1124 let test = Value::Record(vec![(
1125 "a".to_owned(),
1126 Value::Record(vec![
1127 ("type".to_owned(), Value::String("Double".to_owned())),
1128 (
1129 "value".to_owned(),
1130 Value::Union(1, Box::new(Value::Double(64.0))),
1131 ),
1132 ]),
1133 )]);
1134 let final_value: TestSingleValueExternalEnum = from_value(&test)?;
1135 assert_eq!(
1136 final_value, expected,
1137 "Error deserializing single value external enum(union)"
1138 );
1139
1140 Ok(())
1141 }
1142
1143 #[test]
1144 fn test_from_value_struct_enum() -> TestResult {
1145 let expected = TestStructExternalEnum {
1146 a: StructExternalEnum::Val1 { x: 1.0, y: 2.0 },
1147 };
1148
1149 let test = Value::Record(vec![(
1150 "a".to_owned(),
1151 Value::Record(vec![
1152 ("type".to_owned(), Value::String("Val1".to_owned())),
1153 (
1154 "value".to_owned(),
1155 Value::Union(
1156 0,
1157 Box::new(Value::Record(vec![
1158 ("x".to_owned(), Value::Float(1.0)),
1159 ("y".to_owned(), Value::Float(2.0)),
1160 ])),
1161 ),
1162 ),
1163 ]),
1164 )]);
1165 let final_value: TestStructExternalEnum = from_value(&test)?;
1166 assert_eq!(
1167 final_value, expected,
1168 "error deserializing struct external enum(union)"
1169 );
1170
1171 Ok(())
1172 }
1173
1174 #[test]
1175 fn test_avro_3692_from_value_struct_flatten() -> TestResult {
1176 #[derive(Deserialize, PartialEq, Debug)]
1177 struct S1 {
1178 f1: String,
1179 #[serde(flatten)]
1180 inner: S2,
1181 }
1182 #[derive(Deserialize, PartialEq, Debug)]
1183 struct S2 {
1184 f2: String,
1185 }
1186 let expected = S1 {
1187 f1: "Hello".to_owned(),
1188 inner: S2 {
1189 f2: "World".to_owned(),
1190 },
1191 };
1192
1193 let test = Value::Record(vec![
1194 ("f1".to_owned(), "Hello".into()),
1195 ("f2".to_owned(), "World".into()),
1196 ]);
1197 let final_value: S1 = from_value(&test)?;
1198 assert_eq!(final_value, expected);
1199
1200 Ok(())
1201 }
1202
1203 #[test]
1204 fn test_from_value_tuple_enum() -> TestResult {
1205 let expected = TestTupleExternalEnum {
1206 a: TupleExternalEnum::Val1(1.0, 2.0),
1207 };
1208
1209 let test = Value::Record(vec![(
1210 "a".to_owned(),
1211 Value::Record(vec![
1212 ("type".to_owned(), Value::String("Val1".to_owned())),
1213 (
1214 "value".to_owned(),
1215 Value::Union(
1216 0,
1217 Box::new(Value::Array(vec![Value::Float(1.0), Value::Float(2.0)])),
1218 ),
1219 ),
1220 ]),
1221 )]);
1222 let final_value: TestTupleExternalEnum = from_value(&test)?;
1223 assert_eq!(
1224 final_value, expected,
1225 "error serializing tuple external enum(union)"
1226 );
1227
1228 Ok(())
1229 }
1230
1231 #[test]
1232 fn test_date() -> TestResult {
1233 let raw_value = 1;
1234 let value = Value::Date(raw_value);
1235 let result = crate::from_value::<i32>(&value)?;
1236 assert_eq!(result, raw_value);
1237 Ok(())
1238 }
1239
1240 #[test]
1241 fn test_time_millis() -> TestResult {
1242 let raw_value = 1;
1243 let value = Value::TimeMillis(raw_value);
1244 let result = crate::from_value::<i32>(&value)?;
1245 assert_eq!(result, raw_value);
1246 Ok(())
1247 }
1248
1249 #[test]
1250 fn test_time_micros() -> TestResult {
1251 let raw_value = 1;
1252 let value = Value::TimeMicros(raw_value);
1253 let result = crate::from_value::<i64>(&value)?;
1254 assert_eq!(result, raw_value);
1255 Ok(())
1256 }
1257
1258 #[test]
1259 fn test_timestamp_millis() -> TestResult {
1260 let raw_value = 1;
1261 let value = Value::TimestampMillis(raw_value);
1262 let result = crate::from_value::<i64>(&value)?;
1263 assert_eq!(result, raw_value);
1264 Ok(())
1265 }
1266
1267 #[test]
1268 fn test_timestamp_micros() -> TestResult {
1269 let raw_value = 1;
1270 let value = Value::TimestampMicros(raw_value);
1271 let result = from_value::<i64>(&value)?;
1272 assert_eq!(result, raw_value);
1273 Ok(())
1274 }
1275
1276 #[test]
1277 fn test_avro_3916_timestamp_nanos() -> TestResult {
1278 let raw_value = 1;
1279 let value = Value::TimestampNanos(raw_value);
1280 let result = from_value::<i64>(&value)?;
1281 assert_eq!(result, raw_value);
1282 Ok(())
1283 }
1284
1285 #[test]
1286 fn test_avro_3853_local_timestamp_millis() -> TestResult {
1287 let raw_value = 1;
1288 let value = Value::LocalTimestampMillis(raw_value);
1289 let result = from_value::<i64>(&value)?;
1290 assert_eq!(result, raw_value);
1291 Ok(())
1292 }
1293
1294 #[test]
1295 fn test_avro_3853_local_timestamp_micros() -> TestResult {
1296 let raw_value = 1;
1297 let value = Value::LocalTimestampMicros(raw_value);
1298 let result = crate::from_value::<i64>(&value)?;
1299 assert_eq!(result, raw_value);
1300 Ok(())
1301 }
1302
1303 #[test]
1304 fn test_avro_3916_local_timestamp_nanos() -> TestResult {
1305 let raw_value = 1;
1306 let value = Value::LocalTimestampNanos(raw_value);
1307 let result = crate::from_value::<i64>(&value)?;
1308 assert_eq!(result, raw_value);
1309 Ok(())
1310 }
1311
1312 #[test]
1313 fn test_from_value_uuid_str() -> TestResult {
1314 let raw_value = "9ec535ff-3e2a-45bd-91d3-0a01321b5a49";
1315 let value = Value::Uuid(Uuid::parse_str(raw_value)?);
1316 let result = from_value::<Uuid>(&value)?;
1317 assert_eq!(result.to_string(), raw_value);
1318 Ok(())
1319 }
1320
1321 #[test]
1322 fn test_from_value_uuid_slice() -> TestResult {
1323 let raw_value = &[4, 54, 67, 12, 43, 2, 2, 76, 32, 50, 87, 5, 1, 33, 43, 87];
1324 let value = Value::Uuid(Uuid::from_slice(raw_value)?);
1325 let result = crate::from_value::<Uuid>(&value)?;
1326 assert_eq!(result.as_bytes(), raw_value);
1327 Ok(())
1328 }
1329
1330 #[test]
1331 fn test_from_value_with_union() -> TestResult {
1332 #[derive(Debug, Deserialize, PartialEq, Eq)]
1336 struct RecordInUnion {
1337 record_in_union: i32,
1338 }
1339
1340 #[derive(Debug, Deserialize, PartialEq, Eq)]
1341 enum EnumInStruct {
1342 Val1,
1343 }
1344
1345 #[derive(Debug, Deserialize, PartialEq, Eq)]
1346 struct StructWithMissingFields {
1347 a_string: String,
1348 a_record: Option<RecordInUnion>,
1349 an_array: Option<[bool; 2]>,
1350 a_union_map: Option<HashMap<String, i64>>,
1351 an_enum: EnumInStruct,
1352 }
1353
1354 let raw_map: HashMap<String, i64> = [
1355 ("long_one".to_string(), 1),
1356 ("long_two".to_string(), 2),
1357 ("long_three".to_string(), 3),
1358 ("time_micros_a".to_string(), 123),
1359 ("timestamp_millis_b".to_string(), 234),
1360 ("timestamp_micros_c".to_string(), 345),
1361 ("timestamp_nanos_d".to_string(), 345_001),
1362 ("local_timestamp_millis_d".to_string(), 678),
1363 ("local_timestamp_micros_e".to_string(), 789),
1364 ("local_timestamp_nanos_f".to_string(), 345_002),
1365 ]
1366 .iter()
1367 .cloned()
1368 .collect();
1369
1370 let value_map = raw_map
1371 .iter()
1372 .map(|(k, v)| match k {
1373 key if key.starts_with("long_") => (k.clone(), Value::Long(*v)),
1374 key if key.starts_with("time_micros_") => (k.clone(), Value::TimeMicros(*v)),
1375 key if key.starts_with("timestamp_millis_") => {
1376 (k.clone(), Value::TimestampMillis(*v))
1377 }
1378 key if key.starts_with("timestamp_micros_") => {
1379 (k.clone(), Value::TimestampMicros(*v))
1380 }
1381 key if key.starts_with("timestamp_nanos_") => {
1382 (k.clone(), Value::TimestampNanos(*v))
1383 }
1384 key if key.starts_with("local_timestamp_millis_") => {
1385 (k.clone(), Value::LocalTimestampMillis(*v))
1386 }
1387 key if key.starts_with("local_timestamp_micros_") => {
1388 (k.clone(), Value::LocalTimestampMicros(*v))
1389 }
1390 key if key.starts_with("local_timestamp_nanos_") => {
1391 (k.clone(), Value::LocalTimestampNanos(*v))
1392 }
1393 _ => unreachable!("unexpected key: {:?}", k),
1394 })
1395 .collect();
1396
1397 let record = Value::Record(vec![
1398 (
1399 "a_string".to_string(),
1400 Value::String("a valid message field".to_string()),
1401 ),
1402 (
1403 "a_non_existing_string".to_string(),
1404 Value::String("a string".to_string()),
1405 ),
1406 (
1407 "a_union_string".to_string(),
1408 Value::Union(0, Box::new(Value::String("a union string".to_string()))),
1409 ),
1410 (
1411 "a_union_long".to_string(),
1412 Value::Union(0, Box::new(Value::Long(412))),
1413 ),
1414 (
1415 "a_union_long".to_string(),
1416 Value::Union(0, Box::new(Value::Long(412))),
1417 ),
1418 (
1419 "a_time_micros".to_string(),
1420 Value::Union(0, Box::new(Value::TimeMicros(123))),
1421 ),
1422 (
1423 "a_non_existing_time_micros".to_string(),
1424 Value::Union(0, Box::new(Value::TimeMicros(-123))),
1425 ),
1426 (
1427 "a_timestamp_millis".to_string(),
1428 Value::Union(0, Box::new(Value::TimestampMillis(234))),
1429 ),
1430 (
1431 "a_non_existing_timestamp_millis".to_string(),
1432 Value::Union(0, Box::new(Value::TimestampMillis(-234))),
1433 ),
1434 (
1435 "a_timestamp_micros".to_string(),
1436 Value::Union(0, Box::new(Value::TimestampMicros(345))),
1437 ),
1438 (
1439 "a_non_existing_timestamp_micros".to_string(),
1440 Value::Union(0, Box::new(Value::TimestampMicros(-345))),
1441 ),
1442 (
1443 "a_timestamp_nanos".to_string(),
1444 Value::Union(0, Box::new(Value::TimestampNanos(345))),
1445 ),
1446 (
1447 "a_non_existing_timestamp_nanos".to_string(),
1448 Value::Union(0, Box::new(Value::TimestampNanos(-345))),
1449 ),
1450 (
1451 "a_local_timestamp_millis".to_string(),
1452 Value::Union(0, Box::new(Value::LocalTimestampMillis(678))),
1453 ),
1454 (
1455 "a_non_existing_local_timestamp_millis".to_string(),
1456 Value::Union(0, Box::new(Value::LocalTimestampMillis(-678))),
1457 ),
1458 (
1459 "a_local_timestamp_micros".to_string(),
1460 Value::Union(0, Box::new(Value::LocalTimestampMicros(789))),
1461 ),
1462 (
1463 "a_non_existing_local_timestamp_micros".to_string(),
1464 Value::Union(0, Box::new(Value::LocalTimestampMicros(-789))),
1465 ),
1466 (
1467 "a_local_timestamp_nanos".to_string(),
1468 Value::Union(0, Box::new(Value::LocalTimestampNanos(789))),
1469 ),
1470 (
1471 "a_non_existing_local_timestamp_nanos".to_string(),
1472 Value::Union(0, Box::new(Value::LocalTimestampNanos(-789))),
1473 ),
1474 (
1475 "a_record".to_string(),
1476 Value::Union(
1477 0,
1478 Box::new(Value::Record(vec![(
1479 "record_in_union".to_string(),
1480 Value::Int(-2),
1481 )])),
1482 ),
1483 ),
1484 (
1485 "a_non_existing_record".to_string(),
1486 Value::Union(
1487 0,
1488 Box::new(Value::Record(vec![("blah".to_string(), Value::Int(-22))])),
1489 ),
1490 ),
1491 (
1492 "an_array".to_string(),
1493 Value::Union(
1494 0,
1495 Box::new(Value::Array(vec![
1496 Value::Boolean(true),
1497 Value::Boolean(false),
1498 ])),
1499 ),
1500 ),
1501 (
1502 "a_non_existing_array".to_string(),
1503 Value::Union(
1504 0,
1505 Box::new(Value::Array(vec![
1506 Value::Boolean(false),
1507 Value::Boolean(true),
1508 ])),
1509 ),
1510 ),
1511 (
1512 "a_union_map".to_string(),
1513 Value::Union(0, Box::new(Value::Map(value_map))),
1514 ),
1515 (
1516 "a_non_existing_union_map".to_string(),
1517 Value::Union(0, Box::new(Value::Map(HashMap::new()))),
1518 ),
1519 ("an_enum".to_string(), Value::Enum(0, "Val1".to_owned())),
1520 (
1521 "a_non_existing_enum".to_string(),
1522 Value::Enum(0, "AnotherVariant".to_owned()),
1523 ),
1524 ]);
1525
1526 let deserialized: StructWithMissingFields = crate::from_value(&record)?;
1527 let reference = StructWithMissingFields {
1528 a_string: "a valid message field".to_string(),
1529 a_record: Some(RecordInUnion {
1530 record_in_union: -2,
1531 }),
1532 an_array: Some([true, false]),
1533 a_union_map: Some(raw_map),
1534 an_enum: EnumInStruct::Val1,
1535 };
1536 assert_eq!(deserialized, reference);
1537 Ok(())
1538 }
1539
1540 #[test]
1541 fn avro_3747_human_readable_false() -> TestResult {
1542 use serde::de::Deserializer as SerdeDeserializer;
1543
1544 assert!(!crate::util::is_human_readable());
1545
1546 let deser = &Deserializer::new(&Value::Null);
1547
1548 assert!(!deser.is_human_readable());
1549
1550 Ok(())
1551 }
1552
1553 #[test]
1554 fn test_avro_3892_deserialize_string_from_bytes() -> TestResult {
1555 let raw_value = vec![1, 2, 3, 4];
1556 let value = Value::Bytes(raw_value.clone());
1557 let result = from_value::<String>(&value)?;
1558 assert_eq!(result, String::from_utf8(raw_value)?);
1559 Ok(())
1560 }
1561
1562 #[test]
1563 fn test_avro_3892_deserialize_str_from_bytes() -> TestResult {
1564 let raw_value = &[1, 2, 3, 4];
1565 let value = Value::Bytes(raw_value.to_vec());
1566 let result = from_value::<&str>(&value)?;
1567 assert_eq!(result, std::str::from_utf8(raw_value)?);
1568 Ok(())
1569 }
1570
1571 #[derive(Debug)]
1572 struct Bytes(Vec<u8>);
1573
1574 impl<'de> Deserialize<'de> for Bytes {
1575 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1576 where
1577 D: serde::Deserializer<'de>,
1578 {
1579 struct BytesVisitor;
1580 impl Visitor<'_> for BytesVisitor {
1581 type Value = Bytes;
1582
1583 fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
1584 formatter.write_str("a byte array")
1585 }
1586
1587 fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
1588 where
1589 E: serde::de::Error,
1590 {
1591 Ok(Bytes(v.to_vec()))
1592 }
1593 }
1594 deserializer.deserialize_bytes(BytesVisitor)
1595 }
1596 }
1597
1598 #[test]
1599 fn test_avro_3892_deserialize_bytes_from_decimal() -> TestResult {
1600 let expected_bytes = BigInt::from(123456789).to_signed_bytes_be();
1601 let value = Value::Decimal(Decimal::from(&expected_bytes));
1602 let raw_bytes = from_value::<Bytes>(&value)?;
1603 assert_eq!(raw_bytes.0, expected_bytes);
1604
1605 let value = Value::Union(0, Box::new(Value::Decimal(Decimal::from(&expected_bytes))));
1606 let raw_bytes = from_value::<Option<Bytes>>(&value)?;
1607 assert_eq!(raw_bytes.unwrap().0, expected_bytes);
1608 Ok(())
1609 }
1610
1611 #[test]
1612 fn test_avro_3892_deserialize_bytes_from_uuid() -> TestResult {
1613 let uuid_str = "10101010-2020-2020-2020-101010101010";
1614 let expected_bytes = Uuid::parse_str(uuid_str)?.as_bytes().to_vec();
1615 let value = Value::Uuid(Uuid::parse_str(uuid_str)?);
1616 let raw_bytes = from_value::<Bytes>(&value)?;
1617 assert_eq!(raw_bytes.0, expected_bytes);
1618
1619 let value = Value::Union(0, Box::new(Value::Uuid(Uuid::parse_str(uuid_str)?)));
1620 let raw_bytes = from_value::<Option<Bytes>>(&value)?;
1621 assert_eq!(raw_bytes.unwrap().0, expected_bytes);
1622 Ok(())
1623 }
1624
1625 #[test]
1626 fn test_avro_3892_deserialize_bytes_from_fixed() -> TestResult {
1627 let expected_bytes = vec![1, 2, 3, 4];
1628 let value = Value::Fixed(4, expected_bytes.clone());
1629 let raw_bytes = from_value::<Bytes>(&value)?;
1630 assert_eq!(raw_bytes.0, expected_bytes);
1631
1632 let value = Value::Union(0, Box::new(Value::Fixed(4, expected_bytes.clone())));
1633 let raw_bytes = from_value::<Option<Bytes>>(&value)?;
1634 assert_eq!(raw_bytes.unwrap().0, expected_bytes);
1635 Ok(())
1636 }
1637
1638 #[test]
1639 fn test_avro_3892_deserialize_bytes_from_bytes() -> TestResult {
1640 let expected_bytes = vec![1, 2, 3, 4];
1641 let value = Value::Bytes(expected_bytes.clone());
1642 let raw_bytes = from_value::<Bytes>(&value)?;
1643 assert_eq!(raw_bytes.0, expected_bytes);
1644
1645 let value = Value::Union(0, Box::new(Value::Bytes(expected_bytes.clone())));
1646 let raw_bytes = from_value::<Option<Bytes>>(&value)?;
1647 assert_eq!(raw_bytes.unwrap().0, expected_bytes);
1648 Ok(())
1649 }
1650}