Skip to content

Commit 431d448

Browse files
RUST-1406 Convert serde errors to standard error type (#562)
1 parent 3ee9858 commit 431d448

File tree

20 files changed

+419
-633
lines changed

20 files changed

+419
-633
lines changed

src/bson.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,36 @@ impl Bson {
911911
.collect(),
912912
)
913913
}
914+
915+
/// Method for converting a given [`Bson`] value to a [`serde::de::Unexpected`] for error
916+
/// reporting.
917+
#[cfg(feature = "serde")]
918+
pub(crate) fn as_unexpected(&self) -> serde::de::Unexpected {
919+
use serde::de::Unexpected;
920+
match self {
921+
Bson::Array(_) => Unexpected::Seq,
922+
Bson::Binary(b) => Unexpected::Bytes(b.bytes.as_slice()),
923+
Bson::Boolean(b) => Unexpected::Bool(*b),
924+
Bson::DbPointer(_) => Unexpected::Other("dbpointer"),
925+
Bson::Document(_) => Unexpected::Map,
926+
Bson::Double(f) => Unexpected::Float(*f),
927+
Bson::Int32(i) => Unexpected::Signed(*i as i64),
928+
Bson::Int64(i) => Unexpected::Signed(*i),
929+
Bson::JavaScriptCode(_) => Unexpected::Other("javascript code"),
930+
Bson::JavaScriptCodeWithScope(_) => Unexpected::Other("javascript code with scope"),
931+
Bson::MaxKey => Unexpected::Other("maxkey"),
932+
Bson::MinKey => Unexpected::Other("minkey"),
933+
Bson::Null => Unexpected::Unit,
934+
Bson::Undefined => Unexpected::Other("undefined"),
935+
Bson::ObjectId(_) => Unexpected::Other("objectid"),
936+
Bson::RegularExpression(_) => Unexpected::Other("regex"),
937+
Bson::String(s) => Unexpected::Str(s.as_str()),
938+
Bson::Symbol(_) => Unexpected::Other("symbol"),
939+
Bson::Timestamp(_) => Unexpected::Other("timestamp"),
940+
Bson::DateTime(_) => Unexpected::Other("datetime"),
941+
Bson::Decimal128(_) => Unexpected::Other("decimal128"),
942+
}
943+
}
914944
}
915945

916946
/// Value helpers

src/datetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use crate::error::{Error, Result};
7070
/// chrono_datetime: chrono::DateTime<chrono::Utc>,
7171
/// }
7272
///
73-
/// # fn main() -> bson::ser::Result<()> {
73+
/// # fn main() -> bson::error::Result<()> {
7474
/// let f = Foo { date_time: bson::DateTime::now(), chrono_datetime: chrono::Utc::now() };
7575
/// println!("{:?}", bson::serialize_to_document(&f)?);
7676
/// # Ok(())

src/de.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,16 @@
2121

2222
//! Deserializer
2323
24-
mod error;
2524
mod raw;
2625
mod serde;
2726

28-
pub use self::{
29-
error::{Error, Result},
30-
serde::Deserializer,
31-
};
27+
pub use self::serde::Deserializer;
3228

3329
use std::io::Read;
3430

3531
use crate::{
3632
bson::{Bson, Document},
33+
error::{Error, Result},
3734
raw::reader_to_vec,
3835
spec::BinarySubtype,
3936
};

src/de/error.rs

Lines changed: 0 additions & 152 deletions
This file was deleted.

src/de/raw.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'de> Deserializer<'de> {
6161
}
6262

6363
fn value(&self) -> Result<RawBsonRef<'de>> {
64-
Ok(self.element.value()?)
64+
self.element.value()
6565
}
6666

6767
/// Deserialize the element, using the type of the element along with the
@@ -419,7 +419,7 @@ impl<'de> serde::de::EnumAccess<'de> for DocumentAccess<'de> {
419419
self.advance()?;
420420
let elem = match &self.elem {
421421
Some(e) => e,
422-
None => return Err(Error::EndOfStream),
422+
None => return Err(Error::end_of_stream()),
423423
};
424424
let de: BorrowedStrDeserializer<'_, Error> = BorrowedStrDeserializer::new(elem.key());
425425
let key = seed.deserialize(de)?;
@@ -1077,7 +1077,7 @@ impl<'de> serde::de::MapAccess<'de> for CodeWithScopeAccess<'de> {
10771077
self.stage = match self.stage {
10781078
CodeWithScopeDeserializationStage::Code => CodeWithScopeDeserializationStage::Scope,
10791079
CodeWithScopeDeserializationStage::Scope => CodeWithScopeDeserializationStage::Done,
1080-
CodeWithScopeDeserializationStage::Done => return Err(Error::EndOfStream),
1080+
CodeWithScopeDeserializationStage::Done => return Err(Error::end_of_stream()),
10811081
};
10821082
Ok(value)
10831083
}
@@ -1105,7 +1105,7 @@ impl<'de> serde::de::Deserializer<'de> for &CodeWithScopeAccess<'de> {
11051105
_ => visitor.visit_map(DocumentAccess::new(scope, self.options.clone())?),
11061106
}
11071107
}
1108-
CodeWithScopeDeserializationStage::Done => Err(Error::EndOfStream),
1108+
CodeWithScopeDeserializationStage::Done => Err(Error::end_of_stream()),
11091109
}
11101110
}
11111111

0 commit comments

Comments
 (0)