Skip to content

Convert NaiveDate/NaiveDateTime::checked_(add/sub)_days to return Result#1475

Merged
pitdicker merged 1 commit into
chronotope:0.5.xfrom
Zomtir:add_days
Mar 12, 2024
Merged

Convert NaiveDate/NaiveDateTime::checked_(add/sub)_days to return Result#1475
pitdicker merged 1 commit into
chronotope:0.5.xfrom
Zomtir:add_days

Conversation

@Zomtir

@Zomtir Zomtir commented Mar 1, 2024

Copy link
Copy Markdown
Contributor

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

@codecov

codecov Bot commented Mar 1, 2024

Copy link
Copy Markdown

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.

Comment thread src/lib.rs Outdated
Comment thread src/lib.rs Outdated
Comment thread src/naive/date/mod.rs
Comment thread src/naive/date/tests.rs
Comment thread src/datetime/mod.rs Outdated
@Zomtir

Zomtir commented Mar 4, 2024

Copy link
Copy Markdown
Contributor Author

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

@pitdicker

Copy link
Copy Markdown
Collaborator

Thank you for the update. There is no hurry.

Comment thread src/naive/date/mod.rs
/// 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
Copy Markdown
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 👍.

Comment thread src/naive/date/mod.rs
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
Copy Markdown
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.

Comment thread src/naive/date/tests.rs
@pitdicker pitdicker merged commit ed5717d into chronotope:0.5.x Mar 12, 2024
@pitdicker

Copy link
Copy Markdown
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