Skip to content

Commit

Permalink
chore: unwrap safe try_days call
Browse files Browse the repository at this point in the history
  • Loading branch information
bconn98 committed Jul 13, 2024
1 parent ef197e5 commit 9acd373
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/append/rolling_file/policy/compound/trigger/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,15 @@ impl TimeTrigger {
let day = current.day();
if let TimeTriggerInterval::Week(n) = interval {
let week0 = current.iso_week().week0() as i64;
let weekday = current.weekday().num_days_from_monday() as i64; // Monday is the first day of the week
let time = Local.with_ymd_and_hms(year, month, day, 0, 0, 0).unwrap();
let increment = if modulate { n - week0 % n } else { n };

// Unwrap instead of propogate the try_days call as this is a known safe value
// generated by the chrono library as a value between 0-6.
let weekday = current.weekday().num_days_from_monday() as i64; // Monday is the first day of the week
let dur = Duration::try_weeks(increment).ok_or(
TimeTriggerIntervalError::OutOfBounds("Weeks".to_owned(), interval),
)? - Duration::try_days(weekday).ok_or(
TimeTriggerIntervalError::OutOfBounds("Days".to_owned(), interval),
)?;
)? - Duration::try_days(weekday).unwrap();
return Ok(time + dur);
}

Expand Down Expand Up @@ -527,7 +528,7 @@ mod test {
}

#[test]
fn test_err() {
fn test_days_overflow() {
let config = TimeTriggerConfig {
interval: TimeTriggerInterval::Day(i64::MAX),
modulate: false,
Expand Down

0 comments on commit 9acd373

Please sign in to comment.