Skip to content
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

Convert timestamp methods on DateTime to return Results #1495

Merged
merged 6 commits into from
Mar 8, 2024

Conversation

pitdicker
Copy link
Collaborator

All the preparations are in!
Also adds an ok_or! macro.

cc @Zomtir.

Copy link

codecov bot commented Mar 8, 2024

Codecov Report

Attention: Patch coverage is 84.21053% with 15 lines in your changes are missing coverage. Please review.

Project coverage is 94.04%. Comparing base (b01933e) to head (cc5fbf1).
Report is 7 commits behind head on 0.5.x.

Files Patch % Lines
src/naive/datetime/serde.rs 70.58% 5 Missing ⚠️
src/datetime/serde.rs 78.57% 3 Missing ⚠️
src/format/parsed.rs 0.00% 3 Missing ⚠️
src/offset/mod.rs 50.00% 3 Missing ⚠️
src/datetime/mod.rs 97.14% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            0.5.x    #1495      +/-   ##
==========================================
- Coverage   94.11%   94.04%   -0.08%     
==========================================
  Files          37       37              
  Lines       17125    17033      -92     
==========================================
- Hits        16118    16019      -99     
- Misses       1007     1014       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -217,15 +219,15 @@ fn test_datetime_from_timestamp_nanos() {
let nanos = parsed.timestamp_nanos().unwrap();
assert_eq!(
Some(DateTime::from_timestamp_nanos(nanos)),
Copy link
Contributor

@Zomtir Zomtir Mar 8, 2024

Choose a reason for hiding this comment

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

I'd prefer to do the comparison in the Result domain:

    assert_eq!(
        Ok(DateTime::from_timestamp_nanos(nanos)),
        DateTime::from_timestamp(nanos / A_BILLION, (nanos % A_BILLION) as u32)
    );

@@ -282,7 +282,8 @@ impl<Tz: TimeZone> DateTime<Tz> {
subsec_nanos -= 1_000_000_000;
timestamp += 1;
}
try_opt!(timestamp.checked_mul(1_000_000_000)).checked_add(subsec_nanos)
let ts = try_err!(ok_or!(timestamp.checked_mul(1_000_000_000), Error::OutOfRange));
Copy link
Contributor

Choose a reason for hiding this comment

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

If I remember correctly, we want the value to be unwrapped anyway in all instances. I sugges try_ok_or instead that directly unwraps T or returns the error early.

try_err!(ok_or!(..)) vs try_ok_or!

I don't really mind chaining macros, but wanted to have it mentioned.

@pitdicker pitdicker force-pushed the convert_timestamp branch from f07b60c to d430631 Compare March 8, 2024 08:19
@pitdicker
Copy link
Collaborator Author

Updated to have a to_or macro and used ? in the doctests (starting to get payoffs 😄).

@djc
Copy link
Member

djc commented Mar 8, 2024

Uh, what/where/why is the to_or macro?

@pitdicker
Copy link
Collaborator Author

Sorry, just after your review.

It is a combination of try_err!(ok_or!(..)).

@djc
Copy link
Member

djc commented Mar 8, 2024

I don't like the to_or!() name -- try_ok_or!() looks better to me.

@pitdicker pitdicker force-pushed the convert_timestamp branch from d430631 to afee651 Compare March 8, 2024 08:53
src/datetime/tests.rs Outdated Show resolved Hide resolved
@pitdicker pitdicker force-pushed the convert_timestamp branch from afee651 to cc5fbf1 Compare March 8, 2024 10:03
@pitdicker
Copy link
Collaborator Author

I don't like the to_or!() name -- try_ok_or!() looks better to me.

Yea, that is better.

@pitdicker pitdicker merged commit 6c68842 into chronotope:0.5.x Mar 8, 2024
34 of 35 checks passed
@pitdicker pitdicker deleted the convert_timestamp branch March 8, 2024 12:59
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.

3 participants