apache_avro/
de.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Logic for serde-compatible deserialization.
19use 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
59/// A `serde::de::EnumAccess` and `serde::de::VariantAccess` implementation for deserializing
60/// union types.  A `UnionDeserializer` is returned when deserializing into an enum, and the value
61/// being deserialized is an Avro union.  The enum type being deserialized into should match the
62/// structure of the union type of the value being deserialized, (i.e. matching number of variants
63/// and schemas of variants.)
64///
65/// The `input` field is the name of the variant.  Since Avro union variants don't have names, this
66/// should come from one of the variant names passed into the `serde::de::Deserializer::deserialize_enum`
67/// function call that returned this `UnionDeserializer`
68///
69/// `value` the value of the variant, deserialized into a [`crate::types::Value`].
70struct 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    /// Deserialize the variant name
261    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                Value::BigDecimal(ref big_decimal) => {
355                    visitor.visit_str(big_decimal.to_plain_string().as_str())
356                }
357                _ => Err(de::Error::custom(format!(
358                    "unsupported union: {:?}",
359                    self.input
360                ))),
361            },
362            Value::Record(fields) => visitor.visit_map(RecordDeserializer::new(fields)),
363            Value::Array(fields) => visitor.visit_seq(SeqDeserializer::new(fields)),
364            Value::String(s) => visitor.visit_borrowed_str(s),
365            Value::Uuid(uuid) => visitor.visit_str(&uuid.to_string()),
366            Value::Map(items) => visitor.visit_map(MapDeserializer::new(items)),
367            Value::Bytes(bytes) | Value::Fixed(_, bytes) => visitor.visit_bytes(bytes),
368            Value::Decimal(d) => visitor.visit_bytes(&d.to_vec()?),
369            Value::Enum(_, s) => visitor.visit_borrowed_str(s),
370            Value::BigDecimal(big_decimal) => {
371                visitor.visit_str(big_decimal.to_plain_string().as_str())
372            }
373            value => Err(de::Error::custom(format!(
374                "incorrect value of type: {:?}",
375                crate::schema::SchemaKind::from(value)
376            ))),
377        }
378    }
379
380    forward_to_deserialize_any! {
381        bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64
382    }
383
384    fn deserialize_char<V>(self, _: V) -> Result<V::Value, Self::Error>
385    where
386        V: Visitor<'de>,
387    {
388        Err(de::Error::custom("avro does not support char"))
389    }
390
391    fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
392    where
393        V: Visitor<'de>,
394    {
395        match *self.input {
396            Value::String(ref s) => visitor.visit_borrowed_str(s),
397            Value::Bytes(ref bytes) | Value::Fixed(_, ref bytes) => ::std::str::from_utf8(bytes)
398                .map_err(|e| de::Error::custom(e.to_string()))
399                .and_then(|s| visitor.visit_borrowed_str(s)),
400            Value::Uuid(ref u) => visitor.visit_str(&u.to_string()),
401            _ => Err(de::Error::custom(format!(
402                "Expected a String|Bytes|Fixed|Uuid, but got {:?}",
403                self.input
404            ))),
405        }
406    }
407
408    fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
409    where
410        V: Visitor<'de>,
411    {
412        match *self.input {
413            Value::Enum(_, ref s) | Value::String(ref s) => visitor.visit_borrowed_str(s),
414            Value::Bytes(ref bytes) | Value::Fixed(_, ref bytes) => {
415                String::from_utf8(bytes.to_owned())
416                    .map_err(|e| de::Error::custom(e.to_string()))
417                    .and_then(|s| visitor.visit_string(s))
418            }
419            Value::Uuid(ref u) => visitor.visit_str(&u.to_string()),
420            Value::Union(_i, ref x) => match **x {
421                Value::String(ref s) => visitor.visit_borrowed_str(s),
422                Value::Bytes(ref bytes) | Value::Fixed(_, ref bytes) => {
423                    String::from_utf8(bytes.to_owned())
424                        .map_err(|e| de::Error::custom(e.to_string()))
425                        .and_then(|s| visitor.visit_string(s))
426                }
427                Value::Uuid(ref u) => visitor.visit_str(&u.to_string()),
428                _ => Err(de::Error::custom(format!(
429                    "Expected a String|Bytes|Fixed|Uuid, but got {x:?}"
430                ))),
431            },
432            _ => Err(de::Error::custom(format!(
433                "Expected a String|Bytes|Fixed|Uuid|Union|Enum, but got {:?}",
434                self.input
435            ))),
436        }
437    }
438
439    fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
440    where
441        V: Visitor<'de>,
442    {
443        match *self.input {
444            Value::String(ref s) => visitor.visit_bytes(s.as_bytes()),
445            Value::Bytes(ref bytes) | Value::Fixed(_, ref bytes) => {
446                if DE_BYTES_BORROWED.get() {
447                    visitor.visit_borrowed_bytes(bytes)
448                } else {
449                    visitor.visit_bytes(bytes)
450                }
451            }
452            Value::Uuid(ref u) => visitor.visit_bytes(u.as_bytes()),
453            Value::Decimal(ref d) => visitor.visit_bytes(&d.to_vec()?),
454            _ => Err(de::Error::custom(format!(
455                "Expected a String|Bytes|Fixed|Uuid|Decimal, but got {:?}",
456                self.input
457            ))),
458        }
459    }
460
461    fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
462    where
463        V: Visitor<'de>,
464    {
465        match *self.input {
466            Value::String(ref s) => visitor.visit_byte_buf(s.clone().into_bytes()),
467            Value::Bytes(ref bytes) | Value::Fixed(_, ref bytes) => {
468                visitor.visit_byte_buf(bytes.to_owned())
469            }
470            _ => Err(de::Error::custom(format!(
471                "Expected a String|Bytes|Fixed, but got {:?}",
472                self.input
473            ))),
474        }
475    }
476
477    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
478    where
479        V: Visitor<'de>,
480    {
481        match *self.input {
482            Value::Union(_i, ref inner) if inner.as_ref() == &Value::Null => visitor.visit_none(),
483            Value::Union(_i, ref inner) => visitor.visit_some(&Deserializer::new(inner)),
484            _ => Err(de::Error::custom(format!(
485                "Expected a Union, but got {:?}",
486                self.input
487            ))),
488        }
489    }
490
491    fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
492    where
493        V: Visitor<'de>,
494    {
495        match *self.input {
496            Value::Null => visitor.visit_unit(),
497            Value::Union(_i, ref x) => match **x {
498                Value::Null => visitor.visit_unit(),
499                _ => Err(de::Error::custom(format!(
500                    "Expected a Null, but got {:?}",
501                    self.input
502                ))),
503            },
504            _ => Err(de::Error::custom(format!(
505                "Expected a Null|Union, but got {:?}",
506                self.input
507            ))),
508        }
509    }
510
511    fn deserialize_unit_struct<V>(
512        self,
513        _struct_name: &'static str,
514        visitor: V,
515    ) -> Result<V::Value, Self::Error>
516    where
517        V: Visitor<'de>,
518    {
519        self.deserialize_unit(visitor)
520    }
521
522    fn deserialize_newtype_struct<V>(
523        self,
524        _struct_name: &'static str,
525        visitor: V,
526    ) -> Result<V::Value, Self::Error>
527    where
528        V: Visitor<'de>,
529    {
530        visitor.visit_newtype_struct(self)
531    }
532
533    fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
534    where
535        V: Visitor<'de>,
536    {
537        match *self.input {
538            Value::Array(ref items) => visitor.visit_seq(SeqDeserializer::new(items)),
539            Value::Union(_i, ref inner) => match **inner {
540                Value::Array(ref items) => visitor.visit_seq(SeqDeserializer::new(items)),
541                Value::Null => visitor.visit_seq(SeqDeserializer::new(&[])),
542                _ => Err(de::Error::custom(format!(
543                    "Expected an Array or Null, but got: {inner:?}"
544                ))),
545            },
546            _ => Err(de::Error::custom(format!(
547                "Expected an Array or Union, but got: {:?}",
548                self.input
549            ))),
550        }
551    }
552
553    fn deserialize_tuple<V>(self, _: usize, visitor: V) -> Result<V::Value, Self::Error>
554    where
555        V: Visitor<'de>,
556    {
557        self.deserialize_seq(visitor)
558    }
559
560    fn deserialize_tuple_struct<V>(
561        self,
562        _struct_name: &'static str,
563        _len: usize,
564        visitor: V,
565    ) -> Result<V::Value, Self::Error>
566    where
567        V: Visitor<'de>,
568    {
569        self.deserialize_seq(visitor)
570    }
571
572    fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
573    where
574        V: Visitor<'de>,
575    {
576        match *self.input {
577            Value::Map(ref items) => visitor.visit_map(MapDeserializer::new(items)),
578            Value::Record(ref fields) => visitor.visit_map(RecordDeserializer::new(fields)),
579            _ => Err(de::Error::custom(format_args!(
580                "Expected a record or a map. Got: {:?}",
581                &self.input
582            ))),
583        }
584    }
585
586    fn deserialize_struct<V>(
587        self,
588        _struct_name: &'static str,
589        _fields: &'static [&'static str],
590        visitor: V,
591    ) -> Result<V::Value, Self::Error>
592    where
593        V: Visitor<'de>,
594    {
595        match *self.input {
596            Value::Record(ref fields) => visitor.visit_map(RecordDeserializer::new(fields)),
597            Value::Union(_i, ref inner) => match **inner {
598                Value::Record(ref fields) => visitor.visit_map(RecordDeserializer::new(fields)),
599                Value::Null => visitor.visit_map(RecordDeserializer::new(&[])),
600                _ => Err(de::Error::custom(format!(
601                    "Expected a Record or Null, got: {inner:?}"
602                ))),
603            },
604            _ => Err(de::Error::custom(format!(
605                "Expected a Record or Union, got: {:?}",
606                self.input
607            ))),
608        }
609    }
610
611    fn deserialize_enum<V>(
612        self,
613        _enum_name: &'static str,
614        variants: &'static [&'static str],
615        visitor: V,
616    ) -> Result<V::Value, Self::Error>
617    where
618        V: Visitor<'de>,
619    {
620        match *self.input {
621            // This branch can be anything...
622            Value::Record(ref fields) => visitor.visit_enum(EnumDeserializer::new(fields)),
623            Value::String(ref field) => visitor.visit_enum(EnumUnitDeserializer::new(field)),
624            Value::Union(idx, ref inner) => {
625                if (idx as usize) < variants.len() {
626                    visitor.visit_enum(UnionDeserializer::new(
627                        variants[idx as usize],
628                        inner.as_ref(),
629                    ))
630                } else {
631                    Err(Details::GetUnionVariant {
632                        index: idx as i64,
633                        num_variants: variants.len(),
634                    }
635                    .into())
636                }
637            }
638            // This has to be a unit Enum
639            Value::Enum(_index, ref field) => visitor.visit_enum(EnumUnitDeserializer::new(field)),
640            _ => Err(de::Error::custom(format!(
641                "Expected a Record|Enum, but got {:?}",
642                self.input
643            ))),
644        }
645    }
646
647    fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
648    where
649        V: Visitor<'de>,
650    {
651        self.deserialize_str(visitor)
652    }
653
654    fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
655    where
656        V: Visitor<'de>,
657    {
658        self.deserialize_any(visitor)
659    }
660
661    fn is_human_readable(&self) -> bool {
662        crate::util::is_human_readable()
663    }
664}
665
666impl<'de> de::SeqAccess<'de> for SeqDeserializer<'de> {
667    type Error = Error;
668
669    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
670    where
671        T: DeserializeSeed<'de>,
672    {
673        match self.input.next() {
674            Some(item) => seed.deserialize(&Deserializer::new(item)).map(Some),
675            None => Ok(None),
676        }
677    }
678}
679
680impl<'de> de::MapAccess<'de> for MapDeserializer<'de> {
681    type Error = Error;
682
683    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
684    where
685        K: DeserializeSeed<'de>,
686    {
687        match self.input_keys.next() {
688            Some(key) => seed
689                .deserialize(StringDeserializer {
690                    input: (*key).clone(),
691                })
692                .map(Some),
693            None => Ok(None),
694        }
695    }
696
697    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
698    where
699        V: DeserializeSeed<'de>,
700    {
701        match self.input_values.next() {
702            Some(value) => seed.deserialize(&Deserializer::new(value)),
703            None => Err(de::Error::custom("should not happen - too many values")),
704        }
705    }
706}
707
708impl<'de> de::MapAccess<'de> for RecordDeserializer<'de> {
709    type Error = Error;
710
711    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
712    where
713        K: DeserializeSeed<'de>,
714    {
715        match self.input.next() {
716            Some(item) => {
717                let (ref field, ref value) = *item;
718                self.value = Some(value);
719                seed.deserialize(StringDeserializer {
720                    input: field.clone(),
721                })
722                .map(Some)
723            }
724            None => Ok(None),
725        }
726    }
727
728    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
729    where
730        V: DeserializeSeed<'de>,
731    {
732        match self.value.take() {
733            Some(value) => seed.deserialize(&Deserializer::new(value)),
734            None => Err(de::Error::custom("should not happen - too many values")),
735        }
736    }
737}
738
739#[derive(Clone)]
740struct StringDeserializer {
741    input: String,
742}
743
744impl<'de> de::Deserializer<'de> for StringDeserializer {
745    type Error = Error;
746
747    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
748    where
749        V: Visitor<'de>,
750    {
751        visitor.visit_string(self.input)
752    }
753
754    forward_to_deserialize_any! {
755        bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
756        seq bytes byte_buf map unit_struct newtype_struct
757        tuple_struct struct tuple enum identifier ignored_any
758    }
759}
760
761/// Interpret a `Value` as an instance of type `D`.
762///
763/// This conversion can fail if the structure of the `Value` does not match the
764/// structure expected by `D`.
765pub fn from_value<'de, D: Deserialize<'de>>(value: &'de Value) -> Result<D, Error> {
766    let de = Deserializer::new(value);
767    D::deserialize(&de)
768}
769
770#[cfg(test)]
771mod tests {
772    use num_bigint::BigInt;
773    use pretty_assertions::assert_eq;
774    use serde::{Deserialize, Serialize};
775    use uuid::Uuid;
776
777    use apache_avro_test_helper::TestResult;
778
779    use super::*;
780    use crate::Decimal;
781
782    #[derive(PartialEq, Eq, Serialize, Deserialize, Debug)]
783    pub struct StringEnum {
784        pub source: String,
785    }
786
787    #[test]
788    fn avro_3955_decode_enum() -> TestResult {
789        let schema_content = r#"
790{
791  "name": "AccessLog",
792  "namespace": "com.clevercloud.accesslogs.common.avro",
793  "type": "record",
794  "fields": [
795    {
796      "name": "source",
797      "type": {
798        "type": "enum",
799        "name": "SourceType",
800        "items": "string",
801        "symbols": ["SOZU", "HAPROXY", "HAPROXY_TCP"]
802      }
803    }
804  ]
805}
806"#;
807
808        let schema = crate::Schema::parse_str(schema_content)?;
809        let data = StringEnum {
810            source: "SOZU".to_string(),
811        };
812
813        // encode into avro
814        let value = crate::to_value(&data)?;
815
816        let mut buf = std::io::Cursor::new(crate::to_avro_datum(&schema, value)?);
817
818        // decode from avro
819        let value = crate::from_avro_datum(&schema, &mut buf, None)?;
820
821        let decoded_data: StringEnum = crate::from_value(&value)?;
822
823        assert_eq!(decoded_data, data);
824
825        Ok(())
826    }
827
828    #[test]
829    fn avro_3955_encode_enum_data_with_wrong_content() -> TestResult {
830        let schema_content = r#"
831{
832  "name": "AccessLog",
833  "namespace": "com.clevercloud.accesslogs.common.avro",
834  "type": "record",
835  "fields": [
836    {
837      "name": "source",
838      "type": {
839        "type": "enum",
840        "name": "SourceType",
841        "items": "string",
842        "symbols": ["SOZU", "HAPROXY", "HAPROXY_TCP"]
843      }
844    }
845  ]
846}
847"#;
848
849        let schema = crate::Schema::parse_str(schema_content)?;
850        let data = StringEnum {
851            source: "WRONG_ITEM".to_string(),
852        };
853
854        // encode into avro
855        let value = crate::to_value(data)?;
856
857        // The following sentence have to fail has the data is wrong.
858        let encoded_data = crate::to_avro_datum(&schema, value);
859
860        assert!(encoded_data.is_err());
861
862        Ok(())
863    }
864
865    #[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
866    struct Test {
867        a: i64,
868        b: String,
869        c: Decimal,
870    }
871
872    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
873    struct TestInner {
874        a: Test,
875        b: i32,
876    }
877
878    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
879    struct TestUnitExternalEnum {
880        a: UnitExternalEnum,
881    }
882
883    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
884    enum UnitExternalEnum {
885        Val1,
886        Val2,
887    }
888
889    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
890    struct TestUnitInternalEnum {
891        a: UnitInternalEnum,
892    }
893
894    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
895    #[serde(tag = "t")]
896    enum UnitInternalEnum {
897        Val1,
898        Val2,
899    }
900
901    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
902    struct TestUnitAdjacentEnum {
903        a: UnitAdjacentEnum,
904    }
905
906    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
907    #[serde(tag = "t", content = "v")]
908    enum UnitAdjacentEnum {
909        Val1,
910        Val2,
911    }
912
913    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
914    struct TestUnitUntaggedEnum {
915        a: UnitUntaggedEnum,
916    }
917
918    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
919    #[serde(untagged)]
920    enum UnitUntaggedEnum {
921        Val1,
922        Val2,
923    }
924
925    #[derive(Debug, Serialize, Deserialize, PartialEq)]
926    struct TestSingleValueExternalEnum {
927        a: SingleValueExternalEnum,
928    }
929
930    #[derive(Debug, Serialize, Deserialize, PartialEq)]
931    enum SingleValueExternalEnum {
932        Double(f64),
933        String(String),
934    }
935
936    #[derive(Debug, Serialize, Deserialize, PartialEq)]
937    struct TestStructExternalEnum {
938        a: StructExternalEnum,
939    }
940
941    #[derive(Debug, Serialize, Deserialize, PartialEq)]
942    enum StructExternalEnum {
943        Val1 { x: f32, y: f32 },
944        Val2 { x: f32, y: f32 },
945    }
946
947    #[derive(Debug, Serialize, Deserialize, PartialEq)]
948    struct TestTupleExternalEnum {
949        a: TupleExternalEnum,
950    }
951
952    #[derive(Debug, Serialize, Deserialize, PartialEq)]
953    enum TupleExternalEnum {
954        Val1(f32, f32),
955        Val2(f32, f32, f32),
956    }
957
958    #[test]
959    fn test_from_value() -> TestResult {
960        let test = Value::Record(vec![
961            ("a".to_owned(), Value::Long(27)),
962            ("b".to_owned(), Value::String("foo".to_owned())),
963            ("c".to_owned(), Value::Decimal(Decimal::from(vec![1, 24]))),
964        ]);
965        let expected = Test {
966            a: 27,
967            b: "foo".to_owned(),
968            c: Decimal::from(vec![1, 24]),
969        };
970        let final_value: Test = from_value(&test)?;
971        assert_eq!(final_value, expected);
972
973        let test_inner = Value::Record(vec![
974            (
975                "a".to_owned(),
976                Value::Record(vec![
977                    ("a".to_owned(), Value::Long(27)),
978                    ("b".to_owned(), Value::String("foo".to_owned())),
979                    ("c".to_owned(), Value::Decimal(Decimal::from(vec![1, 24]))),
980                ]),
981            ),
982            ("b".to_owned(), Value::Int(35)),
983        ]);
984
985        let expected_inner = TestInner { a: expected, b: 35 };
986        let final_value: TestInner = from_value(&test_inner)?;
987        assert_eq!(final_value, expected_inner);
988
989        Ok(())
990    }
991
992    #[test]
993    fn test_from_value_unit_enum() -> TestResult {
994        let expected = TestUnitExternalEnum {
995            a: UnitExternalEnum::Val1,
996        };
997
998        let test = Value::Record(vec![("a".to_owned(), Value::Enum(0, "Val1".to_owned()))]);
999        let final_value: TestUnitExternalEnum = from_value(&test)?;
1000        assert_eq!(
1001            final_value, expected,
1002            "Error deserializing unit external enum"
1003        );
1004
1005        let expected = TestUnitInternalEnum {
1006            a: UnitInternalEnum::Val1,
1007        };
1008
1009        let test = Value::Record(vec![(
1010            "a".to_owned(),
1011            Value::Record(vec![("t".to_owned(), Value::String("Val1".to_owned()))]),
1012        )]);
1013        let final_value: TestUnitInternalEnum = from_value(&test)?;
1014        assert_eq!(
1015            final_value, expected,
1016            "Error deserializing unit internal enum"
1017        );
1018        let expected = TestUnitAdjacentEnum {
1019            a: UnitAdjacentEnum::Val1,
1020        };
1021
1022        let test = Value::Record(vec![(
1023            "a".to_owned(),
1024            Value::Record(vec![("t".to_owned(), Value::String("Val1".to_owned()))]),
1025        )]);
1026        let final_value: TestUnitAdjacentEnum = from_value(&test)?;
1027        assert_eq!(
1028            final_value, expected,
1029            "Error deserializing unit adjacent enum"
1030        );
1031        let expected = TestUnitUntaggedEnum {
1032            a: UnitUntaggedEnum::Val1,
1033        };
1034
1035        let test = Value::Record(vec![("a".to_owned(), Value::Null)]);
1036        let final_value: TestUnitUntaggedEnum = from_value(&test)?;
1037        assert_eq!(
1038            final_value, expected,
1039            "Error deserializing unit untagged enum"
1040        );
1041        Ok(())
1042    }
1043
1044    #[test]
1045    fn avro_3645_3646_test_from_value_enum() -> TestResult {
1046        #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
1047        struct TestNullExternalEnum {
1048            a: NullExternalEnum,
1049        }
1050
1051        #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
1052        enum NullExternalEnum {
1053            Val1,
1054            Val2(),
1055            Val3(()),
1056            Val4(u64),
1057        }
1058
1059        let data = [
1060            (
1061                TestNullExternalEnum {
1062                    a: NullExternalEnum::Val1,
1063                },
1064                Value::Record(vec![("a".to_owned(), Value::Enum(0, "Val1".to_owned()))]),
1065            ),
1066            (
1067                TestNullExternalEnum {
1068                    a: NullExternalEnum::Val2(),
1069                },
1070                Value::Record(vec![(
1071                    "a".to_owned(),
1072                    Value::Record(vec![
1073                        ("type".to_owned(), Value::Enum(1, "Val2".to_owned())),
1074                        ("value".to_owned(), Value::Union(1, Box::new(Value::Null))),
1075                    ]),
1076                )]),
1077            ),
1078            (
1079                TestNullExternalEnum {
1080                    a: NullExternalEnum::Val2(),
1081                },
1082                Value::Record(vec![(
1083                    "a".to_owned(),
1084                    Value::Record(vec![
1085                        ("type".to_owned(), Value::Enum(1, "Val2".to_owned())),
1086                        ("value".to_owned(), Value::Array(vec![])),
1087                    ]),
1088                )]),
1089            ),
1090            (
1091                TestNullExternalEnum {
1092                    a: NullExternalEnum::Val3(()),
1093                },
1094                Value::Record(vec![(
1095                    "a".to_owned(),
1096                    Value::Record(vec![
1097                        ("type".to_owned(), Value::Enum(2, "Val3".to_owned())),
1098                        ("value".to_owned(), Value::Union(2, Box::new(Value::Null))),
1099                    ]),
1100                )]),
1101            ),
1102            (
1103                TestNullExternalEnum {
1104                    a: NullExternalEnum::Val4(123),
1105                },
1106                Value::Record(vec![(
1107                    "a".to_owned(),
1108                    Value::Record(vec![
1109                        ("type".to_owned(), Value::Enum(3, "Val4".to_owned())),
1110                        ("value".to_owned(), Value::Union(3, Value::Long(123).into())),
1111                    ]),
1112                )]),
1113            ),
1114        ];
1115
1116        for (expected, test) in data.iter() {
1117            let actual: TestNullExternalEnum = from_value(test)?;
1118            assert_eq!(actual, *expected);
1119        }
1120
1121        Ok(())
1122    }
1123
1124    #[test]
1125    fn test_from_value_single_value_enum() -> TestResult {
1126        let expected = TestSingleValueExternalEnum {
1127            a: SingleValueExternalEnum::Double(64.0),
1128        };
1129
1130        let test = Value::Record(vec![(
1131            "a".to_owned(),
1132            Value::Record(vec![
1133                ("type".to_owned(), Value::String("Double".to_owned())),
1134                (
1135                    "value".to_owned(),
1136                    Value::Union(1, Box::new(Value::Double(64.0))),
1137                ),
1138            ]),
1139        )]);
1140        let final_value: TestSingleValueExternalEnum = from_value(&test)?;
1141        assert_eq!(
1142            final_value, expected,
1143            "Error deserializing single value external enum(union)"
1144        );
1145
1146        Ok(())
1147    }
1148
1149    #[test]
1150    fn test_from_value_struct_enum() -> TestResult {
1151        let expected = TestStructExternalEnum {
1152            a: StructExternalEnum::Val1 { x: 1.0, y: 2.0 },
1153        };
1154
1155        let test = Value::Record(vec![(
1156            "a".to_owned(),
1157            Value::Record(vec![
1158                ("type".to_owned(), Value::String("Val1".to_owned())),
1159                (
1160                    "value".to_owned(),
1161                    Value::Union(
1162                        0,
1163                        Box::new(Value::Record(vec![
1164                            ("x".to_owned(), Value::Float(1.0)),
1165                            ("y".to_owned(), Value::Float(2.0)),
1166                        ])),
1167                    ),
1168                ),
1169            ]),
1170        )]);
1171        let final_value: TestStructExternalEnum = from_value(&test)?;
1172        assert_eq!(
1173            final_value, expected,
1174            "error deserializing struct external enum(union)"
1175        );
1176
1177        Ok(())
1178    }
1179
1180    #[test]
1181    fn test_avro_3692_from_value_struct_flatten() -> TestResult {
1182        #[derive(Deserialize, PartialEq, Debug)]
1183        struct S1 {
1184            f1: String,
1185            #[serde(flatten)]
1186            inner: S2,
1187        }
1188        #[derive(Deserialize, PartialEq, Debug)]
1189        struct S2 {
1190            f2: String,
1191        }
1192        let expected = S1 {
1193            f1: "Hello".to_owned(),
1194            inner: S2 {
1195                f2: "World".to_owned(),
1196            },
1197        };
1198
1199        let test = Value::Record(vec![
1200            ("f1".to_owned(), "Hello".into()),
1201            ("f2".to_owned(), "World".into()),
1202        ]);
1203        let final_value: S1 = from_value(&test)?;
1204        assert_eq!(final_value, expected);
1205
1206        Ok(())
1207    }
1208
1209    #[test]
1210    fn test_from_value_tuple_enum() -> TestResult {
1211        let expected = TestTupleExternalEnum {
1212            a: TupleExternalEnum::Val1(1.0, 2.0),
1213        };
1214
1215        let test = Value::Record(vec![(
1216            "a".to_owned(),
1217            Value::Record(vec![
1218                ("type".to_owned(), Value::String("Val1".to_owned())),
1219                (
1220                    "value".to_owned(),
1221                    Value::Union(
1222                        0,
1223                        Box::new(Value::Array(vec![Value::Float(1.0), Value::Float(2.0)])),
1224                    ),
1225                ),
1226            ]),
1227        )]);
1228        let final_value: TestTupleExternalEnum = from_value(&test)?;
1229        assert_eq!(
1230            final_value, expected,
1231            "error serializing tuple external enum(union)"
1232        );
1233
1234        Ok(())
1235    }
1236
1237    #[test]
1238    fn test_date() -> TestResult {
1239        let raw_value = 1;
1240        let value = Value::Date(raw_value);
1241        let result = crate::from_value::<i32>(&value)?;
1242        assert_eq!(result, raw_value);
1243        Ok(())
1244    }
1245
1246    #[test]
1247    fn test_time_millis() -> TestResult {
1248        let raw_value = 1;
1249        let value = Value::TimeMillis(raw_value);
1250        let result = crate::from_value::<i32>(&value)?;
1251        assert_eq!(result, raw_value);
1252        Ok(())
1253    }
1254
1255    #[test]
1256    fn test_time_micros() -> TestResult {
1257        let raw_value = 1;
1258        let value = Value::TimeMicros(raw_value);
1259        let result = crate::from_value::<i64>(&value)?;
1260        assert_eq!(result, raw_value);
1261        Ok(())
1262    }
1263
1264    #[test]
1265    fn test_timestamp_millis() -> TestResult {
1266        let raw_value = 1;
1267        let value = Value::TimestampMillis(raw_value);
1268        let result = crate::from_value::<i64>(&value)?;
1269        assert_eq!(result, raw_value);
1270        Ok(())
1271    }
1272
1273    #[test]
1274    fn test_timestamp_micros() -> TestResult {
1275        let raw_value = 1;
1276        let value = Value::TimestampMicros(raw_value);
1277        let result = from_value::<i64>(&value)?;
1278        assert_eq!(result, raw_value);
1279        Ok(())
1280    }
1281
1282    #[test]
1283    fn test_avro_3916_timestamp_nanos() -> TestResult {
1284        let raw_value = 1;
1285        let value = Value::TimestampNanos(raw_value);
1286        let result = from_value::<i64>(&value)?;
1287        assert_eq!(result, raw_value);
1288        Ok(())
1289    }
1290
1291    #[test]
1292    fn test_avro_3853_local_timestamp_millis() -> TestResult {
1293        let raw_value = 1;
1294        let value = Value::LocalTimestampMillis(raw_value);
1295        let result = from_value::<i64>(&value)?;
1296        assert_eq!(result, raw_value);
1297        Ok(())
1298    }
1299
1300    #[test]
1301    fn test_avro_3853_local_timestamp_micros() -> TestResult {
1302        let raw_value = 1;
1303        let value = Value::LocalTimestampMicros(raw_value);
1304        let result = crate::from_value::<i64>(&value)?;
1305        assert_eq!(result, raw_value);
1306        Ok(())
1307    }
1308
1309    #[test]
1310    fn test_avro_3916_local_timestamp_nanos() -> TestResult {
1311        let raw_value = 1;
1312        let value = Value::LocalTimestampNanos(raw_value);
1313        let result = crate::from_value::<i64>(&value)?;
1314        assert_eq!(result, raw_value);
1315        Ok(())
1316    }
1317
1318    #[test]
1319    fn test_from_value_uuid_str() -> TestResult {
1320        let raw_value = "9ec535ff-3e2a-45bd-91d3-0a01321b5a49";
1321        let value = Value::Uuid(Uuid::parse_str(raw_value)?);
1322        let result = from_value::<Uuid>(&value)?;
1323        assert_eq!(result.to_string(), raw_value);
1324        Ok(())
1325    }
1326
1327    #[test]
1328    fn test_from_value_uuid_slice() -> TestResult {
1329        let raw_value = &[4, 54, 67, 12, 43, 2, 2, 76, 32, 50, 87, 5, 1, 33, 43, 87];
1330        let value = Value::Uuid(Uuid::from_slice(raw_value)?);
1331        let result = crate::from_value::<Uuid>(&value)?;
1332        assert_eq!(result.as_bytes(), raw_value);
1333        Ok(())
1334    }
1335
1336    #[test]
1337    fn test_from_value_with_union() -> TestResult {
1338        // AVRO-3232 test for deserialize_any on missing fields on the destination struct:
1339        // Error: DeserializeValue("Unsupported union")
1340        // Error: DeserializeValue("incorrect value of type: String")
1341        #[derive(Debug, Deserialize, PartialEq, Eq)]
1342        struct RecordInUnion {
1343            record_in_union: i32,
1344        }
1345
1346        #[derive(Debug, Deserialize, PartialEq, Eq)]
1347        enum EnumInStruct {
1348            Val1,
1349        }
1350
1351        #[derive(Debug, Deserialize, PartialEq, Eq)]
1352        struct StructWithMissingFields {
1353            a_string: String,
1354            a_record: Option<RecordInUnion>,
1355            an_array: Option<[bool; 2]>,
1356            a_union_map: Option<HashMap<String, i64>>,
1357            an_enum: EnumInStruct,
1358        }
1359
1360        let raw_map: HashMap<String, i64> = [
1361            ("long_one".to_string(), 1),
1362            ("long_two".to_string(), 2),
1363            ("long_three".to_string(), 3),
1364            ("time_micros_a".to_string(), 123),
1365            ("timestamp_millis_b".to_string(), 234),
1366            ("timestamp_micros_c".to_string(), 345),
1367            ("timestamp_nanos_d".to_string(), 345_001),
1368            ("local_timestamp_millis_d".to_string(), 678),
1369            ("local_timestamp_micros_e".to_string(), 789),
1370            ("local_timestamp_nanos_f".to_string(), 345_002),
1371        ]
1372        .iter()
1373        .cloned()
1374        .collect();
1375
1376        let value_map = raw_map
1377            .iter()
1378            .map(|(k, v)| match k {
1379                key if key.starts_with("long_") => (k.clone(), Value::Long(*v)),
1380                key if key.starts_with("time_micros_") => (k.clone(), Value::TimeMicros(*v)),
1381                key if key.starts_with("timestamp_millis_") => {
1382                    (k.clone(), Value::TimestampMillis(*v))
1383                }
1384                key if key.starts_with("timestamp_micros_") => {
1385                    (k.clone(), Value::TimestampMicros(*v))
1386                }
1387                key if key.starts_with("timestamp_nanos_") => {
1388                    (k.clone(), Value::TimestampNanos(*v))
1389                }
1390                key if key.starts_with("local_timestamp_millis_") => {
1391                    (k.clone(), Value::LocalTimestampMillis(*v))
1392                }
1393                key if key.starts_with("local_timestamp_micros_") => {
1394                    (k.clone(), Value::LocalTimestampMicros(*v))
1395                }
1396                key if key.starts_with("local_timestamp_nanos_") => {
1397                    (k.clone(), Value::LocalTimestampNanos(*v))
1398                }
1399                _ => unreachable!("unexpected key: {:?}", k),
1400            })
1401            .collect();
1402
1403        let record = Value::Record(vec![
1404            (
1405                "a_string".to_string(),
1406                Value::String("a valid message field".to_string()),
1407            ),
1408            (
1409                "a_non_existing_string".to_string(),
1410                Value::String("a string".to_string()),
1411            ),
1412            (
1413                "a_union_string".to_string(),
1414                Value::Union(0, Box::new(Value::String("a union string".to_string()))),
1415            ),
1416            (
1417                "a_union_long".to_string(),
1418                Value::Union(0, Box::new(Value::Long(412))),
1419            ),
1420            (
1421                "a_union_long".to_string(),
1422                Value::Union(0, Box::new(Value::Long(412))),
1423            ),
1424            (
1425                "a_time_micros".to_string(),
1426                Value::Union(0, Box::new(Value::TimeMicros(123))),
1427            ),
1428            (
1429                "a_non_existing_time_micros".to_string(),
1430                Value::Union(0, Box::new(Value::TimeMicros(-123))),
1431            ),
1432            (
1433                "a_timestamp_millis".to_string(),
1434                Value::Union(0, Box::new(Value::TimestampMillis(234))),
1435            ),
1436            (
1437                "a_non_existing_timestamp_millis".to_string(),
1438                Value::Union(0, Box::new(Value::TimestampMillis(-234))),
1439            ),
1440            (
1441                "a_timestamp_micros".to_string(),
1442                Value::Union(0, Box::new(Value::TimestampMicros(345))),
1443            ),
1444            (
1445                "a_non_existing_timestamp_micros".to_string(),
1446                Value::Union(0, Box::new(Value::TimestampMicros(-345))),
1447            ),
1448            (
1449                "a_timestamp_nanos".to_string(),
1450                Value::Union(0, Box::new(Value::TimestampNanos(345))),
1451            ),
1452            (
1453                "a_non_existing_timestamp_nanos".to_string(),
1454                Value::Union(0, Box::new(Value::TimestampNanos(-345))),
1455            ),
1456            (
1457                "a_local_timestamp_millis".to_string(),
1458                Value::Union(0, Box::new(Value::LocalTimestampMillis(678))),
1459            ),
1460            (
1461                "a_non_existing_local_timestamp_millis".to_string(),
1462                Value::Union(0, Box::new(Value::LocalTimestampMillis(-678))),
1463            ),
1464            (
1465                "a_local_timestamp_micros".to_string(),
1466                Value::Union(0, Box::new(Value::LocalTimestampMicros(789))),
1467            ),
1468            (
1469                "a_non_existing_local_timestamp_micros".to_string(),
1470                Value::Union(0, Box::new(Value::LocalTimestampMicros(-789))),
1471            ),
1472            (
1473                "a_local_timestamp_nanos".to_string(),
1474                Value::Union(0, Box::new(Value::LocalTimestampNanos(789))),
1475            ),
1476            (
1477                "a_non_existing_local_timestamp_nanos".to_string(),
1478                Value::Union(0, Box::new(Value::LocalTimestampNanos(-789))),
1479            ),
1480            (
1481                "a_record".to_string(),
1482                Value::Union(
1483                    0,
1484                    Box::new(Value::Record(vec![(
1485                        "record_in_union".to_string(),
1486                        Value::Int(-2),
1487                    )])),
1488                ),
1489            ),
1490            (
1491                "a_non_existing_record".to_string(),
1492                Value::Union(
1493                    0,
1494                    Box::new(Value::Record(vec![("blah".to_string(), Value::Int(-22))])),
1495                ),
1496            ),
1497            (
1498                "an_array".to_string(),
1499                Value::Union(
1500                    0,
1501                    Box::new(Value::Array(vec![
1502                        Value::Boolean(true),
1503                        Value::Boolean(false),
1504                    ])),
1505                ),
1506            ),
1507            (
1508                "a_non_existing_array".to_string(),
1509                Value::Union(
1510                    0,
1511                    Box::new(Value::Array(vec![
1512                        Value::Boolean(false),
1513                        Value::Boolean(true),
1514                    ])),
1515                ),
1516            ),
1517            (
1518                "a_union_map".to_string(),
1519                Value::Union(0, Box::new(Value::Map(value_map))),
1520            ),
1521            (
1522                "a_non_existing_union_map".to_string(),
1523                Value::Union(0, Box::new(Value::Map(HashMap::new()))),
1524            ),
1525            ("an_enum".to_string(), Value::Enum(0, "Val1".to_owned())),
1526            (
1527                "a_non_existing_enum".to_string(),
1528                Value::Enum(0, "AnotherVariant".to_owned()),
1529            ),
1530        ]);
1531
1532        let deserialized: StructWithMissingFields = crate::from_value(&record)?;
1533        let reference = StructWithMissingFields {
1534            a_string: "a valid message field".to_string(),
1535            a_record: Some(RecordInUnion {
1536                record_in_union: -2,
1537            }),
1538            an_array: Some([true, false]),
1539            a_union_map: Some(raw_map),
1540            an_enum: EnumInStruct::Val1,
1541        };
1542        assert_eq!(deserialized, reference);
1543        Ok(())
1544    }
1545
1546    #[test]
1547    fn avro_3747_human_readable_false() -> TestResult {
1548        use serde::de::Deserializer as SerdeDeserializer;
1549
1550        assert!(!crate::util::is_human_readable());
1551
1552        let deser = &Deserializer::new(&Value::Null);
1553
1554        assert!(!deser.is_human_readable());
1555
1556        Ok(())
1557    }
1558
1559    #[test]
1560    fn test_avro_3892_deserialize_string_from_bytes() -> TestResult {
1561        let raw_value = vec![1, 2, 3, 4];
1562        let value = Value::Bytes(raw_value.clone());
1563        let result = from_value::<String>(&value)?;
1564        assert_eq!(result, String::from_utf8(raw_value)?);
1565        Ok(())
1566    }
1567
1568    #[test]
1569    fn test_avro_3892_deserialize_str_from_bytes() -> TestResult {
1570        let raw_value = &[1, 2, 3, 4];
1571        let value = Value::Bytes(raw_value.to_vec());
1572        let result = from_value::<&str>(&value)?;
1573        assert_eq!(result, std::str::from_utf8(raw_value)?);
1574        Ok(())
1575    }
1576
1577    #[derive(Debug)]
1578    struct Bytes(Vec<u8>);
1579
1580    impl<'de> Deserialize<'de> for Bytes {
1581        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1582        where
1583            D: serde::Deserializer<'de>,
1584        {
1585            struct BytesVisitor;
1586            impl Visitor<'_> for BytesVisitor {
1587                type Value = Bytes;
1588
1589                fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
1590                    formatter.write_str("a byte array")
1591                }
1592
1593                fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
1594                where
1595                    E: serde::de::Error,
1596                {
1597                    Ok(Bytes(v.to_vec()))
1598                }
1599            }
1600            deserializer.deserialize_bytes(BytesVisitor)
1601        }
1602    }
1603
1604    #[test]
1605    fn test_avro_3892_deserialize_bytes_from_decimal() -> TestResult {
1606        let expected_bytes = BigInt::from(123456789).to_signed_bytes_be();
1607        let value = Value::Decimal(Decimal::from(&expected_bytes));
1608        let raw_bytes = from_value::<Bytes>(&value)?;
1609        assert_eq!(raw_bytes.0, expected_bytes);
1610
1611        let value = Value::Union(0, Box::new(Value::Decimal(Decimal::from(&expected_bytes))));
1612        let raw_bytes = from_value::<Option<Bytes>>(&value)?;
1613        assert_eq!(raw_bytes.unwrap().0, expected_bytes);
1614        Ok(())
1615    }
1616
1617    #[test]
1618    fn test_avro_3892_deserialize_bytes_from_uuid() -> TestResult {
1619        let uuid_str = "10101010-2020-2020-2020-101010101010";
1620        let expected_bytes = Uuid::parse_str(uuid_str)?.as_bytes().to_vec();
1621        let value = Value::Uuid(Uuid::parse_str(uuid_str)?);
1622        let raw_bytes = from_value::<Bytes>(&value)?;
1623        assert_eq!(raw_bytes.0, expected_bytes);
1624
1625        let value = Value::Union(0, Box::new(Value::Uuid(Uuid::parse_str(uuid_str)?)));
1626        let raw_bytes = from_value::<Option<Bytes>>(&value)?;
1627        assert_eq!(raw_bytes.unwrap().0, expected_bytes);
1628        Ok(())
1629    }
1630
1631    #[test]
1632    fn test_avro_3892_deserialize_bytes_from_fixed() -> TestResult {
1633        let expected_bytes = vec![1, 2, 3, 4];
1634        let value = Value::Fixed(4, expected_bytes.clone());
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::Fixed(4, expected_bytes.clone())));
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_bytes() -> TestResult {
1646        let expected_bytes = vec![1, 2, 3, 4];
1647        let value = Value::Bytes(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::Bytes(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}