Skip to content

Commit

Permalink
refactor: make Repetition enum more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkrapido committed Nov 1, 2023
1 parent 09858b2 commit d125467
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/models/budget/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl StepBudget {
}

pub fn initialize(&mut self, budget_start: NaiveDateTime, budget_end: NaiveDateTime) {
let mut repetition: Repetition = Repetition::Weekly(1);
let mut repetition: Repetition = Repetition::WEEKLY(1);
match self.step_budget_type {
BudgetType::Weekly => (),
BudgetType::Daily => repetition = Repetition::DAILY(1),
Expand Down
61 changes: 37 additions & 24 deletions src/models/repetition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,32 @@ use std::fmt;
/// determine how many steps to generate from a goal.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Repetition {
DAILY(usize),
HOURLY,
Weekly(usize),
WEEKDAYS,
WEEKENDS,
EveryXdays(usize),
EveryXhours(usize),

MONDAYS,
TUESDAYS,
WEDNESDAYS,
THURSDAYS,
FRIDAYS,
SATURDAYS,
SUNDAYS,
FlexDaily(usize, usize),
FlexWeekly(usize, usize),

WEEKDAYS,
WEEKENDS,

HOURLY,
DAILY(usize),
WEEKLY(usize),

#[allow(non_camel_case_types)]
EVERY_X_HOURS(usize),
#[allow(non_camel_case_types)]
EVERY_X_DAYS(usize),

#[allow(non_camel_case_types)]
FLEX_DAILY(usize, usize),
#[allow(non_camel_case_types)]
FLEX_WEEKLY(usize, usize),

}

//How to implement serde deserialize: https://serde.rs/impl-deserialize.html
Expand All @@ -47,18 +57,21 @@ impl<'de> Visitor<'de> for RepetitionVisitor {
E: de::Error,
{
match s {
"daily" => Ok(Repetition::DAILY(1)),
"hourly" => Ok(Repetition::HOURLY),
"weekly" => Ok(Repetition::Weekly(1)),
"weekdays" => Ok(Repetition::WEEKDAYS),
"weekends" => Ok(Repetition::WEEKENDS),
"mondays" => Ok(Repetition::MONDAYS),
"tuesdays" => Ok(Repetition::TUESDAYS),
"wednesdays" => Ok(Repetition::WEDNESDAYS),
"thursdays" => Ok(Repetition::THURSDAYS),
"fridays" => Ok(Repetition::FRIDAYS),
"saturdays" => Ok(Repetition::SATURDAYS),
"sundays" => Ok(Repetition::SUNDAYS),

"daily" => Ok(Repetition::DAILY(1)),
"hourly" => Ok(Repetition::HOURLY),
"weekly" => Ok(Repetition::WEEKLY(1)),

"weekdays" => Ok(Repetition::WEEKDAYS),
"weekends" => Ok(Repetition::WEEKENDS),

_ => {
if s.contains('-') && s.contains('/') {
//e.g. '3-5/week'
Expand All @@ -73,8 +86,8 @@ impl<'de> Visitor<'de> for RepetitionVisitor {
.parse::<usize>()
.expect("expected format to be x-y/period"); //e.g. 5
match rep {
"week" => Ok(Repetition::FlexWeekly(min, max)),
"day" => Ok(Repetition::FlexDaily(min, max)),
"week" => Ok(Repetition::FLEX_WEEKLY(min, max)),
"day" => Ok(Repetition::FLEX_DAILY(min, max)),
_ => panic!("unrecognized repetition: {}", rep),
}
} else if s.contains('/') {
Expand All @@ -84,7 +97,7 @@ impl<'de> Visitor<'de> for RepetitionVisitor {
.parse::<usize>()
.expect("expected format to be x/period");
match split[1] {
"week" => Ok(Repetition::Weekly(num)),
"week" => Ok(Repetition::WEEKLY(num)),
"day" => Ok(Repetition::DAILY(num)),
_ => panic!("unrecognized repetition: {}", s),
}
Expand All @@ -96,9 +109,9 @@ impl<'de> Visitor<'de> for RepetitionVisitor {
.expect("front end should use format 'every x days' or 'every x hours' ");
let rep = split[2];
if rep == "days" {
Ok(Repetition::EveryXdays(num))
Ok(Repetition::EVERY_X_DAYS(num))
} else if rep == "hours" {
Ok(Repetition::EveryXhours(num))
Ok(Repetition::EVERY_X_HOURS(num))
} else {
panic!("front end should use format 'every x days' or 'every x hours' ");
}
Expand Down Expand Up @@ -129,20 +142,20 @@ impl fmt::Display for Repetition {
f.write_str(match *self {
Repetition::DAILY(_) => "DAILY",
Repetition::HOURLY => "HOURLY",
Repetition::Weekly(_) => "Weekly",
Repetition::WEEKLY(_) => "Weekly",
Repetition::WEEKDAYS => "WEEKDAYS",
Repetition::WEEKENDS => "WEEKENDS",
Repetition::EveryXdays(_) => "EveryXdays",
Repetition::EveryXhours(_) => "EveryXhours",
Repetition::EVERY_X_DAYS(_) => "EveryXdays",
Repetition::EVERY_X_HOURS(_) => "EveryXhours",
Repetition::MONDAYS => "Mon",
Repetition::TUESDAYS => "Tue",
Repetition::WEDNESDAYS => "Wed",
Repetition::THURSDAYS => "Thu",
Repetition::FRIDAYS => "Fri",
Repetition::SATURDAYS => "Sat",
Repetition::SUNDAYS => "Sun",
Repetition::FlexDaily(_, _) => "FlexDaily",
Repetition::FlexWeekly(_, _) => "FlexWeekly",
Repetition::FLEX_DAILY(_, _) => "FlexDaily",
Repetition::FLEX_WEEKLY(_, _) => "FlexWeekly",
})
}
}
10 changes: 5 additions & 5 deletions src/models/slots_iterator/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn get_start_of_repeat_step(
match repeat {
Repetition::DAILY(_) => result.checked_add_days(Days::new(1)).unwrap(),
Repetition::HOURLY => result.checked_add_signed(Duration::hours(1)).unwrap(),
Repetition::Weekly(_) => next_week(&result),
Repetition::WEEKLY(_) => next_week(&result),
Repetition::WEEKDAYS => match result.weekday() {
Weekday::Sat => result
.checked_add_days(Days::new(2))
Expand Down Expand Up @@ -78,7 +78,7 @@ pub fn get_start_of_repeat_step(
}
panic!("Shouldn't reach this");
}
Repetition::EveryXdays(day_interval) => result
Repetition::EVERY_X_DAYS(day_interval) => result
.checked_add_days(Days::new(day_interval.try_into().unwrap()))
.unwrap()
.with_hour(0)
Expand All @@ -87,7 +87,7 @@ pub fn get_start_of_repeat_step(
.unwrap()
.with_second(0)
.unwrap(),
Repetition::EveryXhours(hour_interval) => result
Repetition::EVERY_X_HOURS(hour_interval) => result
.checked_add_signed(Duration::hours(hour_interval.try_into().unwrap()))
.unwrap(),
Repetition::MONDAYS => {
Expand Down Expand Up @@ -272,8 +272,8 @@ pub fn get_start_of_repeat_step(
}
panic!("Shouldn't reach this");
}
Repetition::FlexDaily(_, _) => todo!(),
Repetition::FlexWeekly(_, _) => todo!(),
Repetition::FLEX_DAILY(_, _) => todo!(),
Repetition::FLEX_WEEKLY(_, _) => todo!(),
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/services/budgeting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl Goal {
if budget.step_budget_type == BudgetType::Daily {
goal.repeat = Some(Repetition::DAILY(repeatance));
} else {
goal.repeat = Some(Repetition::Weekly(repeatance));
goal.repeat = Some(Repetition::WEEKLY(repeatance));
}
} else {
// get goal.budgets:
Expand All @@ -212,7 +212,7 @@ impl Goal {
if found_daily_budget {
goal.repeat = Some(Repetition::DAILY(repeatance));
} else {
goal.repeat = Some(Repetition::Weekly(repeatance));
goal.repeat = Some(Repetition::WEEKLY(repeatance));
}
}
}
Expand Down Expand Up @@ -277,7 +277,7 @@ mod tests {
min: Some(5),
max: None,
}]),
repeat: Some(Repetition::Weekly(1)),
repeat: Some(Repetition::WEEKLY(1)),
start: Some(calendar.start),
deadline: Some(calendar.end),
tags: vec![Tag::Budget],
Expand Down Expand Up @@ -354,7 +354,7 @@ mod tests {
min: Some(9),
max: None,
}]),
repeat: Some(Repetition::Weekly(1)),
repeat: Some(Repetition::WEEKLY(1)),
start: Some(calendar.start),
deadline: Some(calendar.end),
tags: vec![Tag::Budget],
Expand Down Expand Up @@ -450,7 +450,7 @@ mod tests {
min: Some(5),
max: None,
}]),
repeat: Some(Repetition::Weekly(1)),
repeat: Some(Repetition::WEEKLY(1)),
start: Some(calendar.start),
deadline: Some(calendar.end),
tags: vec![Tag::Budget],
Expand All @@ -469,7 +469,7 @@ mod tests {
min: Some(3),
max: None,
}]),
repeat: Some(Repetition::Weekly(1)),
repeat: Some(Repetition::WEEKLY(1)),
start: Some(calendar.start),
deadline: Some(calendar.end),
tags: vec![Tag::Budget],
Expand Down Expand Up @@ -569,7 +569,7 @@ mod tests {
let budgets = Some(vec![budget]);
goal.budgets = budgets.clone();

let repeat = Some(Repetition::Weekly(1));
let repeat = Some(Repetition::WEEKLY(1));
goal.repeat = repeat;

goal.configure_repeatance(None);
Expand All @@ -593,7 +593,7 @@ mod tests {

goal.configure_repeatance(None);

let expected_repeat = Some(Repetition::Weekly(1));
let expected_repeat = Some(Repetition::WEEKLY(1));

assert_eq!(goal.repeat, expected_repeat);
assert_eq!(goal.budgets, budgets);
Expand Down
8 changes: 4 additions & 4 deletions src/services/preprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ fn populate_goal_dates(
fn generate_flex_weekly_goals(goals: &mut GoalsMap) {
let mut generated_goals: GoalsMap = BTreeMap::new();
for (goal_id, goal) in goals.iter_mut() {
if let Some(Repetition::FlexWeekly(min, max)) = goal.repeat {
if let Some(Repetition::FLEX_WEEKLY(min, max)) = goal.repeat {
//Flex repeat goals are handled as follows:
//If given a goal with 3-5x/week, create 3 goals and 2 extra optional goals
goal.repeat = Some(Repetition::Weekly(1));
goal.repeat = Some(Repetition::WEEKLY(1));

// Create repeated goals and optional repeated goals
for number in 1..max {
Expand Down Expand Up @@ -205,7 +205,7 @@ mod tests {

let mut input_goal = Goal::mock("1", "side project", goal_dates);
input_goal.min_duration = Some(8);
input_goal.repeat = Some(Repetition::FlexWeekly(1, 3));
input_goal.repeat = Some(Repetition::FLEX_WEEKLY(1, 3));

let mut input_goals: GoalsMap = BTreeMap::new();
input_goals.insert(input_goal.id.clone(), input_goal);
Expand All @@ -214,7 +214,7 @@ mod tests {

let mut expected_goal_1 = Goal::mock("1", "side project", goal_dates);
expected_goal_1.min_duration = Some(8);
expected_goal_1.repeat = Some(Repetition::Weekly(1));
expected_goal_1.repeat = Some(Repetition::WEEKLY(1));

let mut expected_goal_2 = expected_goal_1.clone();
expected_goal_2.id = "1-repeat-opt-1".to_string();
Expand Down

0 comments on commit d125467

Please sign in to comment.