Skip to content

RUST-1406 Convert raw errors to standard error type #552

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 3 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
6 changes: 3 additions & 3 deletions src/de/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ impl From<string::FromUtf8Error> for Error {
}
}

impl From<crate::raw::Error> for Error {
fn from(value: crate::raw::Error) -> Self {
Self::deserialization(value)
impl From<crate::error::Error> for Error {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a temp change before de::Error is removed

fn from(error: crate::error::Error) -> Self {
Self::deserialization(error.to_string())
}
}

Expand Down
104 changes: 52 additions & 52 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,12 @@ impl Document {
match self.get(key) {
Some(&Bson::Double(v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::Double,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a mutable reference to the value for the given key if one is present and is of type
Expand All @@ -267,12 +267,12 @@ impl Document {
match self.get_mut(key) {
Some(&mut Bson::Double(ref mut v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::Double,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a reference to the value for the given key if one is present and is of type
Expand All @@ -282,12 +282,12 @@ impl Document {
match self.get(key) {
Some(Bson::Decimal128(v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::Decimal128,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a mutable reference to the value for the given key if one is present and is of type
Expand All @@ -297,42 +297,42 @@ impl Document {
match self.get_mut(key) {
Some(&mut Bson::Decimal128(ref mut v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::Decimal128,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a reference to the value for the given key if one is present and is of type
/// [`ElementType::String`].
pub fn get_str(&self, key: impl AsRef<str>) -> Result<&str> {
let key = key.as_ref();
match self.get(key) {
Some(Bson::String(v)) => Ok(v),
Some(Bson::String(v)) => Ok(v.as_str()),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::String,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a mutable reference to the value for the given key if one is present and is of type
/// [`ElementType::String`].
pub fn get_str_mut(&mut self, key: impl AsRef<str>) -> Result<&mut str> {
let key = key.as_ref();
match self.get_mut(key) {
Some(&mut Bson::String(ref mut v)) => Ok(v),
Some(&mut Bson::String(ref mut v)) => Ok(v.as_mut_str()),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::String,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a reference to the value for the given key if one is present and is of type
Expand All @@ -342,12 +342,12 @@ impl Document {
match self.get(key) {
Some(Bson::Array(v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::Array,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a mutable reference to the value for the given key if one is present and is of type
Expand All @@ -357,12 +357,12 @@ impl Document {
match self.get_mut(key) {
Some(&mut Bson::Array(ref mut v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::Array,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a reference to the value for the given key if one is present and is of type
Expand All @@ -372,12 +372,12 @@ impl Document {
match self.get(key) {
Some(Bson::Document(v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::EmbeddedDocument,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a mutable reference to the value for the given key if one is present and is of type
Expand All @@ -387,12 +387,12 @@ impl Document {
match self.get_mut(key) {
Some(&mut Bson::Document(ref mut v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::EmbeddedDocument,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a reference to the value for the given key if one is present and is of type
Expand All @@ -402,12 +402,12 @@ impl Document {
match self.get(key) {
Some(&Bson::Boolean(v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::Boolean,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a mutable reference to the value for the given key if one is present and is of type
Expand All @@ -417,12 +417,12 @@ impl Document {
match self.get_mut(key) {
Some(&mut Bson::Boolean(ref mut v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::Boolean,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns [`Bson::Null`] if the given key corresponds to a [`Bson::Null`] value.
Expand All @@ -431,12 +431,12 @@ impl Document {
match self.get(key) {
Some(&Bson::Null) => Ok(Bson::Null),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::Null,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns the value for the given key if one is present and is of type [`ElementType::Int32`].
Expand All @@ -445,12 +445,12 @@ impl Document {
match self.get(key) {
Some(&Bson::Int32(v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::Int32,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a mutable reference to the value for the given key if one is present and is of type
Expand All @@ -460,12 +460,12 @@ impl Document {
match self.get_mut(key) {
Some(&mut Bson::Int32(ref mut v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::Int32,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns the value for the given key if one is present and is of type [`ElementType::Int64`].
Expand All @@ -474,12 +474,12 @@ impl Document {
match self.get(key) {
Some(&Bson::Int64(v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::Int64,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a mutable reference to the value for the given key if one is present and is of type
Expand All @@ -489,12 +489,12 @@ impl Document {
match self.get_mut(key) {
Some(&mut Bson::Int64(ref mut v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::Int64,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns the value for the given key if one is present and is of type
Expand All @@ -504,12 +504,12 @@ impl Document {
match self.get(key) {
Some(&Bson::Timestamp(timestamp)) => Ok(timestamp),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::Timestamp,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a mutable reference to the value for the given key if one is present and is of type
Expand All @@ -519,12 +519,12 @@ impl Document {
match self.get_mut(key) {
Some(&mut Bson::Timestamp(ref mut timestamp)) => Ok(timestamp),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::Timestamp,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a reference to the value for the given key if one is present and is of type
Expand All @@ -537,12 +537,12 @@ impl Document {
ref bytes,
})) => Ok(bytes),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::Binary,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a mutable reference to the value for the given key if one is present and is of type
Expand All @@ -555,12 +555,12 @@ impl Document {
ref mut bytes,
})) => Ok(bytes),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::Binary,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns the value for the given key if one is present and is of type
Expand All @@ -570,12 +570,12 @@ impl Document {
match self.get(key) {
Some(&Bson::ObjectId(v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::ObjectId,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a mutable reference to the value for the given key if one is present and is of type
Expand All @@ -585,12 +585,12 @@ impl Document {
match self.get_mut(key) {
Some(&mut Bson::ObjectId(ref mut v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::ObjectId,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a reference to the value for the given key if one is present and is of type
Expand All @@ -600,12 +600,12 @@ impl Document {
match self.get(key) {
Some(Bson::DateTime(v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::DateTime,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns a mutable reference to the value for the given key if one is present and is of type
Expand All @@ -615,12 +615,12 @@ impl Document {
match self.get_mut(key) {
Some(&mut Bson::DateTime(ref mut v)) => Ok(v),
Some(bson) => Err(Error::value_access_unexpected_type(
key,
bson.element_type(),
ElementType::DateTime,
)),
None => Err(Error::value_access_not_present(key)),
None => Err(Error::value_access_not_present()),
}
.map_err(|e| e.with_key(key))
}

/// Returns whether the map contains a value for the specified key.
Expand Down
Loading