1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::time::Duration;
use std::time::SystemTime;

use serde::{Deserialize, Serialize};
use crate::time::Timer;
use crate::time::duration;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Pomodoro {
    #[serde(flatten)]
    timer: Timer,
    description: Option<String>,
    tags: Option<Vec<String>>,
    #[serde(default, with = "crate::time::datetimeopt::unix")]
    finished_at: Option<SystemTime>,
}

impl Pomodoro {
    pub fn new(starts_at: SystemTime, duration: Duration) -> Self {
        let timer = Timer::new(starts_at, duration);
        Self {
            timer,
            finished_at: None,
            description: None,
            tags: None,
        }
    }

    pub fn timer(&self) -> &Timer {
        &self.timer
    }

    pub fn description(&self) -> Option<&str> {
        self.description.as_deref()
    }

    pub fn set_description(&mut self, description: &str) {
        self.description = Some(description.to_string());
    }

    pub fn tags(&self) -> Option<&[String]> {
        self.tags.as_deref()
    }

    pub fn set_tags(&mut self, tags: Vec<String>) {
        self.tags = Some(tags);
    }

    pub fn finish(&mut self, now: SystemTime) {
        self.finished_at = Some(now);
    }

    pub fn format(&self, f: &str, now: SystemTime) -> String {
        let output = f
            .replace("%d", &self.description.as_ref().unwrap_or(&"".to_string()))
            .replace(
                "%t",
                &self
                    .tags
                    .as_ref()
                    .unwrap_or(&Vec::<String>::new())
                    .join(","),
            )
            .replace("%r", &duration::to_kitchen(&self.timer.remaining(now)))
            .replace("%R", &self.timer.remaining(now).as_secs().to_string())
            .replace("%S", &self.timer.starts_at().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs().to_string())
            .replace("%E", &self.timer.ends_at().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs().to_string());

        output
    }
}