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