Skip to content

Commit 6ce6ad6

Browse files
committed
Add FlatMapDeserializerError
1 parent ede9762 commit 6ce6ad6

File tree

1 file changed

+72
-7
lines changed

1 file changed

+72
-7
lines changed

serde/src/private/de.rs

+72-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::de::{
77
};
88

99
#[cfg(any(feature = "std", feature = "alloc"))]
10-
use crate::de::{MapAccess, Unexpected};
10+
use crate::de::{Expected, MapAccess, StdError, Unexpected};
1111

1212
#[cfg(any(feature = "std", feature = "alloc"))]
1313
pub use self::content::{
@@ -2586,6 +2586,72 @@ where
25862586
}
25872587
}
25882588

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+
25892655
#[cfg(any(feature = "std", feature = "alloc"))]
25902656
pub struct FlatMapDeserializer<'a, 'de: 'a, E>(
25912657
pub &'a mut Vec<Option<(Content<'de>, Content<'de>)>>,
@@ -2597,7 +2663,7 @@ impl<'a, 'de, E> FlatMapDeserializer<'a, 'de, E>
25972663
where
25982664
E: Error,
25992665
{
2600-
fn deserialize_other<V>() -> Result<V, E> {
2666+
fn deserialize_other<V>() -> Result<V, FlatMapDeserializerError<E>> {
26012667
Err(Error::custom("can only flatten structs and maps"))
26022668
}
26032669
}
@@ -2621,7 +2687,7 @@ impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E>
26212687
where
26222688
E: Error,
26232689
{
2624-
type Error = E;
2690+
type Error = FlatMapDeserializerError<E>;
26252691

26262692
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
26272693
where
@@ -2645,10 +2711,9 @@ where
26452711
}
26462712
}
26472713

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+
))
26522717
}
26532718

26542719
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>

0 commit comments

Comments
 (0)