Skip to content

Commit

Permalink
chore: resolve review comments + clippy error
Browse files Browse the repository at this point in the history
  • Loading branch information
bconn98 committed Jul 10, 2024
1 parent ad20abc commit 5b34937
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
49 changes: 31 additions & 18 deletions src/append/rolling_file/policy/compound/trigger/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ use crate::append::rolling_file::{policy::compound::trigger::Trigger, LogFile};
#[cfg(feature = "config_parsing")]
use crate::config::{Deserialize, Deserializers};

macro_rules! try_from {
($func: ident, $para: expr, $interval: expr) => {
Duration::$func($para).ok_or(TimeTrigerError::TooLargeInterval($interval))?
};
}

#[cfg(feature = "config_parsing")]
/// Configuration for the time trigger.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default, serde::Deserialize)]
Expand Down Expand Up @@ -79,9 +73,17 @@ impl Default for TimeTriggerInterval {
}

#[derive(Debug, Error)]
enum TimeTrigerError {
#[error("The integer value {0:?} for the specified time trigger interval is too large, it must be less than 9,223,372,036,854,775,807 seconds.")]
TooLargeInterval(TimeTriggerInterval),
enum TimeTrigerIntervalError {
#[error("The 'Seconds' value specified as a time trigger is out of bounds, ensure it fits within an i64: : '{0:?}'")]
Second(TimeTriggerInterval),
#[error("The 'Minutes' value specified as a time trigger is out of bounds, ensure it fits within an i64: : '{0:?}'")]
Minute(TimeTriggerInterval),
#[error("The 'Hours' value specified as a time trigger is out of bounds, ensure it fits within an i64: : '{0:?}'")]
Hour(TimeTriggerInterval),
#[error("The 'Days' value specified as a time trigger is out of bounds, ensure it fits within an i64: : '{0:?}'")]
Day(TimeTriggerInterval),
#[error("The 'Weeks' value specified as a time trigger is out of bounds, ensure it fits within an i64: : '{0:?}'")]
Week(TimeTriggerInterval),
}

#[cfg(feature = "config_parsing")]
Expand Down Expand Up @@ -201,10 +203,16 @@ impl TimeTrigger {

#[cfg(not(test))]
let current = Local::now();
let next_time =
TimeTrigger::get_next_time(current, config.interval, config.modulate).unwrap();
let next_time = match TimeTrigger::get_next_time(current, config.interval, config.modulate)
{
Ok(next_time) => next_time,
Err(err) => panic!("{}", err),
};
let next_roll_time = if config.max_random_delay > 0 {
let random_delay = rand::thread_rng().gen_range(0..config.max_random_delay);
// This is a valid unwrap because chrono::Duration::try_milliseconds accepts an i64
// and we're providing a known valid value. We can trust the Option will always return
// us a valid number.
next_time
+ Duration::try_seconds(random_delay as i64)
.unwrap_or(Duration::try_milliseconds(i64::MAX).unwrap())
Expand All @@ -222,7 +230,7 @@ impl TimeTrigger {
current: DateTime<Local>,
interval: TimeTriggerInterval,
modulate: bool,
) -> Result<DateTime<Local>, TimeTrigerError> {
) -> Result<DateTime<Local>, TimeTrigerIntervalError> {
let year = current.year();
if let TimeTriggerInterval::Year(n) = interval {
let n = n as i32;
Expand Down Expand Up @@ -251,16 +259,18 @@ impl TimeTrigger {
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 };
let dur =
try_from!(try_weeks, increment, interval) - try_from!(try_days, weekday, interval);
let dur = Duration::try_weeks(increment)
.ok_or(TimeTrigerIntervalError::Week(interval))?
- Duration::try_days(weekday).ok_or(TimeTrigerIntervalError::Day(interval))?;
return Ok(time + dur);
}

if let TimeTriggerInterval::Day(n) = interval {
let ordinal0 = current.ordinal0() as i64;
let time = Local.with_ymd_and_hms(year, month, day, 0, 0, 0).unwrap();
let increment = if modulate { n - ordinal0 % n } else { n };
let dur = try_from!(try_days, increment, interval);
let dur =
Duration::try_days(increment).ok_or(TimeTrigerIntervalError::Day(interval))?;
return Ok(time + dur);
}

Expand All @@ -270,7 +280,8 @@ impl TimeTrigger {
.with_ymd_and_hms(year, month, day, hour, 0, 0)
.unwrap();
let increment = if modulate { n - (hour as i64) % n } else { n };
let dur = try_from!(try_hours, increment, interval);
let dur =
Duration::try_hours(increment).ok_or(TimeTrigerIntervalError::Hour(interval))?;
return Ok(time + dur);
}

Expand All @@ -280,7 +291,8 @@ impl TimeTrigger {
.with_ymd_and_hms(year, month, day, hour, min, 0)
.unwrap();
let increment = if modulate { n - (min as i64) % n } else { n };
let dur = try_from!(try_minutes, increment, interval);
let dur = Duration::try_minutes(increment)
.ok_or(TimeTrigerIntervalError::Minute(interval))?;
return Ok(time + dur);
}

Expand All @@ -290,7 +302,8 @@ impl TimeTrigger {
.with_ymd_and_hms(year, month, day, hour, min, sec)
.unwrap();
let increment = if modulate { n - (sec as i64) % n } else { n };
let dur = try_from!(try_seconds, increment, interval);
let dur = Duration::try_seconds(increment)
.ok_or(TimeTrigerIntervalError::Second(interval))?;
return Ok(time + dur);
}
panic!("Should not reach here!");
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
//! For more examples see the [examples](https://github.com/estk/log4rs/tree/main/examples).
//!
#![allow(where_clauses_object_safety, clippy::manual_non_exhaustive)]
#![allow(clippy::manual_non_exhaustive)]
#![warn(missing_docs)]

use std::{
Expand Down

0 comments on commit 5b34937

Please sign in to comment.