@@ -37,6 +37,34 @@ impl fmt::Display for Unexpected {
37
37
}
38
38
}
39
39
40
+ impl From < de:: Unexpected < ' _ > > for Unexpected {
41
+ fn from ( value : de:: Unexpected < ' _ > ) -> Self {
42
+ match value {
43
+ de:: Unexpected :: Bool ( b) => Self :: Bool ( b) ,
44
+ de:: Unexpected :: Unsigned ( u) => Self :: U64 ( u) ,
45
+ de:: Unexpected :: Signed ( s) => Self :: I64 ( s) ,
46
+ de:: Unexpected :: Float ( f) => Self :: Float ( f) ,
47
+ de:: Unexpected :: Char ( c) => Self :: Str ( c. into ( ) ) ,
48
+ de:: Unexpected :: Str ( s) => Self :: Str ( s. into ( ) ) ,
49
+ de:: Unexpected :: Unit => Self :: Unit ,
50
+ de:: Unexpected :: Seq => Self :: Seq ,
51
+ de:: Unexpected :: Map => Self :: Map ,
52
+
53
+ // TODO Maybe add other items to unexpected if needed?
54
+ //de::Unexpected::Bytes(items) => todo!(),
55
+ //de::Unexpected::Option => todo!(),
56
+ //de::Unexpected::NewtypeStruct => todo!(),
57
+ //de::Unexpected::Enum => todo!(),
58
+ //de::Unexpected::UnitVariant => todo!(),
59
+ //de::Unexpected::NewtypeVariant => todo!(),
60
+ //de::Unexpected::TupleVariant => todo!(),
61
+ //de::Unexpected::StructVariant => todo!(),
62
+ //de::Unexpected::Other(_) => todo!(),
63
+ _ => Self :: Unit ,
64
+ }
65
+ }
66
+ }
67
+
40
68
/// Represents all possible errors that can occur when working with
41
69
/// configuration.
42
70
#[ non_exhaustive]
@@ -289,6 +317,24 @@ impl de::Error for ConfigError {
289
317
fn custom < T : fmt:: Display > ( msg : T ) -> Self {
290
318
Self :: Message ( msg. to_string ( ) )
291
319
}
320
+
321
+ fn missing_field ( field : & ' static str ) -> Self {
322
+ Self :: NotFound ( field. into ( ) )
323
+ }
324
+
325
+ fn invalid_type ( unexp : de:: Unexpected < ' _ > , exp : & dyn de:: Expected ) -> Self {
326
+ Self :: Type {
327
+ origin : None ,
328
+ unexpected : unexp. into ( ) ,
329
+ // TODO A better way of doing this? Maybe make "expected" a Cow<str>?
330
+ expected : exp. to_string ( ) . leak ( ) ,
331
+ key : None ,
332
+ }
333
+ }
334
+
335
+ fn invalid_value ( unexp : de:: Unexpected < ' _ > , exp : & dyn de:: Expected ) -> Self {
336
+ <Self as de:: Error >:: invalid_type ( unexp, exp)
337
+ }
292
338
}
293
339
294
340
impl ser:: Error for ConfigError {
0 commit comments