1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use std::{error, fmt, fmt::Display, io, string};
use serde::de::{self, Unexpected};
use crate::Bson;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
IoError(io::Error),
FromUtf8Error(string::FromUtf8Error),
UnrecognizedDocumentElementType {
key: String,
element_type: u8,
},
SyntaxError { message: String },
EndOfStream,
InvalidTimestamp(i64),
AmbiguousTimestamp(i64),
DeserializationError {
message: String,
},
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
Error::IoError(err)
}
}
impl From<string::FromUtf8Error> for Error {
fn from(err: string::FromUtf8Error) -> Error {
Error::FromUtf8Error(err)
}
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::IoError(ref inner) => inner.fmt(fmt),
Error::FromUtf8Error(ref inner) => inner.fmt(fmt),
Error::UnrecognizedDocumentElementType {
ref key,
element_type,
} => write!(
fmt,
"unrecognized element type for key \"{}\": `{:#x}`",
key, element_type
),
Error::SyntaxError { ref message } => message.fmt(fmt),
Error::EndOfStream => fmt.write_str("end of stream"),
Error::DeserializationError { ref message } => message.fmt(fmt),
Error::InvalidTimestamp(ref i) => write!(fmt, "no such local time {}", i),
Error::AmbiguousTimestamp(ref i) => write!(fmt, "ambiguous local time {}", i),
}
}
}
impl error::Error for Error {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
Error::IoError(ref inner) => Some(inner),
Error::FromUtf8Error(ref inner) => Some(inner),
_ => None,
}
}
}
impl de::Error for Error {
fn custom<T: Display>(msg: T) -> Error {
Error::DeserializationError {
message: msg.to_string(),
}
}
}
pub type Result<T> = std::result::Result<T, Error>;
impl Bson {
pub(crate) fn as_unexpected(&self) -> Unexpected {
match self {
Bson::Array(_) => Unexpected::Seq,
Bson::Binary(b) => Unexpected::Bytes(b.bytes.as_slice()),
Bson::Boolean(b) => Unexpected::Bool(*b),
Bson::DbPointer(_) => Unexpected::Other("dbpointer"),
Bson::Document(_) => Unexpected::Map,
Bson::Double(f) => Unexpected::Float(*f),
Bson::Int32(i) => Unexpected::Signed(*i as i64),
Bson::Int64(i) => Unexpected::Signed(*i),
Bson::JavaScriptCode(_) => Unexpected::Other("javascript code"),
Bson::JavaScriptCodeWithScope(_) => Unexpected::Other("javascript code with scope"),
Bson::MaxKey => Unexpected::Other("maxkey"),
Bson::MinKey => Unexpected::Other("minkey"),
Bson::Null => Unexpected::Unit,
Bson::Undefined => Unexpected::Other("undefined"),
Bson::ObjectId(_) => Unexpected::Other("objectid"),
Bson::RegularExpression(_) => Unexpected::Other("regex"),
Bson::String(s) => Unexpected::Str(s.as_str()),
Bson::Symbol(_) => Unexpected::Other("symbol"),
Bson::Timestamp(_) => Unexpected::Other("timestamp"),
Bson::DateTime(_) => Unexpected::Other("datetime"),
Bson::Decimal128(_) => Unexpected::Other("decimal128"),
}
}
}