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
131
132
133
134
135
136
137
138
139
use std::str::Utf8Error;
use crate::spec::ElementType;
#[derive(Debug, PartialEq, Clone)]
#[non_exhaustive]
pub struct Error {
pub kind: ErrorKind,
pub(crate) key: Option<String>,
}
impl Error {
pub(crate) fn new_with_key(key: impl Into<String>, kind: ErrorKind) -> Self {
Self {
kind,
key: Some(key.into()),
}
}
pub(crate) fn new_without_key(kind: ErrorKind) -> Self {
Self { key: None, kind }
}
pub(crate) fn with_key(mut self, key: impl AsRef<str>) -> Self {
self.key = Some(key.as_ref().to_string());
self
}
pub fn key(&self) -> Option<&str> {
self.key.as_deref()
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum ErrorKind {
#[non_exhaustive]
MalformedValue { message: String },
Utf8EncodingError(Utf8Error),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let p = self
.key
.as_ref()
.map(|k| format!("error at key \"{}\": ", k));
let prefix = p.as_ref().map_or("", |p| p.as_str());
match &self.kind {
ErrorKind::MalformedValue { message } => {
write!(f, "{}malformed value: {:?}", prefix, message)
}
ErrorKind::Utf8EncodingError(e) => write!(f, "{}utf-8 encoding error: {}", prefix, e),
}
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;
pub(crate) fn try_with_key<G, F: FnOnce() -> Result<G>>(key: impl AsRef<str>, f: F) -> Result<G> {
f().map_err(|e| e.with_key(key))
}
pub type ValueAccessResult<T> = std::result::Result<T, ValueAccessError>;
#[derive(Debug, PartialEq, Clone)]
#[non_exhaustive]
pub struct ValueAccessError {
pub kind: ValueAccessErrorKind,
pub(crate) key: String,
}
impl ValueAccessError {
pub fn key(&self) -> &str {
self.key.as_str()
}
}
#[derive(Debug, PartialEq, Clone)]
#[non_exhaustive]
pub enum ValueAccessErrorKind {
NotPresent,
#[non_exhaustive]
UnexpectedType {
expected: ElementType,
actual: ElementType,
},
InvalidBson(super::Error),
}
impl std::fmt::Display for ValueAccessError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let prefix = format!("error at key: \"{}\": ", self.key);
match &self.kind {
ValueAccessErrorKind::UnexpectedType { actual, expected } => write!(
f,
"{} unexpected element type: {:?}, expected: {:?}",
prefix, actual, expected
),
ValueAccessErrorKind::InvalidBson(error) => {
write!(f, "{}: {}", prefix, error)
}
ValueAccessErrorKind::NotPresent => write!(f, "{}value not present", prefix),
}
}
}
impl std::error::Error for ValueAccessError {}