Coverage Report

Created: 2026-05-16 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/erased-serde-0.4.10/src/error.rs
Line
Count
Source
1
use alloc::borrow::ToOwned;
2
use alloc::boxed::Box;
3
use alloc::string::{String, ToString};
4
use alloc::vec::Vec;
5
use core::fmt::{self, Debug, Display};
6
use serde::de::Expected;
7
8
/// Error when a `Serializer` or `Deserializer` trait object fails.
9
pub struct Error {
10
    imp: Box<ErrorImpl>,
11
}
12
13
/// Result type alias where the error is `erased_serde::Error`.
14
pub type Result<T> = core::result::Result<T, Error>;
15
16
0
pub(crate) fn erase_de<E: serde::de::Error>(e: E) -> Error {
17
0
    serde::de::Error::custom(e)
18
0
}
19
20
0
pub(crate) fn unerase_de<E: serde::de::Error>(e: Error) -> E {
21
0
    e.as_serde_de_error()
22
0
}
23
24
impl Display for Error {
25
0
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
26
0
        let error = self.as_serde_de_error::<serde::de::value::Error>();
27
0
        Display::fmt(&error, formatter)
28
0
    }
29
}
30
31
impl Debug for Error {
32
0
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
33
0
        let error = self.as_serde_de_error::<serde::de::value::Error>();
34
0
        Debug::fmt(&error, formatter)
35
0
    }
36
}
37
38
impl serde::ser::StdError for Error {}
39
40
enum ErrorImpl {
41
    Custom(String),
42
    InvalidType {
43
        unexpected: Unexpected,
44
        expected: String,
45
    },
46
    InvalidValue {
47
        unexpected: Unexpected,
48
        expected: String,
49
    },
50
    InvalidLength {
51
        len: usize,
52
        expected: String,
53
    },
54
    UnknownVariant {
55
        variant: String,
56
        expected: &'static [&'static str],
57
    },
58
    UnknownField {
59
        field: String,
60
        expected: &'static [&'static str],
61
    },
62
    MissingField {
63
        field: &'static str,
64
    },
65
    DuplicateField {
66
        field: &'static str,
67
    },
68
}
69
70
enum Unexpected {
71
    Bool(bool),
72
    Unsigned(u64),
73
    Signed(i64),
74
    Float(f64),
75
    Char(char),
76
    Str(String),
77
    Bytes(Vec<u8>),
78
    Unit,
79
    Option,
80
    NewtypeStruct,
81
    Seq,
82
    Map,
83
    Enum,
84
    UnitVariant,
85
    NewtypeVariant,
86
    TupleVariant,
87
    StructVariant,
88
    Other(String),
89
}
90
91
impl serde::ser::Error for Error {
92
0
    fn custom<T: Display>(msg: T) -> Self {
93
0
        let imp = Box::new(ErrorImpl::Custom(msg.to_string()));
94
0
        Error { imp }
95
0
    }
Unexecuted instantiation: <erased_serde::error::Error as serde_core::ser::Error>::custom::<alloc::boxed::Box<alloc::string::String>>
Unexecuted instantiation: <erased_serde::error::Error as serde_core::ser::Error>::custom::<&dyn core::fmt::Display>
Unexecuted instantiation: <erased_serde::error::Error as serde_core::ser::Error>::custom::<_>
96
}
97
98
impl serde::de::Error for Error {
99
0
    fn custom<T: Display>(msg: T) -> Self {
100
0
        let imp = Box::new(ErrorImpl::Custom(msg.to_string()));
101
0
        Error { imp }
102
0
    }
103
104
0
    fn invalid_type(unexpected: serde::de::Unexpected, expected: &dyn Expected) -> Self {
105
0
        let imp = Box::new(ErrorImpl::InvalidType {
106
0
            unexpected: Unexpected::from_serde(unexpected),
107
0
            expected: expected.to_string(),
108
0
        });
109
0
        Error { imp }
110
0
    }
111
112
0
    fn invalid_value(unexpected: serde::de::Unexpected, expected: &dyn Expected) -> Self {
113
0
        let imp = Box::new(ErrorImpl::InvalidValue {
114
0
            unexpected: Unexpected::from_serde(unexpected),
115
0
            expected: expected.to_string(),
116
0
        });
117
0
        Error { imp }
118
0
    }
119
120
0
    fn invalid_length(len: usize, expected: &dyn Expected) -> Self {
121
0
        let imp = Box::new(ErrorImpl::InvalidLength {
122
0
            len,
123
0
            expected: expected.to_string(),
124
0
        });
125
0
        Error { imp }
126
0
    }
127
128
0
    fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self {
129
0
        let imp = Box::new(ErrorImpl::UnknownVariant {
130
0
            variant: variant.to_owned(),
131
0
            expected,
132
0
        });
133
0
        Error { imp }
134
0
    }
135
136
0
    fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self {
137
0
        let imp = Box::new(ErrorImpl::UnknownField {
138
0
            field: field.to_owned(),
139
0
            expected,
140
0
        });
141
0
        Error { imp }
142
0
    }
143
144
0
    fn missing_field(field: &'static str) -> Self {
145
0
        let imp = Box::new(ErrorImpl::MissingField { field });
146
0
        Error { imp }
147
0
    }
148
149
0
    fn duplicate_field(field: &'static str) -> Self {
150
0
        let imp = Box::new(ErrorImpl::DuplicateField { field });
151
0
        Error { imp }
152
0
    }
153
}
154
155
impl Error {
156
0
    fn as_serde_de_error<E: serde::de::Error>(&self) -> E {
157
0
        match self.imp.as_ref() {
158
0
            ErrorImpl::Custom(msg) => E::custom(msg),
159
            ErrorImpl::InvalidType {
160
0
                unexpected,
161
0
                expected,
162
0
            } => E::invalid_type(unexpected.as_serde(), &expected.as_str()),
163
            ErrorImpl::InvalidValue {
164
0
                unexpected,
165
0
                expected,
166
0
            } => E::invalid_value(unexpected.as_serde(), &expected.as_str()),
167
0
            ErrorImpl::InvalidLength { len, expected } => {
168
0
                E::invalid_length(*len, &expected.as_str())
169
            }
170
0
            ErrorImpl::UnknownVariant { variant, expected } => {
171
0
                E::unknown_variant(variant, expected)
172
            }
173
0
            ErrorImpl::UnknownField { field, expected } => E::unknown_field(field, expected),
174
0
            ErrorImpl::MissingField { field } => E::missing_field(field),
175
0
            ErrorImpl::DuplicateField { field } => E::duplicate_field(field),
176
        }
177
0
    }
178
}
179
180
impl Unexpected {
181
0
    fn from_serde(unexpected: serde::de::Unexpected) -> Self {
182
0
        match unexpected {
183
0
            serde::de::Unexpected::Bool(value) => Unexpected::Bool(value),
184
0
            serde::de::Unexpected::Unsigned(value) => Unexpected::Unsigned(value),
185
0
            serde::de::Unexpected::Signed(value) => Unexpected::Signed(value),
186
0
            serde::de::Unexpected::Float(value) => Unexpected::Float(value),
187
0
            serde::de::Unexpected::Char(value) => Unexpected::Char(value),
188
0
            serde::de::Unexpected::Str(value) => Unexpected::Str(value.to_owned()),
189
0
            serde::de::Unexpected::Bytes(value) => Unexpected::Bytes(value.to_owned()),
190
0
            serde::de::Unexpected::Unit => Unexpected::Unit,
191
0
            serde::de::Unexpected::Option => Unexpected::Option,
192
0
            serde::de::Unexpected::NewtypeStruct => Unexpected::NewtypeStruct,
193
0
            serde::de::Unexpected::Seq => Unexpected::Seq,
194
0
            serde::de::Unexpected::Map => Unexpected::Map,
195
0
            serde::de::Unexpected::Enum => Unexpected::Enum,
196
0
            serde::de::Unexpected::UnitVariant => Unexpected::UnitVariant,
197
0
            serde::de::Unexpected::NewtypeVariant => Unexpected::NewtypeVariant,
198
0
            serde::de::Unexpected::TupleVariant => Unexpected::TupleVariant,
199
0
            serde::de::Unexpected::StructVariant => Unexpected::StructVariant,
200
0
            serde::de::Unexpected::Other(msg) => Unexpected::Other(msg.to_owned()),
201
        }
202
0
    }
203
204
0
    fn as_serde(&self) -> serde::de::Unexpected {
205
0
        match self {
206
0
            Unexpected::Bool(value) => serde::de::Unexpected::Bool(*value),
207
0
            Unexpected::Unsigned(value) => serde::de::Unexpected::Unsigned(*value),
208
0
            Unexpected::Signed(value) => serde::de::Unexpected::Signed(*value),
209
0
            Unexpected::Float(value) => serde::de::Unexpected::Float(*value),
210
0
            Unexpected::Char(value) => serde::de::Unexpected::Char(*value),
211
0
            Unexpected::Str(value) => serde::de::Unexpected::Str(value),
212
0
            Unexpected::Bytes(value) => serde::de::Unexpected::Bytes(value),
213
0
            Unexpected::Unit => serde::de::Unexpected::Unit,
214
0
            Unexpected::Option => serde::de::Unexpected::Option,
215
0
            Unexpected::NewtypeStruct => serde::de::Unexpected::NewtypeStruct,
216
0
            Unexpected::Seq => serde::de::Unexpected::Seq,
217
0
            Unexpected::Map => serde::de::Unexpected::Map,
218
0
            Unexpected::Enum => serde::de::Unexpected::Enum,
219
0
            Unexpected::UnitVariant => serde::de::Unexpected::UnitVariant,
220
0
            Unexpected::NewtypeVariant => serde::de::Unexpected::NewtypeVariant,
221
0
            Unexpected::TupleVariant => serde::de::Unexpected::TupleVariant,
222
0
            Unexpected::StructVariant => serde::de::Unexpected::StructVariant,
223
0
            Unexpected::Other(msg) => serde::de::Unexpected::Other(msg),
224
        }
225
0
    }
226
}