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