vault_tasks_time_management/
lib.rsuse std::time::Duration;
use pomodoro::Pomodoro;
use time_management_technique::TimeManagementTechnique;
pub mod flow_time;
pub mod pomodoro;
pub mod time_management_technique;
#[derive(Debug, PartialEq, Clone)]
pub enum State {
Focus(Option<Duration>),
Break(Option<Duration>),
}
#[derive(Debug)]
pub struct TimeManagementEngine {
pub mode: Box<dyn TimeManagementTechnique>,
pub state: Option<State>,
}
impl Default for TimeManagementEngine {
fn default() -> Self {
Self {
mode: Box::new(Pomodoro::classic_pomodoro()),
state: None,
}
}
}
impl TimeManagementEngine {
pub fn new(technique: Box<dyn TimeManagementTechnique>) -> Self {
Self {
mode: technique,
state: None,
}
}
pub fn switch(&mut self, time_spent: Duration) -> State {
let new_state = self.mode.switch(&self.state, time_spent);
self.state = Some(new_state.clone());
new_state
}
}
#[cfg(test)]
mod tests {
use color_eyre::eyre::Result;
use crate::{flow_time::FlowTime, pomodoro::Pomodoro, State, TimeManagementEngine};
use std::time::Duration;
#[test]
fn test_run_pomodoro() {
let mut time_tracker = TimeManagementEngine::new(Box::new(Pomodoro::classic_pomodoro()));
let focus_time = Duration::from_secs(60 * 25);
let short_break_time = Duration::from_secs(60 * 5);
assert!(time_tracker.state.is_none());
let to_spend_opt = time_tracker.switch(Duration::default());
assert!(time_tracker.state.is_some());
assert_eq!(
time_tracker.state.clone().unwrap(),
State::Focus(Some(focus_time))
);
assert_eq!(State::Focus(Some(focus_time)), to_spend_opt);
let to_spend_opt = time_tracker.switch(Duration::default());
assert!(time_tracker.state.is_some());
assert_eq!(
time_tracker.state.clone().unwrap(),
State::Break(Some(short_break_time))
);
assert_eq!(State::Break(Some(short_break_time)), to_spend_opt);
}
#[test]
fn test_full_run_pomodoro() {
let mut time_tracker = TimeManagementEngine::new(Box::new(Pomodoro::classic_pomodoro()));
assert!(time_tracker.state.is_none());
let mut to_spend_opt = State::Focus(None);
for _i in 0..2 {
for _j in 0..(3 * 2) {
let to_spend_opt2 = time_tracker.switch(Duration::from_secs(0));
to_spend_opt = to_spend_opt2;
}
assert!(time_tracker.state.is_some());
assert_eq!(time_tracker.state.clone().unwrap(), to_spend_opt);
}
}
#[test]
fn test_run_flowtime() -> Result<()> {
let break_factor = 5;
let mut time_tracker = TimeManagementEngine::new(Box::new(FlowTime::new(break_factor)?));
assert!(time_tracker.state.is_none());
let focus_time = Duration::from_secs(25);
let break_time = focus_time / break_factor;
let to_spend_opt = time_tracker.switch(Duration::from_secs(0));
assert_eq!(State::Focus(None), to_spend_opt);
assert!(time_tracker.state.is_some());
assert_eq!(time_tracker.state.clone().unwrap(), State::Focus(None));
let to_spend_opt = time_tracker.switch(focus_time);
assert!(time_tracker.state.is_some());
assert_eq!(
time_tracker.state.clone().unwrap(),
State::Break(Some(break_time))
);
assert_eq!(State::Break(Some(break_time)), to_spend_opt);
Ok(())
}
}