-
Notifications
You must be signed in to change notification settings - Fork 144
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
RUST-1225 Add base64 string constructor to Binary
#365
Conversation
src/bson.rs
Outdated
pub fn from_base64( | ||
input: &str, | ||
subtype: impl Into<Option<BinarySubtype>>, | ||
) -> Result<Self, DecodeError> { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f896bf9
to
ced93a7
Compare
There was a problem hiding this 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
src/binary/error.rs
Outdated
#[non_exhaustive] | ||
pub enum Error { | ||
/// While trying to decode from base64, an error was returned. | ||
DecodingError, |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this 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
It looks like those failures are from an unrelated PR (#258). Should I go ahead and fix them anyway? @isabelatkinson |
hmm you probably need to rebase this branch on main, it looks like #366 went in after this PR was created |
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. |
ohh I see what the issue is, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this 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> { |
There was a problem hiding this comment.
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`]. |
There was a problem hiding this comment.
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.
Binary
cecdc38
to
88943fb
Compare
there's an unrelated clippy failure -- going to fix in a separate PR |
Added a Binary constructor that accepts a base64 string instead of the bytes themselves.