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