Skip to content

Conversation

isabelatkinson
Copy link
Contributor

@isabelatkinson isabelatkinson commented Jun 4, 2025

RUST-1406

This PR introduces crate-wide Error and Result types, which are modeled on the equivalent types in the driver, and does the first pass of error conversions for the Document and RawDocument getter methods. I also did a bit of documentation cleanup for the Document methods. I will follow up with more PRs to port over the rest of the error types and finalize the design/documentation of the new types.

3
]
}"#;
let doc_display_pretty_expectation = "{\n \"hello\": [\n 1, \n 2, \n 3\n ]\n}";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unrelated, but rustfmt was deleting whitespace at the end of lines within the multi-line string literals (which is very bad behavior), so I rewrote them as single-line strings.

src/document.rs Outdated
pub fn is_null(&self, key: impl AsRef<str>) -> bool {
self.get(key) == Some(&Bson::Null)
/// Returns the unit type if the given key corresponds to a [`Bson::Null`] value.
pub fn get_null(&self, key: impl AsRef<str>) -> Result<()> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

breaking change here, there's no real value to return but I thought it would be useful to have the same error information as the rest of the getter methods.

Copy link
Contributor

Choose a reason for hiding this comment

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

Reasonable! Nit: I'd lean towards returning a Bson::Null in case the user was stuffing the return value back into a Bson enum or something like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense - updated

@isabelatkinson isabelatkinson marked this pull request as ready for review June 4, 2025 17:01
@isabelatkinson isabelatkinson requested a review from a team as a code owner June 4, 2025 17:01
@isabelatkinson isabelatkinson requested a review from abr-egn June 4, 2025 17:01
abr-egn
abr-egn previously approved these changes Jun 5, 2025
Copy link
Contributor

@abr-egn abr-egn left a comment

Choose a reason for hiding this comment

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

LGTM! Doc cleanup is very nice.

src/document.rs Outdated
pub fn is_null(&self, key: impl AsRef<str>) -> bool {
self.get(key) == Some(&Bson::Null)
/// Returns the unit type if the given key corresponds to a [`Bson::Null`] value.
pub fn get_null(&self, key: impl AsRef<str>) -> Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Reasonable! Nit: I'd lean towards returning a Bson::Null in case the user was stuffing the return value back into a Bson enum or something like that.

#[non_exhaustive]
ValueAccess {
/// The key of the value.
key: String,
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if it's worth hoisting this to be an optional element of Error; I suspect key is relevant data for more types of failure than just this. Probably this'll become clear as the conversion continues :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes that could definitely be useful - I'll make the change in a future PR if it comes up!

@isabelatkinson isabelatkinson merged commit d1aaa8e into mongodb:main Jun 5, 2025
11 checks passed
@isabelatkinson isabelatkinson deleted the error-types branch June 5, 2025 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants