zentime_rs_timer/pomodoro_timer/
state.rs1use 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
6pub trait PomodoroState {}
8
9#[derive(Clone, Copy, Debug)]
12pub struct PomodoroTimerState {
13 pub round: u64,
16
17 pub postponed_count: u16,
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct ViewState {
25 pub is_break: bool,
27
28 pub is_postponed: bool,
30
31 pub postpone_count: u16,
33
34 pub round: u64,
36
37 pub time: String,
39
40 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#[derive(Clone)]
61pub struct PomodoroTimer<S: PomodoroState> {
62 pub config: PomodoroTimerConfig,
64
65 pub callbacks: Callbacks,
67
68 pub shared_state: PomodoroTimerState,
70
71 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 pub fn reset(config: PomodoroTimerConfig, callbacks: Callbacks) -> PomodoroTimer<Interval> {
88 PomodoroTimer::new(config, callbacks.on_timer_end, callbacks.on_tick)
89 }
90}