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 NaiveDate/NaiveDateTime::checked_(add/sub)_days to return Result #1475

Merged
merged 1 commit into from
Mar 12, 2024

Conversation

Zomtir
Copy link
Contributor

@Zomtir Zomtir commented Mar 1, 2024

This pull request targets the issue #1469.

  • Adds a try_opt_ok macro to simplify unwrapping Result in an Option context
  • Adds a try_add macro to improve handling of i32::checked_add()
  • Convert NaiveDate/NaiveDateTime::checked_(add/sub)_days to return Result

cc @pitdicker

Copy link

codecov bot commented Mar 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.02%. Comparing base (45d22e8) to head (7e358c3).

Additional details and impacted files
@@            Coverage Diff             @@
##            0.5.x    #1475      +/-   ##
==========================================
- Coverage   94.02%   94.02%   -0.01%     
==========================================
  Files          37       37              
  Lines       16974    16954      -20     
==========================================
- Hits        15960    15941      -19     
+ Misses       1014     1013       -1     

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

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/naive/date/mod.rs Show resolved Hide resolved
src/naive/date/tests.rs Show resolved Hide resolved
src/datetime/mod.rs Outdated Show resolved Hide resolved
@Zomtir
Copy link
Contributor Author

Zomtir commented Mar 4, 2024

Thanks for the feedback, I will take it into account. Just haven't found time so far.

@pitdicker
Copy link
Collaborator

Thank you for the update. There is no hurry.

/// NaiveDate::from_ymd(2022, 7, 31).unwrap().checked_add_days(Days::new(2)),
/// Some(NaiveDate::from_ymd(2022, 8, 2).unwrap())
/// NaiveDate::from_ymd(2022, 2, 20)?.checked_add_days(Days::new(9)),
/// NaiveDate::from_ymd(2022, 3, 1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems like a more interesting example 👍.

@@ -1999,7 +1993,7 @@ impl Iterator for NaiveDateWeeksIterator {

fn next(&mut self) -> Option<Self::Item> {
let current = self.value;
self.value = current.checked_add_days(Days::new(7))?;
self.value = current.checked_add_days(Days::new(7)).ok()?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unrelated to this PR, so just ignore this comment.
I wonder if the NaiveDateWeeksIterator can cover the complete range of NaiveDate, or if it fails one week before the end.

src/naive/date/tests.rs Show resolved Hide resolved
@pitdicker pitdicker merged commit ed5717d into chronotope:0.5.x Mar 12, 2024
35 checks passed
@pitdicker
Copy link
Collaborator

Thank you!

@Zomtir Zomtir deleted the add_days branch April 6, 2024 08:07
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