Skip to main content

zentime_rs_timer/pomodoro_timer/
state.rs

1use super::{interval::Interval, on_end_handler::OnTimerEnd, on_tick_handler::OnTick};
2use crate::config::PomodoroTimerConfig;
3use serde::{Deserialize, Serialize};
4use std::{fmt::Debug, marker::PhantomData};
5
6/// General trait describing the various states a pomodoro timer can be in
7pub trait PomodoroState {}
8
9/// State which is shared between timers when they transition from one timer state to the
10/// next. Some of its information is also being shared with the [OnTick] closure.
11#[derive(Clone, Copy, Debug)]
12pub struct PomodoroTimerState {
13    /// The current pomodoro round.
14    /// This is incremented after each break.
15    pub round: u64,
16
17    /// Times the current break has been postponed - if a limit for postponing
18    /// has been set this will be used to determine if postponing is possible or not.
19    pub postponed_count: u16,
20}
21
22/// Information that will be handed to the [on_tick] closure continously
23#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct ViewState {
25    /// Denotes if the current timer is a break timer
26    pub is_break: bool,
27
28    /// Denotes if the timer is currently in a postponed state or not
29    pub is_postponed: bool,
30
31    /// Denotes how often the current timer has already been postponed
32    pub postpone_count: u16,
33
34    /// Denotes the current interval round
35    pub round: u64,
36
37    /// Denotes the current time of the timer
38    pub time: String,
39
40    /// Denotes if the timer is currently paused
41    pub is_paused: bool,
42}
43
44#[derive(Clone)]
45pub struct Callbacks {
46    pub on_timer_end: OnTimerEnd,
47    pub on_tick: OnTick,
48}
49
50impl Debug for Callbacks {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        f.debug_struct("PomodoroTimer")
53            .field("on_timer_end", &"callback closure")
54            .field("on_tick", &"callback closure")
55            .finish()
56    }
57}
58
59/// A Pomodoro-timer instance
60#[derive(Clone)]
61pub struct PomodoroTimer<S: PomodoroState> {
62    /// User configuration of the pomodoro timer
63    pub config: PomodoroTimerConfig,
64
65    /// Callback handlers
66    pub callbacks: Callbacks,
67
68    /// State which is shared between PomodoroTimer-transitions
69    pub shared_state: PomodoroTimerState,
70
71    /// Marker designating in which typestate we are currently in
72    pub marker: PhantomData<S>,
73}
74
75impl<S: PomodoroState + Debug> Debug for PomodoroTimer<S> {
76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77        f.debug_struct("PomodoroTimer")
78            .field("config", &self.config)
79            .field("on_timer_end", &"[closure] without context")
80            .field("on_tick", &"[closure] without context")
81            .finish()
82    }
83}
84
85impl<S: PomodoroState> PomodoroTimer<S> {
86    /// Resets the pomodoro timer to the very first interval
87    pub fn reset(config: PomodoroTimerConfig, callbacks: Callbacks) -> PomodoroTimer<Interval> {
88        PomodoroTimer::new(config, callbacks.on_timer_end, callbacks.on_tick)
89    }
90}