Skip to content

Commit dce846a

Browse files
committed
Distinguish between custom and flattened variant errors
1 parent ede9762 commit dce846a

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

serde/src/private/de.rs

+40-18
Original file line numberDiff line numberDiff line change
@@ -2587,17 +2587,43 @@ where
25872587
}
25882588

25892589
#[cfg(any(feature = "std", feature = "alloc"))]
2590-
pub struct FlatMapDeserializer<'a, 'de: 'a, E>(
2591-
pub &'a mut Vec<Option<(Content<'de>, Content<'de>)>>,
2592-
pub PhantomData<E>,
2593-
);
2590+
#[cfg_attr(any(feature = "std", feature = "alloc"), derive(Debug))]
2591+
pub enum FlatMapDeserializerError {
2592+
Message(String),
2593+
NoVariantFoundInFlattenedData(&'static str),
2594+
}
25942595

25952596
#[cfg(any(feature = "std", feature = "alloc"))]
2596-
impl<'a, 'de, E> FlatMapDeserializer<'a, 'de, E>
2597-
where
2598-
E: Error,
2599-
{
2600-
fn deserialize_other<V>() -> Result<V, E> {
2597+
impl Display for FlatMapDeserializerError {
2598+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2599+
match self {
2600+
FlatMapDeserializerError::Message(msg) => write!(f, "{}", msg),
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 std::error::Error for FlatMapDeserializerError {}
2610+
2611+
#[cfg(any(feature = "std", feature = "alloc"))]
2612+
impl Error for FlatMapDeserializerError {
2613+
fn custom<T>(msg: T) -> Self
2614+
where
2615+
T: Display,
2616+
{
2617+
FlatMapDeserializerError::Message(msg.to_string())
2618+
}
2619+
}
2620+
2621+
#[cfg(any(feature = "std", feature = "alloc"))]
2622+
pub struct FlatMapDeserializer<'a, 'de: 'a>(pub &'a mut Vec<Option<(Content<'de>, Content<'de>)>>);
2623+
2624+
#[cfg(any(feature = "std", feature = "alloc"))]
2625+
impl<'a, 'de> FlatMapDeserializer<'a, 'de> {
2626+
fn deserialize_other<V>() -> Result<V, FlatMapDeserializerError> {
26012627
Err(Error::custom("can only flatten structs and maps"))
26022628
}
26032629
}
@@ -2617,11 +2643,8 @@ macro_rules! forward_to_deserialize_other {
26172643
}
26182644

26192645
#[cfg(any(feature = "std", feature = "alloc"))]
2620-
impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E>
2621-
where
2622-
E: Error,
2623-
{
2624-
type Error = E;
2646+
impl<'a, 'de> Deserializer<'de> for FlatMapDeserializer<'a, 'de> {
2647+
type Error = FlatMapDeserializerError;
26252648

26262649
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
26272650
where
@@ -2645,10 +2668,9 @@ where
26452668
}
26462669
}
26472670

2648-
Err(Error::custom(format_args!(
2649-
"no variant of enum {} found in flattened data",
2650-
name
2651-
)))
2671+
Err(FlatMapDeserializerError::NoVariantFoundInFlattenedData(
2672+
name,
2673+
))
26522674
}
26532675

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

0 commit comments

Comments
 (0)