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::writer::datum::GenericDatumWriter;
957    use crate::{Decimal, reader::datum::GenericDatumReader};
958
959    #[derive(PartialEq, Eq, Serialize, Deserialize, Debug)]
960    pub struct StringEnum {
961        pub source: String,
962    }
963
964    #[test]
965    fn avro_3955_decode_enum() -> TestResult {
966        let schema_content = r#"
967{
968  "name": "AccessLog",
969  "namespace": "com.clevercloud.accesslogs.common.avro",
970  "type": "record",
971  "fields": [
972    {
973      "name": "source",
974      "type": {
975        "type": "enum",
976        "name": "SourceType",
977        "items": "string",
978        "symbols": ["SOZU", "HAPROXY", "HAPROXY_TCP"]
979      }
980    }
981  ]
982}
983"#;
984
985        let schema = crate::Schema::parse_str(schema_content)?;
986        let data = StringEnum {
987            source: "SOZU".to_string(),
988        };
989
990        // encode into avro
991        let value = crate::to_value(&data)?;
992
993        let mut buf = std::io::Cursor::new(
994            GenericDatumWriter::builder(&schema)
995                .build()?
996                .write_value_to_vec(value)?,
997        );
998
999        // decode from avro
1000        let value = GenericDatumReader::builder(&schema)
1001            .build()?
1002            .read_value(&mut buf)?;
1003
1004        let decoded_data: StringEnum = crate::from_value(&value)?;
1005
1006        assert_eq!(decoded_data, data);
1007
1008        Ok(())
1009    }
1010
1011    #[test]
1012    fn avro_3955_encode_enum_data_with_wrong_content() -> TestResult {
1013        let schema_content = r#"
1014{
1015  "name": "AccessLog",
1016  "namespace": "com.clevercloud.accesslogs.common.avro",
1017  "type": "record",
1018  "fields": [
1019    {
1020      "name": "source",
1021      "type": {
1022        "type": "enum",
1023        "name": "SourceType",
1024        "items": "string",
1025        "symbols": ["SOZU", "HAPROXY", "HAPROXY_TCP"]
1026      }
1027    }
1028  ]
1029}
1030"#;
1031
1032        let schema = crate::Schema::parse_str(schema_content)?;
1033        let data = StringEnum {
1034            source: "WRONG_ITEM".to_string(),
1035        };
1036
1037        // encode into avro
1038        let value = crate::to_value(data)?;
1039
1040        // The following sentence have to fail has the data is wrong.
1041        let encoded_data = GenericDatumWriter::builder(&schema)
1042            .build()?
1043            .write_value_to_vec(value);
1044
1045        assert!(encoded_data.is_err());
1046
1047        Ok(())
1048    }
1049
1050    #[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
1051    struct Test {
1052        a: i64,
1053        b: String,
1054        c: Decimal,
1055    }
1056
1057    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
1058    struct TestInner {
1059        a: Test,
1060        b: i32,
1061    }
1062
1063    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
1064    struct TestUnitExternalEnum {
1065        a: UnitExternalEnum,
1066    }
1067
1068    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
1069    enum UnitExternalEnum {
1070        Val1,
1071        Val2,
1072    }
1073
1074    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
1075    struct TestUnitInternalEnum {
1076        a: UnitInternalEnum,
1077    }
1078
1079    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
1080    #[serde(tag = "t")]
1081    enum UnitInternalEnum {
1082        Val1,
1083        Val2,
1084    }
1085
1086    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
1087    struct TestUnitAdjacentEnum {
1088        a: UnitAdjacentEnum,
1089    }
1090
1091    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
1092    #[serde(tag = "t", content = "v")]
1093    enum UnitAdjacentEnum {
1094        Val1,
1095        Val2,
1096    }
1097
1098    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
1099    struct TestUnitUntaggedEnum {
1100        a: UnitUntaggedEnum,
1101    }
1102
1103    #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
1104    #[serde(untagged)]
1105    enum UnitUntaggedEnum {
1106        Val1,
1107        Val2,
1108    }
1109
1110    #[derive(Debug, Serialize, Deserialize, PartialEq)]
1111    struct TestSingleValueExternalEnum {
1112        a: SingleValueExternalEnum,
1113    }
1114
1115    #[derive(Debug, Serialize, Deserialize, PartialEq)]
1116    enum SingleValueExternalEnum {
1117        Double(f64),
1118        String(String),
1119    }
1120
1121    #[derive(Debug, Serialize, Deserialize, PartialEq)]
1122    struct TestStructExternalEnum {
1123        a: StructExternalEnum,
1124    }
1125
1126    #[derive(Debug, Serialize, Deserialize, PartialEq)]
1127    enum StructExternalEnum {
1128        Val1 { x: f32, y: f32 },
1129        Val2 { x: f32, y: f32 },
1130    }
1131
1132    #[derive(Debug, Serialize, Deserialize, PartialEq)]
1133    struct TestTupleExternalEnum {
1134        a: TupleExternalEnum,
1135    }
1136
1137    #[derive(Debug, Serialize, Deserialize, PartialEq)]
1138    enum TupleExternalEnum {
1139        Val1(f32, f32),
1140        Val2(f32, f32, f32),
1141    }
1142
1143    #[test]
1144    fn test_from_value() -> TestResult {
1145        let test = Value::Record(vec![
1146            ("a".to_owned(), Value::Long(27)),
1147            ("b".to_owned(), Value::String("foo".to_owned())),
1148            ("c".to_owned(), Value::Decimal(Decimal::from(vec![1, 24]))),
1149        ]);
1150        let expected = Test {
1151            a: 27,
1152            b: "foo".to_owned(),
1153            c: Decimal::from(vec![1, 24]),
1154        };
1155        let final_value: Test = from_value(&test)?;
1156        assert_eq!(final_value, expected);
1157
1158        let test_inner = Value::Record(vec![
1159            (
1160                "a".to_owned(),
1161                Value::Record(vec![
1162                    ("a".to_owned(), Value::Long(27)),
1163                    ("b".to_owned(), Value::String("foo".to_owned())),
1164                    ("c".to_owned(), Value::Decimal(Decimal::from(vec![1, 24]))),
1165                ]),
1166            ),
1167            ("b".to_owned(), Value::Int(35)),
1168        ]);
1169
1170        let expected_inner = TestInner { a: expected, b: 35 };
1171        let final_value: TestInner = from_value(&test_inner)?;
1172        assert_eq!(final_value, expected_inner);
1173
1174        Ok(())
1175    }
1176
1177    #[test]
1178    fn test_from_value_unit_enum() -> TestResult {
1179        let expected = TestUnitExternalEnum {
1180            a: UnitExternalEnum::Val1,
1181        };
1182
1183        let test = Value::Record(vec![("a".to_owned(), Value::Enum(0, "Val1".to_owned()))]);
1184        let final_value: TestUnitExternalEnum = from_value(&test)?;
1185        assert_eq!(
1186            final_value, expected,
1187            "Error deserializing unit external enum"
1188        );
1189
1190        let expected = TestUnitInternalEnum {
1191            a: UnitInternalEnum::Val1,
1192        };
1193
1194        let test = Value::Record(vec![(
1195            "a".to_owned(),
1196            Value::Record(vec![("t".to_owned(), Value::String("Val1".to_owned()))]),
1197        )]);
1198        let final_value: TestUnitInternalEnum = from_value(&test)?;
1199        assert_eq!(
1200            final_value, expected,
1201            "Error deserializing unit internal enum"
1202        );
1203        let expected = TestUnitAdjacentEnum {
1204            a: UnitAdjacentEnum::Val1,
1205        };
1206
1207        let test = Value::Record(vec![(
1208            "a".to_owned(),
1209            Value::Record(vec![("t".to_owned(), Value::String("Val1".to_owned()))]),
1210        )]);
1211        let final_value: TestUnitAdjacentEnum = from_value(&test)?;
1212        assert_eq!(
1213            final_value, expected,
1214            "Error deserializing unit adjacent enum"
1215        );
1216        let expected = TestUnitUntaggedEnum {
1217            a: UnitUntaggedEnum::Val1,
1218        };
1219
1220        let test = Value::Record(vec![("a".to_owned(), Value::Null)]);
1221        let final_value: TestUnitUntaggedEnum = from_value(&test)?;
1222        assert_eq!(
1223            final_value, expected,
1224            "Error deserializing unit untagged enum"
1225        );
1226        Ok(())
1227    }
1228
1229    #[test]
1230    fn avro_3645_3646_test_from_value_enum() -> TestResult {
1231        #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
1232        struct TestNullExternalEnum {
1233            a: NullExternalEnum,
1234        }
1235
1236        #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
1237        enum NullExternalEnum {
1238            Val1,
1239            Val2(),
1240            Val3(()),
1241            Val4(u64),
1242        }
1243
1244        let data = [
1245            (
1246                TestNullExternalEnum {
1247                    a: NullExternalEnum::Val1,
1248                },
1249                Value::Record(vec![("a".to_owned(), Value::Enum(0, "Val1".to_owned()))]),
1250            ),
1251            (
1252                TestNullExternalEnum {
1253                    a: NullExternalEnum::Val2(),
1254                },
1255                Value::Record(vec![(
1256                    "a".to_owned(),
1257                    Value::Record(vec![
1258                        ("type".to_owned(), Value::Enum(1, "Val2".to_owned())),
1259                        ("value".to_owned(), Value::Union(1, Box::new(Value::Null))),
1260                    ]),
1261                )]),
1262            ),
1263            (
1264                TestNullExternalEnum {
1265                    a: NullExternalEnum::Val2(),
1266                },
1267                Value::Record(vec![(
1268                    "a".to_owned(),
1269                    Value::Record(vec![
1270                        ("type".to_owned(), Value::Enum(1, "Val2".to_owned())),
1271                        ("value".to_owned(), Value::Array(vec![])),
1272                    ]),
1273                )]),
1274            ),
1275            (
1276                TestNullExternalEnum {
1277                    a: NullExternalEnum::Val3(()),
1278                },
1279                Value::Record(vec![(
1280                    "a".to_owned(),
1281                    Value::Record(vec![
1282                        ("type".to_owned(), Value::Enum(2, "Val3".to_owned())),
1283                        ("value".to_owned(), Value::Union(2, Box::new(Value::Null))),
1284                    ]),
1285                )]),
1286            ),
1287            (
1288                TestNullExternalEnum {
1289                    a: NullExternalEnum::Val4(123),
1290                },
1291                Value::Record(vec![(
1292                    "a".to_owned(),
1293                    Value::Record(vec![
1294                        ("type".to_owned(), Value::Enum(3, "Val4".to_owned())),
1295                        ("value".to_owned(), Value::Union(3, Value::Long(123).into())),
1296                    ]),
1297                )]),
1298            ),
1299        ];
1300
1301        for (expected, test) in data.iter() {
1302            let actual: TestNullExternalEnum = from_value(test)?;
1303            assert_eq!(actual, *expected);
1304        }
1305
1306        Ok(())
1307    }
1308
1309    #[test]
1310    fn test_from_value_single_value_enum() -> TestResult {
1311        let expected = TestSingleValueExternalEnum {
1312            a: SingleValueExternalEnum::Double(64.0),
1313        };
1314
1315        let test = Value::Record(vec![(
1316            "a".to_owned(),
1317            Value::Record(vec![
1318                ("type".to_owned(), Value::String("Double".to_owned())),
1319                (
1320                    "value".to_owned(),
1321                    Value::Union(1, Box::new(Value::Double(64.0))),
1322                ),
1323            ]),
1324        )]);
1325        let final_value: TestSingleValueExternalEnum = from_value(&test)?;
1326        assert_eq!(
1327            final_value, expected,
1328            "Error deserializing single value external enum(union)"
1329        );
1330
1331        Ok(())
1332    }
1333
1334    #[test]
1335    fn test_from_value_struct_enum() -> TestResult {
1336        let expected = TestStructExternalEnum {
1337            a: StructExternalEnum::Val1 { x: 1.0, y: 2.0 },
1338        };
1339
1340        let test = Value::Record(vec![(
1341            "a".to_owned(),
1342            Value::Record(vec![
1343                ("type".to_owned(), Value::String("Val1".to_owned())),
1344                (
1345                    "value".to_owned(),
1346                    Value::Union(
1347                        0,
1348                        Box::new(Value::Record(vec![
1349                            ("x".to_owned(), Value::Float(1.0)),
1350                            ("y".to_owned(), Value::Float(2.0)),
1351                        ])),
1352                    ),
1353                ),
1354            ]),
1355        )]);
1356        let final_value: TestStructExternalEnum = from_value(&test)?;
1357        assert_eq!(
1358            final_value, expected,
1359            "error deserializing struct external enum(union)"
1360        );
1361
1362        Ok(())
1363    }
1364
1365    #[test]
1366    fn test_avro_3692_from_value_struct_flatten() -> TestResult {
1367        #[derive(Deserialize, PartialEq, Debug)]
1368        struct S1 {
1369            f1: String,
1370            #[serde(flatten)]
1371            inner: S2,
1372        }
1373        #[derive(Deserialize, PartialEq, Debug)]
1374        struct S2 {
1375            f2: String,
1376        }
1377        let expected = S1 {
1378            f1: "Hello".to_owned(),
1379            inner: S2 {
1380                f2: "World".to_owned(),
1381            },
1382        };
1383
1384        let test = Value::Record(vec![
1385            ("f1".to_owned(), "Hello".into()),
1386            ("f2".to_owned(), "World".into()),
1387        ]);
1388        let final_value: S1 = from_value(&test)?;
1389        assert_eq!(final_value, expected);
1390
1391        Ok(())
1392    }
1393
1394    #[test]
1395    fn test_from_value_tuple_enum() -> TestResult {
1396        let expected = TestTupleExternalEnum {
1397            a: TupleExternalEnum::Val1(1.0, 2.0),
1398        };
1399
1400        let test = Value::Record(vec![(
1401            "a".to_owned(),
1402            Value::Record(vec![
1403                ("type".to_owned(), Value::String("Val1".to_owned())),
1404                (
1405                    "value".to_owned(),
1406                    Value::Union(
1407                        0,
1408                        Box::new(Value::Array(vec![Value::Float(1.0), Value::Float(2.0)])),
1409                    ),
1410                ),
1411            ]),
1412        )]);
1413        let final_value: TestTupleExternalEnum = from_value(&test)?;
1414        assert_eq!(
1415            final_value, expected,
1416            "error serializing tuple external enum(union)"
1417        );
1418
1419        Ok(())
1420    }
1421
1422    #[test]
1423    fn test_date() -> TestResult {
1424        let raw_value = 1;
1425        let value = Value::Date(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_millis() -> TestResult {
1433        let raw_value = 1;
1434        let value = Value::TimeMillis(raw_value);
1435        let result = crate::from_value::<i32>(&value)?;
1436        assert_eq!(result, raw_value);
1437        Ok(())
1438    }
1439
1440    #[test]
1441    fn test_time_micros() -> TestResult {
1442        let raw_value = 1;
1443        let value = Value::TimeMicros(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_millis() -> TestResult {
1451        let raw_value = 1;
1452        let value = Value::TimestampMillis(raw_value);
1453        let result = crate::from_value::<i64>(&value)?;
1454        assert_eq!(result, raw_value);
1455        Ok(())
1456    }
1457
1458    #[test]
1459    fn test_timestamp_micros() -> TestResult {
1460        let raw_value = 1;
1461        let value = Value::TimestampMicros(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_3916_timestamp_nanos() -> TestResult {
1469        let raw_value = 1;
1470        let value = Value::TimestampNanos(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_millis() -> TestResult {
1478        let raw_value = 1;
1479        let value = Value::LocalTimestampMillis(raw_value);
1480        let result = from_value::<i64>(&value)?;
1481        assert_eq!(result, raw_value);
1482        Ok(())
1483    }
1484
1485    #[test]
1486    fn test_avro_3853_local_timestamp_micros() -> TestResult {
1487        let raw_value = 1;
1488        let value = Value::LocalTimestampMicros(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_avro_3916_local_timestamp_nanos() -> TestResult {
1496        let raw_value = 1;
1497        let value = Value::LocalTimestampNanos(raw_value);
1498        let result = crate::from_value::<i64>(&value)?;
1499        assert_eq!(result, raw_value);
1500        Ok(())
1501    }
1502
1503    #[test]
1504    fn test_from_value_uuid_str() -> TestResult {
1505        let raw_value = "9ec535ff-3e2a-45bd-91d3-0a01321b5a49";
1506        let value = Value::Uuid(Uuid::parse_str(raw_value)?);
1507        let result = from_value::<Uuid>(&value)?;
1508        assert_eq!(result.to_string(), raw_value);
1509        Ok(())
1510    }
1511
1512    #[test]
1513    fn test_from_value_uuid_slice() -> TestResult {
1514        let raw_value = &[4, 54, 67, 12, 43, 2, 2, 76, 32, 50, 87, 5, 1, 33, 43, 87];
1515        let value = Value::Uuid(Uuid::from_slice(raw_value)?);
1516        let result = crate::from_value::<Uuid>(&value)?;
1517        assert_eq!(result.as_bytes(), raw_value);
1518        Ok(())
1519    }
1520
1521    #[test]
1522    fn test_from_value_with_union() -> TestResult {
1523        // AVRO-3232 test for deserialize_any on missing fields on the destination struct:
1524        // Error: DeserializeValue("Unsupported union")
1525        // Error: DeserializeValue("incorrect value of type: String")
1526        #[derive(Debug, Deserialize, PartialEq, Eq)]
1527        struct RecordInUnion {
1528            record_in_union: i32,
1529        }
1530
1531        #[derive(Debug, Deserialize, PartialEq, Eq)]
1532        enum EnumInStruct {
1533            Val1,
1534        }
1535
1536        #[derive(Debug, Deserialize, PartialEq, Eq)]
1537        struct StructWithMissingFields {
1538            a_string: String,
1539            a_record: Option<RecordInUnion>,
1540            an_array: Option<[bool; 2]>,
1541            a_union_map: Option<HashMap<String, i64>>,
1542            an_enum: EnumInStruct,
1543        }
1544
1545        let raw_map: HashMap<String, i64> = [
1546            ("long_one".to_string(), 1),
1547            ("long_two".to_string(), 2),
1548            ("long_three".to_string(), 3),
1549            ("time_micros_a".to_string(), 123),
1550            ("timestamp_millis_b".to_string(), 234),
1551            ("timestamp_micros_c".to_string(), 345),
1552            ("timestamp_nanos_d".to_string(), 345_001),
1553            ("local_timestamp_millis_d".to_string(), 678),
1554            ("local_timestamp_micros_e".to_string(), 789),
1555            ("local_timestamp_nanos_f".to_string(), 345_002),
1556        ]
1557        .iter()
1558        .cloned()
1559        .collect();
1560
1561        let value_map = raw_map
1562            .iter()
1563            .map(|(k, v)| match k {
1564                key if key.starts_with("long_") => (k.clone(), Value::Long(*v)),
1565                key if key.starts_with("time_micros_") => (k.clone(), Value::TimeMicros(*v)),
1566                key if key.starts_with("timestamp_millis_") => {
1567                    (k.clone(), Value::TimestampMillis(*v))
1568                }
1569                key if key.starts_with("timestamp_micros_") => {
1570                    (k.clone(), Value::TimestampMicros(*v))
1571                }
1572                key if key.starts_with("timestamp_nanos_") => {
1573                    (k.clone(), Value::TimestampNanos(*v))
1574                }
1575                key if key.starts_with("local_timestamp_millis_") => {
1576                    (k.clone(), Value::LocalTimestampMillis(*v))
1577                }
1578                key if key.starts_with("local_timestamp_micros_") => {
1579                    (k.clone(), Value::LocalTimestampMicros(*v))
1580                }
1581                key if key.starts_with("local_timestamp_nanos_") => {
1582                    (k.clone(), Value::LocalTimestampNanos(*v))
1583                }
1584                _ => unreachable!("unexpected key: {:?}", k),
1585            })
1586            .collect();
1587
1588        let record = Value::Record(vec![
1589            (
1590                "a_string".to_string(),
1591                Value::String("a valid message field".to_string()),
1592            ),
1593            (
1594                "a_non_existing_string".to_string(),
1595                Value::String("a string".to_string()),
1596            ),
1597            (
1598                "a_union_string".to_string(),
1599                Value::Union(0, Box::new(Value::String("a union string".to_string()))),
1600            ),
1601            (
1602                "a_union_long".to_string(),
1603                Value::Union(0, Box::new(Value::Long(412))),
1604            ),
1605            (
1606                "a_union_long".to_string(),
1607                Value::Union(0, Box::new(Value::Long(412))),
1608            ),
1609            (
1610                "a_time_micros".to_string(),
1611                Value::Union(0, Box::new(Value::TimeMicros(123))),
1612            ),
1613            (
1614                "a_non_existing_time_micros".to_string(),
1615                Value::Union(0, Box::new(Value::TimeMicros(-123))),
1616            ),
1617            (
1618                "a_timestamp_millis".to_string(),
1619                Value::Union(0, Box::new(Value::TimestampMillis(234))),
1620            ),
1621            (
1622                "a_non_existing_timestamp_millis".to_string(),
1623                Value::Union(0, Box::new(Value::TimestampMillis(-234))),
1624            ),
1625            (
1626                "a_timestamp_micros".to_string(),
1627                Value::Union(0, Box::new(Value::TimestampMicros(345))),
1628            ),
1629            (
1630                "a_non_existing_timestamp_micros".to_string(),
1631                Value::Union(0, Box::new(Value::TimestampMicros(-345))),
1632            ),
1633            (
1634                "a_timestamp_nanos".to_string(),
1635                Value::Union(0, Box::new(Value::TimestampNanos(345))),
1636            ),
1637            (
1638                "a_non_existing_timestamp_nanos".to_string(),
1639                Value::Union(0, Box::new(Value::TimestampNanos(-345))),
1640            ),
1641            (
1642                "a_local_timestamp_millis".to_string(),
1643                Value::Union(0, Box::new(Value::LocalTimestampMillis(678))),
1644            ),
1645            (
1646                "a_non_existing_local_timestamp_millis".to_string(),
1647                Value::Union(0, Box::new(Value::LocalTimestampMillis(-678))),
1648            ),
1649            (
1650                "a_local_timestamp_micros".to_string(),
1651                Value::Union(0, Box::new(Value::LocalTimestampMicros(789))),
1652            ),
1653            (
1654                "a_non_existing_local_timestamp_micros".to_string(),
1655                Value::Union(0, Box::new(Value::LocalTimestampMicros(-789))),
1656            ),
1657            (
1658                "a_local_timestamp_nanos".to_string(),
1659                Value::Union(0, Box::new(Value::LocalTimestampNanos(789))),
1660            ),
1661            (
1662                "a_non_existing_local_timestamp_nanos".to_string(),
1663                Value::Union(0, Box::new(Value::LocalTimestampNanos(-789))),
1664            ),
1665            (
1666                "a_record".to_string(),
1667                Value::Union(
1668                    0,
1669                    Box::new(Value::Record(vec![(
1670                        "record_in_union".to_string(),
1671                        Value::Int(-2),
1672                    )])),
1673                ),
1674            ),
1675            (
1676                "a_non_existing_record".to_string(),
1677                Value::Union(
1678                    0,
1679                    Box::new(Value::Record(vec![("blah".to_string(), Value::Int(-22))])),
1680                ),
1681            ),
1682            (
1683                "an_array".to_string(),
1684                Value::Union(
1685                    0,
1686                    Box::new(Value::Array(vec![
1687                        Value::Boolean(true),
1688                        Value::Boolean(false),
1689                    ])),
1690                ),
1691            ),
1692            (
1693                "a_non_existing_array".to_string(),
1694                Value::Union(
1695                    0,
1696                    Box::new(Value::Array(vec![
1697                        Value::Boolean(false),
1698                        Value::Boolean(true),
1699                    ])),
1700                ),
1701            ),
1702            (
1703                "a_union_map".to_string(),
1704                Value::Union(0, Box::new(Value::Map(value_map))),
1705            ),
1706            (
1707                "a_non_existing_union_map".to_string(),
1708                Value::Union(0, Box::new(Value::Map(HashMap::new()))),
1709            ),
1710            ("an_enum".to_string(), Value::Enum(0, "Val1".to_owned())),
1711            (
1712                "a_non_existing_enum".to_string(),
1713                Value::Enum(0, "AnotherVariant".to_owned()),
1714            ),
1715        ]);
1716
1717        let deserialized: StructWithMissingFields = crate::from_value(&record)?;
1718        let reference = StructWithMissingFields {
1719            a_string: "a valid message field".to_string(),
1720            a_record: Some(RecordInUnion {
1721                record_in_union: -2,
1722            }),
1723            an_array: Some([true, false]),
1724            a_union_map: Some(raw_map),
1725            an_enum: EnumInStruct::Val1,
1726        };
1727        assert_eq!(deserialized, reference);
1728        Ok(())
1729    }
1730
1731    #[test]
1732    fn avro_3747_human_readable_false() -> TestResult {
1733        use serde::de::Deserializer as SerdeDeserializer;
1734
1735        assert!(!crate::util::is_human_readable());
1736
1737        let deser = Deserializer::new(&Value::Null);
1738
1739        assert!(!deser.is_human_readable());
1740
1741        Ok(())
1742    }
1743
1744    #[test]
1745    fn test_avro_3892_deserialize_string_from_bytes() -> TestResult {
1746        let raw_value = vec![1, 2, 3, 4];
1747        let value = Value::Bytes(raw_value.clone());
1748        let result = from_value::<String>(&value)?;
1749        assert_eq!(result, String::from_utf8(raw_value)?);
1750        Ok(())
1751    }
1752
1753    #[test]
1754    fn test_avro_3892_deserialize_str_from_bytes() -> TestResult {
1755        let raw_value = &[1, 2, 3, 4];
1756        let value = Value::Bytes(raw_value.to_vec());
1757        let result = from_value::<&str>(&value)?;
1758        assert_eq!(result, std::str::from_utf8(raw_value)?);
1759        Ok(())
1760    }
1761
1762    #[derive(Debug)]
1763    struct Bytes(Vec<u8>);
1764
1765    impl<'de> Deserialize<'de> for Bytes {
1766        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1767        where
1768            D: serde::Deserializer<'de>,
1769        {
1770            struct BytesVisitor;
1771            impl Visitor<'_> for BytesVisitor {
1772                type Value = Bytes;
1773
1774                fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
1775                    formatter.write_str("a byte array")
1776                }
1777
1778                fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
1779                where
1780                    E: serde::de::Error,
1781                {
1782                    Ok(Bytes(v.to_vec()))
1783                }
1784            }
1785            deserializer.deserialize_bytes(BytesVisitor)
1786        }
1787    }
1788
1789    #[test]
1790    fn test_avro_3892_deserialize_bytes_from_decimal() -> TestResult {
1791        let expected_bytes = BigInt::from(123456789).to_signed_bytes_be();
1792        let value = Value::Decimal(Decimal::from(&expected_bytes));
1793        let raw_bytes = from_value::<Bytes>(&value)?;
1794        assert_eq!(raw_bytes.0, expected_bytes);
1795
1796        let value = Value::Union(0, Box::new(Value::Decimal(Decimal::from(&expected_bytes))));
1797        let raw_bytes = from_value::<Option<Bytes>>(&value)?;
1798        assert_eq!(raw_bytes.unwrap().0, expected_bytes);
1799        Ok(())
1800    }
1801
1802    #[test]
1803    fn test_avro_3892_deserialize_bytes_from_uuid() -> TestResult {
1804        let uuid_str = "10101010-2020-2020-2020-101010101010";
1805        let expected_bytes = Uuid::parse_str(uuid_str)?.as_bytes().to_vec();
1806        let value = Value::Uuid(Uuid::parse_str(uuid_str)?);
1807        let raw_bytes = from_value::<Bytes>(&value)?;
1808        assert_eq!(raw_bytes.0, expected_bytes);
1809
1810        let value = Value::Union(0, Box::new(Value::Uuid(Uuid::parse_str(uuid_str)?)));
1811        let raw_bytes = from_value::<Option<Bytes>>(&value)?;
1812        assert_eq!(raw_bytes.unwrap().0, expected_bytes);
1813        Ok(())
1814    }
1815
1816    #[test]
1817    fn test_avro_3892_deserialize_bytes_from_fixed() -> TestResult {
1818        let expected_bytes = vec![1, 2, 3, 4];
1819        let value = Value::Fixed(4, expected_bytes.clone());
1820        let raw_bytes = from_value::<Bytes>(&value)?;
1821        assert_eq!(raw_bytes.0, expected_bytes);
1822
1823        let value = Value::Union(0, Box::new(Value::Fixed(4, expected_bytes.clone())));
1824        let raw_bytes = from_value::<Option<Bytes>>(&value)?;
1825        assert_eq!(raw_bytes.unwrap().0, expected_bytes);
1826        Ok(())
1827    }
1828
1829    #[test]
1830    fn test_avro_3892_deserialize_bytes_from_bytes() -> TestResult {
1831        let expected_bytes = vec![1, 2, 3, 4];
1832        let value = Value::Bytes(expected_bytes.clone());
1833        let raw_bytes = from_value::<Bytes>(&value)?;
1834        assert_eq!(raw_bytes.0, expected_bytes);
1835
1836        let value = Value::Union(0, Box::new(Value::Bytes(expected_bytes.clone())));
1837        let raw_bytes = from_value::<Option<Bytes>>(&value)?;
1838        assert_eq!(raw_bytes.unwrap().0, expected_bytes);
1839        Ok(())
1840    }
1841
1842    #[test]
1843    fn avro_rs_414_deserialize_char_from_string() -> TestResult {
1844        let value = Value::String('a'.to_string());
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_bytes() -> TestResult {
1853        let value = Value::Bytes([b'a'].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_fixed() -> TestResult {
1862        let value = Value::Fixed(4, [b'a', 0, 0, 0].to_vec());
1863        let result = from_value::<char>(&value)?;
1864        assert_eq!(result, 'a');
1865
1866        Ok(())
1867    }
1868
1869    #[test]
1870    fn avro_rs_414_deserialize_char_from_long_string() -> TestResult {
1871        let value = Value::String("avro".to_string());
1872        let result = from_value::<char>(&value).unwrap_err().to_string();
1873        assert_eq!(
1874            result,
1875            "Failed to deserialize Avro value into value: Tried to deserialize char from string, but the string was longer than one char: avro"
1876        );
1877
1878        Ok(())
1879    }
1880
1881    #[test]
1882    fn avro_rs_414_deserialize_char_from_long_bytes() -> TestResult {
1883        let value = Value::Bytes(b"avro".to_vec());
1884        let result = from_value::<char>(&value).unwrap_err().to_string();
1885        assert_eq!(
1886            result,
1887            "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"
1888        );
1889
1890        Ok(())
1891    }
1892
1893    #[test]
1894    fn avro_rs_414_deserialize_char_from_long_fixed() -> TestResult {
1895        let value = Value::Fixed(5, [b'a', 0, 0, 0, 0].to_vec());
1896        let result = from_value::<char>(&value).unwrap_err().to_string();
1897        assert_eq!(
1898            result,
1899            "Failed to deserialize Avro value into value: Expected a String|Bytes|Fixed(4) for char, but got Fixed(5, [97, 0, 0, 0, 0])"
1900        );
1901
1902        Ok(())
1903    }
1904
1905    #[test]
1906    fn avro_rs_414_deserialize_char_from_short_fixed() -> TestResult {
1907        let value = Value::Fixed(3, [b'a', 0, 0].to_vec());
1908        let result = from_value::<char>(&value).unwrap_err().to_string();
1909        assert_eq!(
1910            result,
1911            "Failed to deserialize Avro value into value: Expected a String|Bytes|Fixed(4) for char, but got Fixed(3, [97, 0, 0])"
1912        );
1913
1914        Ok(())
1915    }
1916}