diff --git a/src/models/budget/impls.rs b/src/models/budget/impls.rs index 5c8707eb..f346464c 100644 --- a/src/models/budget/impls.rs +++ b/src/models/budget/impls.rs @@ -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), diff --git a/src/models/repetition.rs b/src/models/repetition.rs index fd06d0dc..1fbd27cf 100644 --- a/src/models/repetition.rs +++ b/src/models/repetition.rs @@ -11,13 +11,7 @@ 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, @@ -25,8 +19,24 @@ pub enum Repetition { 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 @@ -47,11 +57,6 @@ 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), @@ -59,6 +64,14 @@ impl<'de> Visitor<'de> for RepetitionVisitor { "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' @@ -73,8 +86,8 @@ impl<'de> Visitor<'de> for RepetitionVisitor { .parse::() .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('/') { @@ -84,7 +97,7 @@ impl<'de> Visitor<'de> for RepetitionVisitor { .parse::() .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), } @@ -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' "); } @@ -129,11 +142,11 @@ 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", @@ -141,8 +154,8 @@ impl fmt::Display for Repetition { Repetition::FRIDAYS => "Fri", Repetition::SATURDAYS => "Sat", Repetition::SUNDAYS => "Sun", - Repetition::FlexDaily(_, _) => "FlexDaily", - Repetition::FlexWeekly(_, _) => "FlexWeekly", + Repetition::FLEX_DAILY(_, _) => "FlexDaily", + Repetition::FLEX_WEEKLY(_, _) => "FlexWeekly", }) } } diff --git a/src/models/slots_iterator/utils.rs b/src/models/slots_iterator/utils.rs index d8d6d8ea..fcd05df3 100644 --- a/src/models/slots_iterator/utils.rs +++ b/src/models/slots_iterator/utils.rs @@ -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)) @@ -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) @@ -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 => { @@ -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!(), } } diff --git a/src/services/budgeting.rs b/src/services/budgeting.rs index 7289e6a0..11d1eae2 100644 --- a/src/services/budgeting.rs +++ b/src/services/budgeting.rs @@ -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: @@ -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)); } } } @@ -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], @@ -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], @@ -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], @@ -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], @@ -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); @@ -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); diff --git a/src/services/preprocess.rs b/src/services/preprocess.rs index 85094d8d..0a7769a2 100644 --- a/src/services/preprocess.rs +++ b/src/services/preprocess.rs @@ -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 { @@ -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); @@ -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();