Debian Codegraph @

src/de/error.rs

raw
1 use std::{error, fmt, fmt::Display, io, string, sync::Arc};
2
3 use serde::de::{self, Unexpected};
4
5 use crate::Bson;
6
7 /// Possible errors that can arise during decoding.
8 #[derive(Clone, Debug)]
9 #[non_exhaustive]
10 pub enum Error {
11 /// A [`std::io::Error`](https://doc.rust-lang.org/std/io/struct.Error.html) encountered while deserializing.
12 Io(Arc<io::Error>),
13
14 /// A [`std::string::FromUtf8Error`](https://doc.rust-lang.org/std/string/struct.FromUtf8Error.html) encountered
15 /// while decoding a UTF-8 String from the input data.
16 InvalidUtf8String(string::FromUtf8Error),
17
18 /// While decoding a [`Document`](crate::Document) from bytes, an unexpected or unsupported
19 /// element type was encountered.
20 #[non_exhaustive]
21 UnrecognizedDocumentElementType {
22 /// The key at which an unexpected/unsupported element type was encountered.
23 key: String,
24
25 /// The encountered element type.
26 element_type: u8,
27 },
28
29 /// The end of the BSON input was reached too soon.
30 EndOfStream,
31
32 /// A general error encountered during deserialization.
33 /// See: <https://docs.serde.rs/serde/de/trait.Error.html>
34 #[non_exhaustive]
35 DeserializationError {
36 /// A message describing the error.
37 message: String,
38 },
39 }
40
41 impl From<io::Error> for Error {
42 fn from(err: io::Error) -> Error {
43 Error::Io(Arc::new(err))
44 }
45 }
46
47 impl From<string::FromUtf8Error> for Error {
48 fn from(err: string::FromUtf8Error) -> Error {
49 Error::InvalidUtf8String(err)
50 }
51 }
52
53 impl fmt::Display for Error {
54 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
55 match *self {
56 Error::Io(ref inner) => inner.fmt(fmt),
57 Error::InvalidUtf8String(ref inner) => inner.fmt(fmt),
58 Error::UnrecognizedDocumentElementType {
59 ref key,
60 element_type,
61 } => write!(
62 fmt,
63 "unrecognized element type for key \"{}\": `{:#x}`",
64 key, element_type
65 ),
66 Error::EndOfStream => fmt.write_str("end of stream"),
67 Error::DeserializationError { ref message } => message.fmt(fmt),
68 }
69 }
70 }
71
72 impl error::Error for Error {
73 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
74 match *self {
75 Error::Io(ref inner) => Some(inner.as_ref()),
76 Error::InvalidUtf8String(ref inner) => Some(inner),
77 _ => None,
78 }
79 }
80 }
81
82 impl de::Error for Error {
83 fn custom<T: Display>(msg: T) -> Error {
84 Error::DeserializationError {
85 message: msg.to_string(),
86 }
87 }
88 }
89
90 /// Alias for `Result<T, Error>`.
91 pub type Result<T> = std::result::Result<T, Error>;
92
93 impl Bson {
94 /// Method for converting a given [`Bson`] value to a [`serde::de::Unexpected`] for error
95 /// reporting.
96 pub(crate) fn as_unexpected(&self) -> Unexpected {
97 match self {
98 Bson::Array(_) => Unexpected::Seq,
99 Bson::Binary(b) => Unexpected::Bytes(b.bytes.as_slice()),
100 Bson::Boolean(b) => Unexpected::Bool(*b),
101 Bson::DbPointer(_) => Unexpected::Other("dbpointer"),
102 Bson::Document(_) => Unexpected::Map,
103 Bson::Double(f) => Unexpected::Float(*f),
104 Bson::Int32(i) => Unexpected::Signed(*i as i64),
105 Bson::Int64(i) => Unexpected::Signed(*i),
106 Bson::JavaScriptCode(_) => Unexpected::Other("javascript code"),
107 Bson::JavaScriptCodeWithScope(_) => Unexpected::Other("javascript code with scope"),
108 Bson::MaxKey => Unexpected::Other("maxkey"),
109 Bson::MinKey => Unexpected::Other("minkey"),
110 Bson::Null => Unexpected::Unit,
111 Bson::Undefined => Unexpected::Other("undefined"),
112 Bson::ObjectId(_) => Unexpected::Other("objectid"),
113 Bson::RegularExpression(_) => Unexpected::Other("regex"),
114 Bson::String(s) => Unexpected::Str(s.as_str()),
115 Bson::Symbol(_) => Unexpected::Other("symbol"),
116 Bson::Timestamp(_) => Unexpected::Other("timestamp"),
117 Bson::DateTime(_) => Unexpected::Other("datetime"),
118 Bson::Decimal128(_) => Unexpected::Other("decimal128"),
119 }
120 }
121 }