Skip to content

RUST-1225 Add base64 string constructor to Binary #365

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

Merged

Conversation

sanav33
Copy link
Contributor

@sanav33 sanav33 commented Jul 5, 2022

Added a Binary constructor that accepts a base64 string instead of the bytes themselves.

@sanav33 sanav33 requested a review from isabelatkinson July 5, 2022 18:37
@sanav33 sanav33 requested a review from isabelatkinson July 11, 2022 18:55
src/bson.rs Outdated
pub fn from_base64(
input: &str,
subtype: impl Into<Option<BinarySubtype>>,
) -> Result<Self, DecodeError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

We tend to avoid including types from other crates in our public API as they don't always have the same stability guarantees that we do. Under semantic versioning, any crate with a version that begins with 0 (e.g. 0.13.0, which is the version of the base64 crate) can make breaking changes at any time -- as an example, base64 could rename DecodeError to DecodingError, which would cause compilation failures for any code that relies upon the name being DecodeError. Because of this we'll want to avoid returning this type directly.

I looked over the various Error types we have defined in the BSON library and I'm not sure any of them are quite what we need for this situation. The closest would be the SerializationError under ser::Error, but that's more so used in the context of serializing to Bson. Alternatively, we could introduce a new Error type for this case.

Thoughts on this @patrickfreed @abr-egn @kmahar?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, the error situation in the bson crate is really tough right now, since we don't have a single error type but rather have individual ones for each and every use case. The two closest precedents for a function like this are Uuid::parse_str and ObjectId::parse_str, and both use their respective type's error/result types, but it's unclear if we want to introduce yet another error type just for Binary values. It also doesn't seem like any of the existing ones match up very well here though.

We could consider introducing a general-purpose BsonError now, though it would only really be used for this purpose, and it might only add to the confusion. I kind of think we have to go with adding a new error case just for Binary values. In 3.0, I think we'll want to do a full redesign of the error hierarchy that eliminates a lot of the individual error types.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed with Patrick on both the immediate thing to do and what we'll want to queue up for 3.0.

Copy link
Contributor

Choose a reason for hiding this comment

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

That sounds good to me. @sanav33 we can walk through what the new error type should look like, I think we'll want to add a binary module for it to live in to be consistent with our other error types. Also filed RUST-1406 to clean this up when we do a 3.0 release.

@sanav33 sanav33 force-pushed the RUST-1225/add-base64-to-binary-method branch from f896bf9 to ced93a7 Compare August 15, 2022 16:07
@sanav33 sanav33 requested a review from isabelatkinson August 15, 2022 16:07
Copy link
Contributor

@isabelatkinson isabelatkinson left a comment

Choose a reason for hiding this comment

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

just a few small comments! looks like there are also some lint failures

#[non_exhaustive]
pub enum Error {
/// While trying to decode from base64, an error was returned.
DecodingError,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be helpful to include some additional information here about what went wrong during decoding, maybe a message field like InvalidUuidString here that stores the stringified version of the base64 error?

src/binary.rs Outdated
@@ -0,0 +1,81 @@
pub mod error;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should either move the stuff in this module directly into this file or make this pub(crate) and add a pub use error::{Error, Result} to be consistent with other types. For example, uuid's Error and Result types are contained in the uuid module rather than within a nested error module.

Copy link
Contributor

@isabelatkinson isabelatkinson left a comment

Choose a reason for hiding this comment

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

looks like there are still some lint errors

@sanav33
Copy link
Contributor Author

sanav33 commented Aug 18, 2022

looks like there are still some lint errors

It looks like those failures are from an unrelated PR (#258). Should I go ahead and fix them anyway? @isabelatkinson

@isabelatkinson
Copy link
Contributor

hmm you probably need to rebase this branch on main, it looks like #366 went in after this PR was created

@sanav33
Copy link
Contributor Author

sanav33 commented Aug 19, 2022

I might be wrong but I think I rebased before the last push to this PR and it looks like all the changes from #366 are reflected in this PR.

@isabelatkinson
Copy link
Contributor

ohh I see what the issue is, chrono must've just released a new version that deprecated some constants. I needed to run cargo update to see the lint errors locally. don't worry about those in this PR, I'll make a separate one to fix.

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!

Copy link
Contributor

@patrickfreed patrickfreed left a comment

Choose a reason for hiding this comment

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

A few minor comments, sorry for not getting them in before Sana left. Do you want to take this over @isabelatkinson?

src/binary.rs Outdated
/// Creates a [`Binary`] from a base64 string and optional [`BinarySubtype`]. If the
/// `subtype` argument is `None`, the [`Binary`] constructed will default to
/// [`BinarySubtype::Generic`].
pub fn from_base64(input: &str, subtype: impl Into<Option<BinarySubtype>>) -> Result<Self> {
Copy link
Contributor

Choose a reason for hiding this comment

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

this &str can be an impl AsRef<str>

impl Binary {
/// Creates a [`Binary`] from a base64 string and optional [`BinarySubtype`]. If the
/// `subtype` argument is `None`, the [`Binary`] constructed will default to
/// [`BinarySubtype::Generic`].
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be good to have a doc example here. I think the test we added for this would actually be pretty good.

@isabelatkinson isabelatkinson changed the title RUST-1225 Add base64 string to Binary constructor RUST-1225 Add base64 string constructor to Binary Nov 29, 2022
@isabelatkinson isabelatkinson force-pushed the RUST-1225/add-base64-to-binary-method branch from cecdc38 to 88943fb Compare November 29, 2022 17:02
@isabelatkinson
Copy link
Contributor

there's an unrelated clippy failure -- going to fix in a separate PR

@isabelatkinson isabelatkinson merged commit 5e0bea6 into mongodb:main Nov 30, 2022
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.

4 participants