@@ -7,7 +7,7 @@ use crate::de::{
7
7
} ;
8
8
9
9
#[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
10
- use crate :: de:: { MapAccess , Unexpected } ;
10
+ use crate :: de:: { Expected , MapAccess , StdError , Unexpected } ;
11
11
12
12
#[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
13
13
pub use self :: content:: {
@@ -2586,6 +2586,72 @@ where
2586
2586
}
2587
2587
}
2588
2588
2589
+ #[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2590
+ #[ derive( Debug ) ]
2591
+ pub enum FlatMapDeserializerError < E > {
2592
+ Inner ( E ) ,
2593
+ NoVariantFoundInFlattenedData ( & ' static str ) ,
2594
+ }
2595
+
2596
+ #[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2597
+ impl < E > Display for FlatMapDeserializerError < E >
2598
+ where
2599
+ E : Display ,
2600
+ {
2601
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2602
+ match self {
2603
+ FlatMapDeserializerError :: Inner ( e) => write ! ( f, "{}" , e) ,
2604
+ FlatMapDeserializerError :: NoVariantFoundInFlattenedData ( name) => {
2605
+ write ! ( f, "no variant of enum {} found in flattened data" , name)
2606
+ }
2607
+ }
2608
+ }
2609
+ }
2610
+
2611
+ #[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2612
+ impl < E > StdError for FlatMapDeserializerError < E > where E : StdError { }
2613
+
2614
+ #[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2615
+ impl < E > Error for FlatMapDeserializerError < E >
2616
+ where
2617
+ E : Error ,
2618
+ {
2619
+ fn custom < T > ( msg : T ) -> Self
2620
+ where
2621
+ T : Display ,
2622
+ {
2623
+ FlatMapDeserializerError :: Inner ( Error :: custom ( msg) )
2624
+ }
2625
+
2626
+ fn invalid_type ( unexp : Unexpected , exp : & Expected ) -> Self {
2627
+ FlatMapDeserializerError :: Inner ( Error :: invalid_type ( unexp, exp) )
2628
+ }
2629
+
2630
+ fn invalid_value ( unexp : Unexpected , exp : & Expected ) -> Self {
2631
+ FlatMapDeserializerError :: Inner ( Error :: invalid_value ( unexp, exp) )
2632
+ }
2633
+
2634
+ fn invalid_length ( len : usize , exp : & Expected ) -> Self {
2635
+ FlatMapDeserializerError :: Inner ( Error :: invalid_length ( len, exp) )
2636
+ }
2637
+
2638
+ fn unknown_variant ( variant : & str , exp : & ' static [ & ' static str ] ) -> Self {
2639
+ FlatMapDeserializerError :: Inner ( Error :: unknown_variant ( variant, exp) )
2640
+ }
2641
+
2642
+ fn unknown_field ( field : & str , exp : & ' static [ & ' static str ] ) -> Self {
2643
+ FlatMapDeserializerError :: Inner ( Error :: unknown_field ( field, exp) )
2644
+ }
2645
+
2646
+ fn missing_field ( field : & ' static str ) -> Self {
2647
+ FlatMapDeserializerError :: Inner ( Error :: missing_field ( field) )
2648
+ }
2649
+
2650
+ fn duplicate_field ( field : & ' static str ) -> Self {
2651
+ FlatMapDeserializerError :: Inner ( Error :: duplicate_field ( field) )
2652
+ }
2653
+ }
2654
+
2589
2655
#[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2590
2656
pub struct FlatMapDeserializer < ' a , ' de : ' a , E > (
2591
2657
pub & ' a mut Vec < Option < ( Content < ' de > , Content < ' de > ) > > ,
@@ -2597,7 +2663,7 @@ impl<'a, 'de, E> FlatMapDeserializer<'a, 'de, E>
2597
2663
where
2598
2664
E : Error ,
2599
2665
{
2600
- fn deserialize_other < V > ( ) -> Result < V , E > {
2666
+ fn deserialize_other < V > ( ) -> Result < V , FlatMapDeserializerError < E > > {
2601
2667
Err ( Error :: custom ( "can only flatten structs and maps" ) )
2602
2668
}
2603
2669
}
@@ -2621,7 +2687,7 @@ impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E>
2621
2687
where
2622
2688
E : Error ,
2623
2689
{
2624
- type Error = E ;
2690
+ type Error = FlatMapDeserializerError < E > ;
2625
2691
2626
2692
fn deserialize_any < V > ( self , visitor : V ) -> Result < V :: Value , Self :: Error >
2627
2693
where
@@ -2645,10 +2711,9 @@ where
2645
2711
}
2646
2712
}
2647
2713
2648
- Err ( Error :: custom ( format_args ! (
2649
- "no variant of enum {} found in flattened data" ,
2650
- name
2651
- ) ) )
2714
+ Err ( FlatMapDeserializerError :: NoVariantFoundInFlattenedData (
2715
+ name,
2716
+ ) )
2652
2717
}
2653
2718
2654
2719
fn deserialize_map < V > ( self , visitor : V ) -> Result < V :: Value , Self :: Error >
0 commit comments