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 serial_test::serial;
770 use std::sync::atomic::Ordering;
771 use uuid::Uuid;
772
773 use apache_avro_test_helper::TestResult;
774
775 use crate::Decimal;
776
777 use super::*;
778
779 #[derive(PartialEq, Eq, Serialize, Deserialize, Debug)]
780 pub struct StringEnum {
781 pub source: String,
782 }
783
784 #[test]
785 fn avro_3955_decode_enum() -> TestResult {
786 let schema_content = r#"
787{
788 "name": "AccessLog",
789 "namespace": "com.clevercloud.accesslogs.common.avro",
790 "type": "record",
791 "fields": [
792 {
793 "name": "source",
794 "type": {
795 "type": "enum",
796 "name": "SourceType",
797 "items": "string",
798 "symbols": ["SOZU", "HAPROXY", "HAPROXY_TCP"]
799 }
800 }
801 ]
802}
803"#;
804
805 let schema = crate::Schema::parse_str(schema_content)?;
806 let data = StringEnum {
807 source: "SOZU".to_string(),
808 };
809
810 let value = crate::to_value(&data)?;
812
813 let mut buf = std::io::Cursor::new(crate::to_avro_datum(&schema, value)?);
814
815 let value = crate::from_avro_datum(&schema, &mut buf, None)?;
817
818 let decoded_data: StringEnum = crate::from_value(&value)?;
819
820 assert_eq!(decoded_data, data);
821
822 Ok(())
823 }
824
825 #[test]
826 fn avro_3955_encode_enum_data_with_wrong_content() -> TestResult {
827 let schema_content = r#"
828{
829 "name": "AccessLog",
830 "namespace": "com.clevercloud.accesslogs.common.avro",
831 "type": "record",
832 "fields": [
833 {
834 "name": "source",
835 "type": {
836 "type": "enum",
837 "name": "SourceType",
838 "items": "string",
839 "symbols": ["SOZU", "HAPROXY", "HAPROXY_TCP"]
840 }
841 }
842 ]
843}
844"#;
845
846 let schema = crate::Schema::parse_str(schema_content)?;
847 let data = StringEnum {
848 source: "WRONG_ITEM".to_string(),
849 };
850
851 let value = crate::to_value(data)?;
853
854 let encoded_data = crate::to_avro_datum(&schema, value);
856
857 assert!(encoded_data.is_err());
858
859 Ok(())
860 }
861
862 #[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
863 struct Test {
864 a: i64,
865 b: String,
866 c: Decimal,
867 }
868
869 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
870 struct TestInner {
871 a: Test,
872 b: i32,
873 }
874
875 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
876 struct TestUnitExternalEnum {
877 a: UnitExternalEnum,
878 }
879
880 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
881 enum UnitExternalEnum {
882 Val1,
883 Val2,
884 }
885
886 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
887 struct TestUnitInternalEnum {
888 a: UnitInternalEnum,
889 }
890
891 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
892 #[serde(tag = "t")]
893 enum UnitInternalEnum {
894 Val1,
895 Val2,
896 }
897
898 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
899 struct TestUnitAdjacentEnum {
900 a: UnitAdjacentEnum,
901 }
902
903 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
904 #[serde(tag = "t", content = "v")]
905 enum UnitAdjacentEnum {
906 Val1,
907 Val2,
908 }
909
910 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
911 struct TestUnitUntaggedEnum {
912 a: UnitUntaggedEnum,
913 }
914
915 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
916 #[serde(untagged)]
917 enum UnitUntaggedEnum {
918 Val1,
919 Val2,
920 }
921
922 #[derive(Debug, Serialize, Deserialize, PartialEq)]
923 struct TestSingleValueExternalEnum {
924 a: SingleValueExternalEnum,
925 }
926
927 #[derive(Debug, Serialize, Deserialize, PartialEq)]
928 enum SingleValueExternalEnum {
929 Double(f64),
930 String(String),
931 }
932
933 #[derive(Debug, Serialize, Deserialize, PartialEq)]
934 struct TestStructExternalEnum {
935 a: StructExternalEnum,
936 }
937
938 #[derive(Debug, Serialize, Deserialize, PartialEq)]
939 enum StructExternalEnum {
940 Val1 { x: f32, y: f32 },
941 Val2 { x: f32, y: f32 },
942 }
943
944 #[derive(Debug, Serialize, Deserialize, PartialEq)]
945 struct TestTupleExternalEnum {
946 a: TupleExternalEnum,
947 }
948
949 #[derive(Debug, Serialize, Deserialize, PartialEq)]
950 enum TupleExternalEnum {
951 Val1(f32, f32),
952 Val2(f32, f32, f32),
953 }
954
955 #[test]
956 fn test_from_value() -> TestResult {
957 let test = Value::Record(vec![
958 ("a".to_owned(), Value::Long(27)),
959 ("b".to_owned(), Value::String("foo".to_owned())),
960 ("c".to_owned(), Value::Decimal(Decimal::from(vec![1, 24]))),
961 ]);
962 let expected = Test {
963 a: 27,
964 b: "foo".to_owned(),
965 c: Decimal::from(vec![1, 24]),
966 };
967 let final_value: Test = from_value(&test)?;
968 assert_eq!(final_value, expected);
969
970 let test_inner = Value::Record(vec![
971 (
972 "a".to_owned(),
973 Value::Record(vec![
974 ("a".to_owned(), Value::Long(27)),
975 ("b".to_owned(), Value::String("foo".to_owned())),
976 ("c".to_owned(), Value::Decimal(Decimal::from(vec![1, 24]))),
977 ]),
978 ),
979 ("b".to_owned(), Value::Int(35)),
980 ]);
981
982 let expected_inner = TestInner { a: expected, b: 35 };
983 let final_value: TestInner = from_value(&test_inner)?;
984 assert_eq!(final_value, expected_inner);
985
986 Ok(())
987 }
988
989 #[test]
990 fn test_from_value_unit_enum() -> TestResult {
991 let expected = TestUnitExternalEnum {
992 a: UnitExternalEnum::Val1,
993 };
994
995 let test = Value::Record(vec![("a".to_owned(), Value::Enum(0, "Val1".to_owned()))]);
996 let final_value: TestUnitExternalEnum = from_value(&test)?;
997 assert_eq!(
998 final_value, expected,
999 "Error deserializing unit external enum"
1000 );
1001
1002 let expected = TestUnitInternalEnum {
1003 a: UnitInternalEnum::Val1,
1004 };
1005
1006 let test = Value::Record(vec![(
1007 "a".to_owned(),
1008 Value::Record(vec![("t".to_owned(), Value::String("Val1".to_owned()))]),
1009 )]);
1010 let final_value: TestUnitInternalEnum = from_value(&test)?;
1011 assert_eq!(
1012 final_value, expected,
1013 "Error deserializing unit internal enum"
1014 );
1015 let expected = TestUnitAdjacentEnum {
1016 a: UnitAdjacentEnum::Val1,
1017 };
1018
1019 let test = Value::Record(vec![(
1020 "a".to_owned(),
1021 Value::Record(vec![("t".to_owned(), Value::String("Val1".to_owned()))]),
1022 )]);
1023 let final_value: TestUnitAdjacentEnum = from_value(&test)?;
1024 assert_eq!(
1025 final_value, expected,
1026 "Error deserializing unit adjacent enum"
1027 );
1028 let expected = TestUnitUntaggedEnum {
1029 a: UnitUntaggedEnum::Val1,
1030 };
1031
1032 let test = Value::Record(vec![("a".to_owned(), Value::Null)]);
1033 let final_value: TestUnitUntaggedEnum = from_value(&test)?;
1034 assert_eq!(
1035 final_value, expected,
1036 "Error deserializing unit untagged enum"
1037 );
1038 Ok(())
1039 }
1040
1041 #[test]
1042 fn avro_3645_3646_test_from_value_enum() -> TestResult {
1043 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
1044 struct TestNullExternalEnum {
1045 a: NullExternalEnum,
1046 }
1047
1048 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
1049 enum NullExternalEnum {
1050 Val1,
1051 Val2(),
1052 Val3(()),
1053 Val4(u64),
1054 }
1055
1056 let data = vec![
1057 (
1058 TestNullExternalEnum {
1059 a: NullExternalEnum::Val1,
1060 },
1061 Value::Record(vec![("a".to_owned(), Value::Enum(0, "Val1".to_owned()))]),
1062 ),
1063 (
1064 TestNullExternalEnum {
1065 a: NullExternalEnum::Val2(),
1066 },
1067 Value::Record(vec![(
1068 "a".to_owned(),
1069 Value::Record(vec![
1070 ("type".to_owned(), Value::Enum(1, "Val2".to_owned())),
1071 ("value".to_owned(), Value::Union(1, Box::new(Value::Null))),
1072 ]),
1073 )]),
1074 ),
1075 (
1076 TestNullExternalEnum {
1077 a: NullExternalEnum::Val2(),
1078 },
1079 Value::Record(vec![(
1080 "a".to_owned(),
1081 Value::Record(vec![
1082 ("type".to_owned(), Value::Enum(1, "Val2".to_owned())),
1083 ("value".to_owned(), Value::Array(vec![])),
1084 ]),
1085 )]),
1086 ),
1087 (
1088 TestNullExternalEnum {
1089 a: NullExternalEnum::Val3(()),
1090 },
1091 Value::Record(vec![(
1092 "a".to_owned(),
1093 Value::Record(vec![
1094 ("type".to_owned(), Value::Enum(2, "Val3".to_owned())),
1095 ("value".to_owned(), Value::Union(2, Box::new(Value::Null))),
1096 ]),
1097 )]),
1098 ),
1099 (
1100 TestNullExternalEnum {
1101 a: NullExternalEnum::Val4(123),
1102 },
1103 Value::Record(vec![(
1104 "a".to_owned(),
1105 Value::Record(vec![
1106 ("type".to_owned(), Value::Enum(3, "Val4".to_owned())),
1107 ("value".to_owned(), Value::Union(3, Value::Long(123).into())),
1108 ]),
1109 )]),
1110 ),
1111 ];
1112
1113 for (expected, test) in data.iter() {
1114 let actual: TestNullExternalEnum = from_value(test)?;
1115 assert_eq!(actual, *expected);
1116 }
1117
1118 Ok(())
1119 }
1120
1121 #[test]
1122 fn test_from_value_single_value_enum() -> TestResult {
1123 let expected = TestSingleValueExternalEnum {
1124 a: SingleValueExternalEnum::Double(64.0),
1125 };
1126
1127 let test = Value::Record(vec![(
1128 "a".to_owned(),
1129 Value::Record(vec![
1130 ("type".to_owned(), Value::String("Double".to_owned())),
1131 (
1132 "value".to_owned(),
1133 Value::Union(1, Box::new(Value::Double(64.0))),
1134 ),
1135 ]),
1136 )]);
1137 let final_value: TestSingleValueExternalEnum = from_value(&test)?;
1138 assert_eq!(
1139 final_value, expected,
1140 "Error deserializing single value external enum(union)"
1141 );
1142
1143 Ok(())
1144 }
1145
1146 #[test]
1147 fn test_from_value_struct_enum() -> TestResult {
1148 let expected = TestStructExternalEnum {
1149 a: StructExternalEnum::Val1 { x: 1.0, y: 2.0 },
1150 };
1151
1152 let test = Value::Record(vec![(
1153 "a".to_owned(),
1154 Value::Record(vec![
1155 ("type".to_owned(), Value::String("Val1".to_owned())),
1156 (
1157 "value".to_owned(),
1158 Value::Union(
1159 0,
1160 Box::new(Value::Record(vec![
1161 ("x".to_owned(), Value::Float(1.0)),
1162 ("y".to_owned(), Value::Float(2.0)),
1163 ])),
1164 ),
1165 ),
1166 ]),
1167 )]);
1168 let final_value: TestStructExternalEnum = from_value(&test)?;
1169 assert_eq!(
1170 final_value, expected,
1171 "error deserializing struct external enum(union)"
1172 );
1173
1174 Ok(())
1175 }
1176
1177 #[test]
1178 fn test_avro_3692_from_value_struct_flatten() -> TestResult {
1179 #[derive(Deserialize, PartialEq, Debug)]
1180 struct S1 {
1181 f1: String,
1182 #[serde(flatten)]
1183 inner: S2,
1184 }
1185 #[derive(Deserialize, PartialEq, Debug)]
1186 struct S2 {
1187 f2: String,
1188 }
1189 let expected = S1 {
1190 f1: "Hello".to_owned(),
1191 inner: S2 {
1192 f2: "World".to_owned(),
1193 },
1194 };
1195
1196 let test = Value::Record(vec![
1197 ("f1".to_owned(), "Hello".into()),
1198 ("f2".to_owned(), "World".into()),
1199 ]);
1200 let final_value: S1 = from_value(&test)?;
1201 assert_eq!(final_value, expected);
1202
1203 Ok(())
1204 }
1205
1206 #[test]
1207 fn test_from_value_tuple_enum() -> TestResult {
1208 let expected = TestTupleExternalEnum {
1209 a: TupleExternalEnum::Val1(1.0, 2.0),
1210 };
1211
1212 let test = Value::Record(vec![(
1213 "a".to_owned(),
1214 Value::Record(vec![
1215 ("type".to_owned(), Value::String("Val1".to_owned())),
1216 (
1217 "value".to_owned(),
1218 Value::Union(
1219 0,
1220 Box::new(Value::Array(vec![Value::Float(1.0), Value::Float(2.0)])),
1221 ),
1222 ),
1223 ]),
1224 )]);
1225 let final_value: TestTupleExternalEnum = from_value(&test)?;
1226 assert_eq!(
1227 final_value, expected,
1228 "error serializing tuple external enum(union)"
1229 );
1230
1231 Ok(())
1232 }
1233
1234 #[test]
1235 fn test_date() -> TestResult {
1236 let raw_value = 1;
1237 let value = Value::Date(raw_value);
1238 let result = crate::from_value::<i32>(&value)?;
1239 assert_eq!(result, raw_value);
1240 Ok(())
1241 }
1242
1243 #[test]
1244 fn test_time_millis() -> TestResult {
1245 let raw_value = 1;
1246 let value = Value::TimeMillis(raw_value);
1247 let result = crate::from_value::<i32>(&value)?;
1248 assert_eq!(result, raw_value);
1249 Ok(())
1250 }
1251
1252 #[test]
1253 fn test_time_micros() -> TestResult {
1254 let raw_value = 1;
1255 let value = Value::TimeMicros(raw_value);
1256 let result = crate::from_value::<i64>(&value)?;
1257 assert_eq!(result, raw_value);
1258 Ok(())
1259 }
1260
1261 #[test]
1262 fn test_timestamp_millis() -> TestResult {
1263 let raw_value = 1;
1264 let value = Value::TimestampMillis(raw_value);
1265 let result = crate::from_value::<i64>(&value)?;
1266 assert_eq!(result, raw_value);
1267 Ok(())
1268 }
1269
1270 #[test]
1271 fn test_timestamp_micros() -> TestResult {
1272 let raw_value = 1;
1273 let value = Value::TimestampMicros(raw_value);
1274 let result = from_value::<i64>(&value)?;
1275 assert_eq!(result, raw_value);
1276 Ok(())
1277 }
1278
1279 #[test]
1280 fn test_avro_3916_timestamp_nanos() -> TestResult {
1281 let raw_value = 1;
1282 let value = Value::TimestampNanos(raw_value);
1283 let result = from_value::<i64>(&value)?;
1284 assert_eq!(result, raw_value);
1285 Ok(())
1286 }
1287
1288 #[test]
1289 fn test_avro_3853_local_timestamp_millis() -> TestResult {
1290 let raw_value = 1;
1291 let value = Value::LocalTimestampMillis(raw_value);
1292 let result = from_value::<i64>(&value)?;
1293 assert_eq!(result, raw_value);
1294 Ok(())
1295 }
1296
1297 #[test]
1298 fn test_avro_3853_local_timestamp_micros() -> TestResult {
1299 let raw_value = 1;
1300 let value = Value::LocalTimestampMicros(raw_value);
1301 let result = crate::from_value::<i64>(&value)?;
1302 assert_eq!(result, raw_value);
1303 Ok(())
1304 }
1305
1306 #[test]
1307 fn test_avro_3916_local_timestamp_nanos() -> TestResult {
1308 let raw_value = 1;
1309 let value = Value::LocalTimestampNanos(raw_value);
1310 let result = crate::from_value::<i64>(&value)?;
1311 assert_eq!(result, raw_value);
1312 Ok(())
1313 }
1314
1315 #[test]
1316 fn test_from_value_uuid_str() -> TestResult {
1317 let raw_value = "9ec535ff-3e2a-45bd-91d3-0a01321b5a49";
1318 let value = Value::Uuid(Uuid::parse_str(raw_value)?);
1319 let result = from_value::<Uuid>(&value)?;
1320 assert_eq!(result.to_string(), raw_value);
1321 Ok(())
1322 }
1323
1324 #[test]
1325 fn test_from_value_uuid_slice() -> TestResult {
1326 let raw_value = &[4, 54, 67, 12, 43, 2, 2, 76, 32, 50, 87, 5, 1, 33, 43, 87];
1327 let value = Value::Uuid(Uuid::from_slice(raw_value)?);
1328 let result = crate::from_value::<Uuid>(&value)?;
1329 assert_eq!(result.as_bytes(), raw_value);
1330 Ok(())
1331 }
1332
1333 #[test]
1334 fn test_from_value_with_union() -> TestResult {
1335 #[derive(Debug, Deserialize, PartialEq, Eq)]
1339 struct RecordInUnion {
1340 record_in_union: i32,
1341 }
1342
1343 #[derive(Debug, Deserialize, PartialEq, Eq)]
1344 enum EnumInStruct {
1345 Val1,
1346 }
1347
1348 #[derive(Debug, Deserialize, PartialEq, Eq)]
1349 struct StructWithMissingFields {
1350 a_string: String,
1351 a_record: Option<RecordInUnion>,
1352 an_array: Option<[bool; 2]>,
1353 a_union_map: Option<HashMap<String, i64>>,
1354 an_enum: EnumInStruct,
1355 }
1356
1357 let raw_map: HashMap<String, i64> = [
1358 ("long_one".to_string(), 1),
1359 ("long_two".to_string(), 2),
1360 ("long_three".to_string(), 3),
1361 ("time_micros_a".to_string(), 123),
1362 ("timestamp_millis_b".to_string(), 234),
1363 ("timestamp_micros_c".to_string(), 345),
1364 ("timestamp_nanos_d".to_string(), 345_001),
1365 ("local_timestamp_millis_d".to_string(), 678),
1366 ("local_timestamp_micros_e".to_string(), 789),
1367 ("local_timestamp_nanos_f".to_string(), 345_002),
1368 ]
1369 .iter()
1370 .cloned()
1371 .collect();
1372
1373 let value_map = raw_map
1374 .iter()
1375 .map(|(k, v)| match k {
1376 key if key.starts_with("long_") => (k.clone(), Value::Long(*v)),
1377 key if key.starts_with("time_micros_") => (k.clone(), Value::TimeMicros(*v)),
1378 key if key.starts_with("timestamp_millis_") => {
1379 (k.clone(), Value::TimestampMillis(*v))
1380 }
1381 key if key.starts_with("timestamp_micros_") => {
1382 (k.clone(), Value::TimestampMicros(*v))
1383 }
1384 key if key.starts_with("timestamp_nanos_") => {
1385 (k.clone(), Value::TimestampNanos(*v))
1386 }
1387 key if key.starts_with("local_timestamp_millis_") => {
1388 (k.clone(), Value::LocalTimestampMillis(*v))
1389 }
1390 key if key.starts_with("local_timestamp_micros_") => {
1391 (k.clone(), Value::LocalTimestampMicros(*v))
1392 }
1393 key if key.starts_with("local_timestamp_nanos_") => {
1394 (k.clone(), Value::LocalTimestampNanos(*v))
1395 }
1396 _ => unreachable!("unexpected key: {:?}", k),
1397 })
1398 .collect();
1399
1400 let record = Value::Record(vec![
1401 (
1402 "a_string".to_string(),
1403 Value::String("a valid message field".to_string()),
1404 ),
1405 (
1406 "a_non_existing_string".to_string(),
1407 Value::String("a string".to_string()),
1408 ),
1409 (
1410 "a_union_string".to_string(),
1411 Value::Union(0, Box::new(Value::String("a union string".to_string()))),
1412 ),
1413 (
1414 "a_union_long".to_string(),
1415 Value::Union(0, Box::new(Value::Long(412))),
1416 ),
1417 (
1418 "a_union_long".to_string(),
1419 Value::Union(0, Box::new(Value::Long(412))),
1420 ),
1421 (
1422 "a_time_micros".to_string(),
1423 Value::Union(0, Box::new(Value::TimeMicros(123))),
1424 ),
1425 (
1426 "a_non_existing_time_micros".to_string(),
1427 Value::Union(0, Box::new(Value::TimeMicros(-123))),
1428 ),
1429 (
1430 "a_timestamp_millis".to_string(),
1431 Value::Union(0, Box::new(Value::TimestampMillis(234))),
1432 ),
1433 (
1434 "a_non_existing_timestamp_millis".to_string(),
1435 Value::Union(0, Box::new(Value::TimestampMillis(-234))),
1436 ),
1437 (
1438 "a_timestamp_micros".to_string(),
1439 Value::Union(0, Box::new(Value::TimestampMicros(345))),
1440 ),
1441 (
1442 "a_non_existing_timestamp_micros".to_string(),
1443 Value::Union(0, Box::new(Value::TimestampMicros(-345))),
1444 ),
1445 (
1446 "a_timestamp_nanos".to_string(),
1447 Value::Union(0, Box::new(Value::TimestampNanos(345))),
1448 ),
1449 (
1450 "a_non_existing_timestamp_nanos".to_string(),
1451 Value::Union(0, Box::new(Value::TimestampNanos(-345))),
1452 ),
1453 (
1454 "a_local_timestamp_millis".to_string(),
1455 Value::Union(0, Box::new(Value::LocalTimestampMillis(678))),
1456 ),
1457 (
1458 "a_non_existing_local_timestamp_millis".to_string(),
1459 Value::Union(0, Box::new(Value::LocalTimestampMillis(-678))),
1460 ),
1461 (
1462 "a_local_timestamp_micros".to_string(),
1463 Value::Union(0, Box::new(Value::LocalTimestampMicros(789))),
1464 ),
1465 (
1466 "a_non_existing_local_timestamp_micros".to_string(),
1467 Value::Union(0, Box::new(Value::LocalTimestampMicros(-789))),
1468 ),
1469 (
1470 "a_local_timestamp_nanos".to_string(),
1471 Value::Union(0, Box::new(Value::LocalTimestampNanos(789))),
1472 ),
1473 (
1474 "a_non_existing_local_timestamp_nanos".to_string(),
1475 Value::Union(0, Box::new(Value::LocalTimestampNanos(-789))),
1476 ),
1477 (
1478 "a_record".to_string(),
1479 Value::Union(
1480 0,
1481 Box::new(Value::Record(vec![(
1482 "record_in_union".to_string(),
1483 Value::Int(-2),
1484 )])),
1485 ),
1486 ),
1487 (
1488 "a_non_existing_record".to_string(),
1489 Value::Union(
1490 0,
1491 Box::new(Value::Record(vec![("blah".to_string(), Value::Int(-22))])),
1492 ),
1493 ),
1494 (
1495 "an_array".to_string(),
1496 Value::Union(
1497 0,
1498 Box::new(Value::Array(vec![
1499 Value::Boolean(true),
1500 Value::Boolean(false),
1501 ])),
1502 ),
1503 ),
1504 (
1505 "a_non_existing_array".to_string(),
1506 Value::Union(
1507 0,
1508 Box::new(Value::Array(vec![
1509 Value::Boolean(false),
1510 Value::Boolean(true),
1511 ])),
1512 ),
1513 ),
1514 (
1515 "a_union_map".to_string(),
1516 Value::Union(0, Box::new(Value::Map(value_map))),
1517 ),
1518 (
1519 "a_non_existing_union_map".to_string(),
1520 Value::Union(0, Box::new(Value::Map(HashMap::new()))),
1521 ),
1522 ("an_enum".to_string(), Value::Enum(0, "Val1".to_owned())),
1523 (
1524 "a_non_existing_enum".to_string(),
1525 Value::Enum(0, "AnotherVariant".to_owned()),
1526 ),
1527 ]);
1528
1529 let deserialized: StructWithMissingFields = crate::from_value(&record)?;
1530 let reference = StructWithMissingFields {
1531 a_string: "a valid message field".to_string(),
1532 a_record: Some(RecordInUnion {
1533 record_in_union: -2,
1534 }),
1535 an_array: Some([true, false]),
1536 a_union_map: Some(raw_map),
1537 an_enum: EnumInStruct::Val1,
1538 };
1539 assert_eq!(deserialized, reference);
1540 Ok(())
1541 }
1542
1543 #[test]
1544 #[serial(serde_is_human_readable)]
1545 fn avro_3747_human_readable_false() -> TestResult {
1546 use serde::de::Deserializer as SerdeDeserializer;
1547
1548 let is_human_readable = false;
1549 crate::util::SERDE_HUMAN_READABLE.store(is_human_readable, Ordering::Release);
1550
1551 let deser = &Deserializer::new(&Value::Null);
1552
1553 assert_eq!(deser.is_human_readable(), is_human_readable);
1554
1555 Ok(())
1556 }
1557
1558 #[test]
1559 #[serial(serde_is_human_readable)]
1560 fn avro_3747_human_readable_true() -> TestResult {
1561 use serde::de::Deserializer as SerdeDeserializer;
1562
1563 crate::util::SERDE_HUMAN_READABLE.store(true, Ordering::Release);
1564
1565 let deser = &Deserializer::new(&Value::Null);
1566
1567 assert!(deser.is_human_readable());
1568
1569 Ok(())
1570 }
1571
1572 #[test]
1573 fn test_avro_3892_deserialize_string_from_bytes() -> TestResult {
1574 let raw_value = vec![1, 2, 3, 4];
1575 let value = Value::Bytes(raw_value.clone());
1576 let result = from_value::<String>(&value)?;
1577 assert_eq!(result, String::from_utf8(raw_value)?);
1578 Ok(())
1579 }
1580
1581 #[test]
1582 fn test_avro_3892_deserialize_str_from_bytes() -> TestResult {
1583 let raw_value = &[1, 2, 3, 4];
1584 let value = Value::Bytes(raw_value.to_vec());
1585 let result = from_value::<&str>(&value)?;
1586 assert_eq!(result, std::str::from_utf8(raw_value)?);
1587 Ok(())
1588 }
1589
1590 #[derive(Debug)]
1591 struct Bytes(Vec<u8>);
1592
1593 impl<'de> Deserialize<'de> for Bytes {
1594 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1595 where
1596 D: serde::Deserializer<'de>,
1597 {
1598 struct BytesVisitor;
1599 impl Visitor<'_> for BytesVisitor {
1600 type Value = Bytes;
1601
1602 fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
1603 formatter.write_str("a byte array")
1604 }
1605
1606 fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
1607 where
1608 E: serde::de::Error,
1609 {
1610 Ok(Bytes(v.to_vec()))
1611 }
1612 }
1613 deserializer.deserialize_bytes(BytesVisitor)
1614 }
1615 }
1616
1617 #[test]
1618 fn test_avro_3892_deserialize_bytes_from_decimal() -> TestResult {
1619 let expected_bytes = BigInt::from(123456789).to_signed_bytes_be();
1620 let value = Value::Decimal(Decimal::from(&expected_bytes));
1621 let raw_bytes = from_value::<Bytes>(&value)?;
1622 assert_eq!(raw_bytes.0, expected_bytes);
1623
1624 let value = Value::Union(0, Box::new(Value::Decimal(Decimal::from(&expected_bytes))));
1625 let raw_bytes = from_value::<Option<Bytes>>(&value)?;
1626 assert_eq!(raw_bytes.unwrap().0, expected_bytes);
1627 Ok(())
1628 }
1629
1630 #[test]
1631 fn test_avro_3892_deserialize_bytes_from_uuid() -> TestResult {
1632 let uuid_str = "10101010-2020-2020-2020-101010101010";
1633 let expected_bytes = Uuid::parse_str(uuid_str)?.as_bytes().to_vec();
1634 let value = Value::Uuid(Uuid::parse_str(uuid_str)?);
1635 let raw_bytes = from_value::<Bytes>(&value)?;
1636 assert_eq!(raw_bytes.0, expected_bytes);
1637
1638 let value = Value::Union(0, Box::new(Value::Uuid(Uuid::parse_str(uuid_str)?)));
1639 let raw_bytes = from_value::<Option<Bytes>>(&value)?;
1640 assert_eq!(raw_bytes.unwrap().0, expected_bytes);
1641 Ok(())
1642 }
1643
1644 #[test]
1645 fn test_avro_3892_deserialize_bytes_from_fixed() -> TestResult {
1646 let expected_bytes = vec![1, 2, 3, 4];
1647 let value = Value::Fixed(4, expected_bytes.clone());
1648 let raw_bytes = from_value::<Bytes>(&value)?;
1649 assert_eq!(raw_bytes.0, expected_bytes);
1650
1651 let value = Value::Union(0, Box::new(Value::Fixed(4, expected_bytes.clone())));
1652 let raw_bytes = from_value::<Option<Bytes>>(&value)?;
1653 assert_eq!(raw_bytes.unwrap().0, expected_bytes);
1654 Ok(())
1655 }
1656
1657 #[test]
1658 fn test_avro_3892_deserialize_bytes_from_bytes() -> TestResult {
1659 let expected_bytes = vec![1, 2, 3, 4];
1660 let value = Value::Bytes(expected_bytes.clone());
1661 let raw_bytes = from_value::<Bytes>(&value)?;
1662 assert_eq!(raw_bytes.0, expected_bytes);
1663
1664 let value = Value::Union(0, Box::new(Value::Bytes(expected_bytes.clone())));
1665 let raw_bytes = from_value::<Option<Bytes>>(&value)?;
1666 assert_eq!(raw_bytes.unwrap().0, expected_bytes);
1667 Ok(())
1668 }
1669}