serde_with/de/
skip_error.rs

1use super::impls::macros::foreach_map;
2use crate::prelude::*;
3#[cfg(feature = "hashbrown_0_14")]
4use hashbrown_0_14::HashMap as HashbrownMap014;
5#[cfg(feature = "hashbrown_0_15")]
6use hashbrown_0_15::HashMap as HashbrownMap015;
7#[cfg(feature = "hashbrown_0_16")]
8use hashbrown_0_16::HashMap as HashbrownMap016;
9#[cfg(feature = "indexmap_1")]
10use indexmap_1::IndexMap;
11#[cfg(feature = "indexmap_2")]
12use indexmap_2::IndexMap as IndexMap2;
13
14enum GoodOrError<T, TAs, I> {
15    Good(T),
16    // Only here to consume the TAs,I generics
17    Error(PhantomData<(TAs, I)>),
18}
19
20impl<'de, T, TAs, I> Deserialize<'de> for GoodOrError<T, TAs, I>
21where
22    TAs: DeserializeAs<'de, T>,
23    I: InspectError,
24{
25    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
26    where
27        D: Deserializer<'de>,
28    {
29        let is_hr = deserializer.is_human_readable();
30        let content: content::de::Content<'de> = Deserialize::deserialize(deserializer)?;
31
32        Ok(
33            match <DeserializeAsWrap<T, TAs>>::deserialize(content::de::ContentDeserializer::<
34                D::Error,
35            >::new(content, is_hr))
36            {
37                Ok(elem) => GoodOrError::Good(elem.into_inner()),
38                Err(e) => {
39                    I::inspect_error(e);
40                    GoodOrError::Error(PhantomData)
41                }
42            },
43        )
44    }
45}
46
47impl<'de, T, U, I> DeserializeAs<'de, Vec<T>> for VecSkipError<U, I>
48where
49    U: DeserializeAs<'de, T>,
50    I: InspectError,
51{
52    fn deserialize_as<D>(deserializer: D) -> Result<Vec<T>, D::Error>
53    where
54        D: Deserializer<'de>,
55    {
56        struct SeqVisitor<T, U, I>(PhantomData<(T, U, I)>);
57
58        impl<'de, T, TAs, I> Visitor<'de> for SeqVisitor<T, TAs, I>
59        where
60            I: InspectError,
61            TAs: DeserializeAs<'de, T>,
62        {
63            type Value = Vec<T>;
64
65            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
66                formatter.write_str("a sequence")
67            }
68
69            fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
70            where
71                A: SeqAccess<'de>,
72            {
73                utils::SeqIter::new(seq)
74                    .filter_map(|res: Result<GoodOrError<T, TAs, I>, A::Error>| match res {
75                        Ok(GoodOrError::Good(value)) => Some(Ok(value)),
76                        Ok(GoodOrError::Error(_)) => None,
77                        Err(err) => Some(Err(err)),
78                    })
79                    .collect()
80            }
81        }
82
83        let visitor = SeqVisitor::<T, U, I>(PhantomData);
84        deserializer.deserialize_seq(visitor)
85    }
86}
87
88struct MapSkipErrorVisitor<MAP, K, KAs, V, VAs, I>(PhantomData<(MAP, K, KAs, V, VAs, I)>);
89
90impl<'de, MAP, K, KAs, V, VAs, I> Visitor<'de> for MapSkipErrorVisitor<MAP, K, KAs, V, VAs, I>
91where
92    MAP: FromIterator<(K, V)>,
93    KAs: DeserializeAs<'de, K>,
94    VAs: DeserializeAs<'de, V>,
95    I: InspectError,
96{
97    type Value = MAP;
98
99    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
100        formatter.write_str("a map")
101    }
102
103    #[inline]
104    fn visit_map<A>(self, access: A) -> Result<Self::Value, A::Error>
105    where
106        A: MapAccess<'de>,
107    {
108        type KVPair<K, KAs, V, VAs, I> = (GoodOrError<K, KAs, I>, GoodOrError<V, VAs, I>);
109        utils::MapIter::new(access)
110            .filter_map(
111                |res: Result<KVPair<K, KAs, V, VAs, I>, A::Error>| match res {
112                    Ok((GoodOrError::Good(key), GoodOrError::Good(value))) => {
113                        Some(Ok((key, value)))
114                    }
115                    Ok(_) => None,
116                    Err(err) => Some(Err(err)),
117                },
118            )
119            .collect()
120    }
121}
122
123#[cfg(feature = "alloc")]
124macro_rules! map_impl {
125    (
126        $ty:ident < K $(: $kbound1:ident $(+ $kbound2:ident)*)?, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)* >,
127        $with_capacity:expr
128    ) => {
129        impl<'de, K, V, KAs, VAs, I $(, $typaram)*> DeserializeAs<'de, $ty<K, V $(, $typaram)*>>
130            for MapSkipError<KAs, VAs, I>
131        where
132            KAs: DeserializeAs<'de, K>,
133            VAs: DeserializeAs<'de, V>,
134            I: InspectError,
135            $(K: $kbound1 $(+ $kbound2)*,)?
136            $($typaram: $bound1 $(+ $bound2)*),*
137        {
138            fn deserialize_as<D>(deserializer: D) -> Result<$ty<K, V $(, $typaram)*>, D::Error>
139            where
140                D: Deserializer<'de>,
141            {
142                deserializer.deserialize_map(MapSkipErrorVisitor::<
143                    $ty<K, V $(, $typaram)*>,
144                    K,
145                    KAs,
146                    V,
147                    VAs,
148                    I,
149                >(PhantomData))
150            }
151        }
152    };
153}
154foreach_map!(map_impl);