@@ -2,12 +2,12 @@ use crate::lib::*;
2
2
3
3
use crate :: de:: value:: { BorrowedBytesDeserializer , BytesDeserializer } ;
4
4
use crate :: de:: {
5
- Deserialize , DeserializeSeed , Deserializer , EnumAccess , Error , IntoDeserializer , VariantAccess ,
6
- Visitor ,
5
+ Deserialize , DeserializeSeed , Deserializer , EnumAccess , Error , Expected , IntoDeserializer ,
6
+ VariantAccess , Visitor ,
7
7
} ;
8
8
9
9
#[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
10
- use crate :: de:: { MapAccess , Unexpected } ;
10
+ use crate :: de:: { MapAccess , StdError , Unexpected } ;
11
11
12
12
#[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
13
13
pub use self :: content:: {
@@ -2586,6 +2586,66 @@ where
2586
2586
}
2587
2587
}
2588
2588
2589
+ #[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2590
+ #[ derive( Debug ) ]
2591
+ pub enum FlatMapDeserializerError < E : Error > {
2592
+ Inner ( E ) ,
2593
+ NoVariantFoundInFlattenedData ( & ' static str ) ,
2594
+ }
2595
+
2596
+ #[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2597
+ impl < E : Error > Display for FlatMapDeserializerError < E > {
2598
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2599
+ match self {
2600
+ FlatMapDeserializerError :: Inner ( e) => write ! ( f, "{}" , e) ,
2601
+ FlatMapDeserializerError :: NoVariantFoundInFlattenedData ( name) => {
2602
+ write ! ( f, "no variant of enum {} found in flattened data" , name)
2603
+ }
2604
+ }
2605
+ }
2606
+ }
2607
+
2608
+ #[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2609
+ impl < E : Error > StdError for FlatMapDeserializerError < E > { }
2610
+
2611
+ #[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2612
+ impl < E : Error > Error for FlatMapDeserializerError < E > {
2613
+ fn custom < T > ( msg : T ) -> Self
2614
+ where
2615
+ T : Display ,
2616
+ {
2617
+ FlatMapDeserializerError :: Inner ( Error :: custom ( msg) )
2618
+ }
2619
+
2620
+ fn invalid_type ( unexp : Unexpected , exp : & Expected ) -> Self {
2621
+ FlatMapDeserializerError :: Inner ( Error :: invalid_type ( unexp, exp) )
2622
+ }
2623
+
2624
+ fn invalid_value ( unexp : Unexpected , exp : & Expected ) -> Self {
2625
+ FlatMapDeserializerError :: Inner ( Error :: invalid_value ( unexp, exp) )
2626
+ }
2627
+
2628
+ fn invalid_length ( len : usize , exp : & Expected ) -> Self {
2629
+ FlatMapDeserializerError :: Inner ( Error :: invalid_length ( len, exp) )
2630
+ }
2631
+
2632
+ fn unknown_variant ( variant : & str , exp : & ' static [ & ' static str ] ) -> Self {
2633
+ FlatMapDeserializerError :: Inner ( Error :: unknown_variant ( variant, exp) )
2634
+ }
2635
+
2636
+ fn unknown_field ( field : & str , exp : & ' static [ & ' static str ] ) -> Self {
2637
+ FlatMapDeserializerError :: Inner ( Error :: unknown_field ( field, exp) )
2638
+ }
2639
+
2640
+ fn missing_field ( field : & ' static str ) -> Self {
2641
+ FlatMapDeserializerError :: Inner ( Error :: missing_field ( field) )
2642
+ }
2643
+
2644
+ fn duplicate_field ( field : & ' static str ) -> Self {
2645
+ FlatMapDeserializerError :: Inner ( Error :: duplicate_field ( field) )
2646
+ }
2647
+ }
2648
+
2589
2649
#[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
2590
2650
pub struct FlatMapDeserializer < ' a , ' de : ' a , E > (
2591
2651
pub & ' a mut Vec < Option < ( Content < ' de > , Content < ' de > ) > > ,
@@ -2597,7 +2657,7 @@ impl<'a, 'de, E> FlatMapDeserializer<'a, 'de, E>
2597
2657
where
2598
2658
E : Error ,
2599
2659
{
2600
- fn deserialize_other < V > ( ) -> Result < V , E > {
2660
+ fn deserialize_other < V > ( ) -> Result < V , FlatMapDeserializerError < E > > {
2601
2661
Err ( Error :: custom ( "can only flatten structs and maps" ) )
2602
2662
}
2603
2663
}
@@ -2621,7 +2681,7 @@ impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E>
2621
2681
where
2622
2682
E : Error ,
2623
2683
{
2624
- type Error = E ;
2684
+ type Error = FlatMapDeserializerError < E > ;
2625
2685
2626
2686
fn deserialize_any < V > ( self , visitor : V ) -> Result < V :: Value , Self :: Error >
2627
2687
where
@@ -2645,10 +2705,9 @@ where
2645
2705
}
2646
2706
}
2647
2707
2648
- Err ( Error :: custom ( format_args ! (
2649
- "no variant of enum {} found in flattened data" ,
2650
- name
2651
- ) ) )
2708
+ Err ( FlatMapDeserializerError :: NoVariantFoundInFlattenedData (
2709
+ name,
2710
+ ) )
2652
2711
}
2653
2712
2654
2713
fn deserialize_map < V > ( self , visitor : V ) -> Result < V :: Value , Self :: Error >
0 commit comments