Skip to content

Commit c5024e4

Browse files
committed
Add FlatMapDeserializerError
1 parent ede9762 commit c5024e4

File tree

1 file changed

+68
-9
lines changed

1 file changed

+68
-9
lines changed

serde/src/private/de.rs

+68-9
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use crate::lib::*;
22

33
use crate::de::value::{BorrowedBytesDeserializer, BytesDeserializer};
44
use crate::de::{
5-
Deserialize, DeserializeSeed, Deserializer, EnumAccess, Error, IntoDeserializer, VariantAccess,
6-
Visitor,
5+
Deserialize, DeserializeSeed, Deserializer, EnumAccess, Error, Expected, IntoDeserializer,
6+
VariantAccess, Visitor,
77
};
88

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

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

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+
25892649
#[cfg(any(feature = "std", feature = "alloc"))]
25902650
pub struct FlatMapDeserializer<'a, 'de: 'a, E>(
25912651
pub &'a mut Vec<Option<(Content<'de>, Content<'de>)>>,
@@ -2597,7 +2657,7 @@ impl<'a, 'de, E> FlatMapDeserializer<'a, 'de, E>
25972657
where
25982658
E: Error,
25992659
{
2600-
fn deserialize_other<V>() -> Result<V, E> {
2660+
fn deserialize_other<V>() -> Result<V, FlatMapDeserializerError<E>> {
26012661
Err(Error::custom("can only flatten structs and maps"))
26022662
}
26032663
}
@@ -2621,7 +2681,7 @@ impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E>
26212681
where
26222682
E: Error,
26232683
{
2624-
type Error = E;
2684+
type Error = FlatMapDeserializerError<E>;
26252685

26262686
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
26272687
where
@@ -2645,10 +2705,9 @@ where
26452705
}
26462706
}
26472707

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+
))
26522711
}
26532712

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

0 commit comments

Comments
 (0)