Skip to content

Conversation

karmenliang
Copy link
Contributor

RUST-977

Add a method to convert a RFC 3339 formatting string to bson::DateTime even without the chrono feature flag.

@karmenliang karmenliang requested a review from abr-egn October 28, 2021 18:06
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.

Looks good! Just a few minor comments.

src/datetime.rs Outdated
}

/// Convert the given RFC 3339 format string to a [`DateTime`].
pub fn from_rfc3339(s: &str) -> Result<Self, crate::de::Error> {
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be made slightly more convenient for the caller by changing the type of the parameter to impl AsRef<str> (and then using it via s.as_ref() in the line below) - that'll let it accept both &str and String values.

src/datetime.rs Outdated
pub fn from_rfc3339(s: &str) -> Result<Self, crate::de::Error> {
match chrono::DateTime::<chrono::FixedOffset>::parse_from_rfc3339(s) {
Ok(d) => Ok(Self::from_chrono(d)),
Err(_) => Err(crate::de::Error::InvalidTimestamp {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should format the error returned from parse_from_rfc3339 either instead of or as part ofmessage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to returning the error message from parse_from_rfc3339 in 7bcb901.

crate::DateTime::from_rfc3339(rfc).unwrap(),
crate::DateTime::from_chrono(date)
);
}
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'd be good to also have a test to validate failure behavior - doesn't need to assert on the exact error message, just that one is returned for invalid input.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added invalid test cases in 7bcb901.

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.

Looks good! Tagging in the other folks on the team for review.

src/de/error.rs Outdated
InvalidUtf8String(string::FromUtf8Error),

/// An error encountered during the conversion of one datetime format to another.
InvalidTimestamp {
Copy link
Contributor

Choose a reason for hiding this comment

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

rather than reusing the deserialization error here, I wonder if it would be better to introduce another error type for datetimes like we have for ObjectIds (oid::Error, returned from ObjectId::parse_str) and UUIDs (uuid::Error, returned from Uuid::parse_str). It's unfortunate that this crate has so many error types, but I think it's something we probably have to commit to at this point.

cc @abr-egn @isabelatkinson

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah that seems fine to me for the sake of consistency

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added an error enum and switched over to using that instead in 0bd1b00.

src/de/error.rs Outdated
InvalidUtf8String(string::FromUtf8Error),

/// An error encountered during the conversion of one datetime format to another.
InvalidTimestamp {
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah that seems fine to me for the sake of consistency

assert_eq!(
crate::DateTime::parse_rfc3339_str(rfc).unwrap(),
crate::DateTime::from_chrono(date)
);
Copy link
Contributor

Choose a reason for hiding this comment

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

can we add an assertion that converting the datetime back to a string matches the input?

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.

One last suggestion but otherwise LGTM!

/// Errors that can occur during [`DateTime`] construction and generation.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Error {
Copy link
Contributor

Choose a reason for hiding this comment

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

To allow users to refer to this type, we'll need to make the datetime module pub in lib.rs. Then users can say bson::datetime::Result.

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.

lgtm pending patrick's last comment!

@karmenliang karmenliang merged commit d60816d into master Nov 3, 2021
@patrickfreed patrickfreed deleted the RUST-977/rfc-3339-to-bson-datetime branch January 25, 2022 20:47
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