Skip to content

RUST-1406 Convert serde errors to standard error type #562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/bson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,36 @@ impl Bson {
.collect(),
)
}

/// Method for converting a given [`Bson`] value to a [`serde::de::Unexpected`] for error
/// reporting.
#[cfg(feature = "serde")]
pub(crate) fn as_unexpected(&self) -> serde::de::Unexpected {
use serde::de::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"),
}
}
}

/// Value helpers
Expand Down
2 changes: 1 addition & 1 deletion src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ use crate::error::{Error, Result};
/// chrono_datetime: chrono::DateTime<chrono::Utc>,
/// }
///
/// # fn main() -> bson::ser::Result<()> {
/// # fn main() -> bson::error::Result<()> {
/// let f = Foo { date_time: bson::DateTime::now(), chrono_datetime: chrono::Utc::now() };
/// println!("{:?}", bson::serialize_to_document(&f)?);
/// # Ok(())
Expand Down
7 changes: 2 additions & 5 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@

//! Deserializer

mod error;
mod raw;
mod serde;

pub use self::{
error::{Error, Result},
serde::Deserializer,
};
pub use self::serde::Deserializer;

use std::io::Read;

use crate::{
bson::{Bson, Document},
error::{Error, Result},
raw::reader_to_vec,
spec::BinarySubtype,
};
Expand Down
152 changes: 0 additions & 152 deletions src/de/error.rs

This file was deleted.

8 changes: 4 additions & 4 deletions src/de/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'de> Deserializer<'de> {
}

fn value(&self) -> Result<RawBsonRef<'de>> {
Ok(self.element.value()?)
self.element.value()
}

/// Deserialize the element, using the type of the element along with the
Expand Down Expand Up @@ -419,7 +419,7 @@ impl<'de> serde::de::EnumAccess<'de> for DocumentAccess<'de> {
self.advance()?;
let elem = match &self.elem {
Some(e) => e,
None => return Err(Error::EndOfStream),
None => return Err(Error::end_of_stream()),
};
let de: BorrowedStrDeserializer<'_, Error> = BorrowedStrDeserializer::new(elem.key());
let key = seed.deserialize(de)?;
Expand Down Expand Up @@ -1077,7 +1077,7 @@ impl<'de> serde::de::MapAccess<'de> for CodeWithScopeAccess<'de> {
self.stage = match self.stage {
CodeWithScopeDeserializationStage::Code => CodeWithScopeDeserializationStage::Scope,
CodeWithScopeDeserializationStage::Scope => CodeWithScopeDeserializationStage::Done,
CodeWithScopeDeserializationStage::Done => return Err(Error::EndOfStream),
CodeWithScopeDeserializationStage::Done => return Err(Error::end_of_stream()),
};
Ok(value)
}
Expand Down Expand Up @@ -1105,7 +1105,7 @@ impl<'de> serde::de::Deserializer<'de> for &CodeWithScopeAccess<'de> {
_ => visitor.visit_map(DocumentAccess::new(scope, self.options.clone())?),
}
}
CodeWithScopeDeserializationStage::Done => Err(Error::EndOfStream),
CodeWithScopeDeserializationStage::Done => Err(Error::end_of_stream()),
}
}

Expand Down
Loading