timerd-rs 0.6.0

A small foreground/background terminal timer for Linux
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use std::time::{SystemTime, UNIX_EPOCH};

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum TimerKind {
    Stopwatch,
    Countdown,
    Alarm,
}

impl TimerKind {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Stopwatch => "stopwatch",
            Self::Countdown => "countdown",
            Self::Alarm => "alarm",
        }
    }

    pub fn counts_down(self) -> bool {
        matches!(self, Self::Countdown | Self::Alarm)
    }
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum TimerStatus {
    Running,
    Paused,
    Done,
    Stopped,
    Cancelled,
}

impl TimerStatus {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Running => "running",
            Self::Paused => "paused",
            Self::Done => "done",
            Self::Stopped => "stopped",
            Self::Cancelled => "cancelled",
        }
    }

    pub fn is_active(self) -> bool {
        matches!(self, Self::Running | Self::Paused)
    }

    pub fn is_final(self) -> bool {
        !self.is_active()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimerTask {
    pub id: u64,
    pub kind: TimerKind,
    pub status: TimerStatus,
    pub note: Option<String>,
    pub duration_millis: Option<u64>,
    #[serde(default)]
    pub target_at_millis: Option<u64>,
    #[serde(default)]
    pub bell: bool,
    pub accumulated_millis: u64,
    pub run_started_at_millis: Option<u64>,
    pub created_at_millis: u64,
    pub last_touched_at_millis: u64,
    pub finished_at_millis: Option<u64>,
}

impl TimerTask {
    pub fn new(
        id: u64,
        kind: TimerKind,
        duration_millis: Option<u64>,
        note: Option<String>,
        now: u64,
    ) -> Self {
        Self {
            id,
            kind,
            status: TimerStatus::Running,
            note: note.filter(|note| !note.trim().is_empty()),
            duration_millis,
            target_at_millis: None,
            bell: false,
            accumulated_millis: 0,
            run_started_at_millis: Some(now),
            created_at_millis: now,
            last_touched_at_millis: now,
            finished_at_millis: None,
        }
    }

    pub fn new_alarm(id: u64, target_at_millis: u64, note: Option<String>, now: u64) -> Self {
        Self {
            id,
            kind: TimerKind::Alarm,
            status: TimerStatus::Running,
            note: note.filter(|note| !note.trim().is_empty()),
            duration_millis: None,
            target_at_millis: Some(target_at_millis),
            bell: false,
            accumulated_millis: 0,
            run_started_at_millis: None,
            created_at_millis: now,
            last_touched_at_millis: now,
            finished_at_millis: None,
        }
    }

    pub fn with_bell(mut self, bell: bool) -> Self {
        self.bell = bell;
        self
    }

    pub fn elapsed_millis(&self, now: u64) -> u64 {
        let current_run = self
            .run_started_at_millis
            .map(|started| now.saturating_sub(started))
            .unwrap_or(0);
        self.accumulated_millis.saturating_add(current_run)
    }

    pub fn display_millis(&self, now: u64) -> u64 {
        match self.kind {
            TimerKind::Stopwatch => self.elapsed_millis(now),
            TimerKind::Countdown => self
                .duration_millis
                .unwrap_or_default()
                .saturating_sub(self.elapsed_millis(now)),
            TimerKind::Alarm if self.status.is_active() => self
                .target_at_millis
                .unwrap_or_default()
                .saturating_sub(now),
            TimerKind::Alarm => self.accumulated_millis,
        }
    }

    pub fn is_due(&self, now: u64) -> bool {
        if self.status != TimerStatus::Running {
            return false;
        }
        match self.kind {
            TimerKind::Stopwatch => false,
            TimerKind::Countdown => {
                self.elapsed_millis(now) >= self.duration_millis.unwrap_or_default()
            }
            TimerKind::Alarm => now >= self.target_at_millis.unwrap_or_default(),
        }
    }

    pub fn complete(&mut self, now: u64) {
        self.accumulated_millis = self.duration_millis.unwrap_or_default();
        self.run_started_at_millis = None;
        self.status = TimerStatus::Done;
        self.finished_at_millis = Some(now);
        self.last_touched_at_millis = now;
    }

    pub fn pause(&mut self, now: u64) -> Result<(), &'static str> {
        if self.kind == TimerKind::Alarm {
            return Err("alarms cannot be paused; cancel the alarm instead");
        }
        if self.status != TimerStatus::Running {
            return Err("timer is not running");
        }
        self.accumulated_millis = self.elapsed_millis(now);
        self.run_started_at_millis = None;
        self.status = TimerStatus::Paused;
        self.last_touched_at_millis = now;
        Ok(())
    }

    pub fn resume(&mut self, now: u64) -> Result<(), &'static str> {
        if self.kind == TimerKind::Alarm {
            return Err("alarms cannot be resumed");
        }
        if self.status != TimerStatus::Paused {
            return Err("timer is not paused");
        }
        self.run_started_at_millis = Some(now);
        self.status = TimerStatus::Running;
        self.last_touched_at_millis = now;
        Ok(())
    }

    pub fn toggle_pause(&mut self, now: u64) -> Result<(), &'static str> {
        match self.status {
            TimerStatus::Running => self.pause(now),
            TimerStatus::Paused => self.resume(now),
            _ => Err("timer has already finished"),
        }
    }

    pub fn stop(&mut self, now: u64) -> Result<(), &'static str> {
        if !self.status.is_active() {
            return Err("timer has already finished");
        }
        self.accumulated_millis = match self.kind {
            TimerKind::Alarm => self.display_millis(now),
            TimerKind::Stopwatch | TimerKind::Countdown => self.elapsed_millis(now),
        };
        self.run_started_at_millis = None;
        self.status = match self.kind {
            TimerKind::Stopwatch => TimerStatus::Stopped,
            TimerKind::Countdown | TimerKind::Alarm => TimerStatus::Cancelled,
        };
        self.finished_at_millis = Some(now);
        self.last_touched_at_millis = now;
        Ok(())
    }

    pub fn snooze(&mut self, delay_millis: u64, now: u64) -> Result<(), &'static str> {
        if !self.status.is_active() {
            return Err("timer has already finished");
        }
        if delay_millis == 0 {
            return Err("snooze duration must be greater than zero");
        }
        match self.kind {
            TimerKind::Stopwatch => return Err("a stopwatch cannot be snoozed"),
            TimerKind::Countdown => {
                let remaining = self
                    .display_millis(now)
                    .checked_add(delay_millis)
                    .ok_or("snooze duration is too large")?;
                self.set_countdown_remaining(remaining, now);
            }
            TimerKind::Alarm => {
                let target = self
                    .target_at_millis
                    .unwrap_or(now)
                    .checked_add(delay_millis)
                    .ok_or("snooze duration is too large")?;
                self.target_at_millis = Some(target);
                self.last_touched_at_millis = now;
            }
        }
        Ok(())
    }

    pub fn edit(
        &mut self,
        note: Option<String>,
        schedule_millis: Option<u64>,
        now: u64,
    ) -> Result<(), &'static str> {
        if !self.status.is_active() {
            return Err("timer has already finished");
        }
        match (self.kind, schedule_millis) {
            (TimerKind::Stopwatch, Some(_)) => {
                return Err("a stopwatch has no schedule to edit");
            }
            (TimerKind::Countdown, Some(0)) => {
                return Err("countdown duration must be greater than zero");
            }
            (TimerKind::Countdown, Some(remaining)) => {
                self.set_countdown_remaining(remaining, now);
            }
            (TimerKind::Alarm, Some(target)) if target <= now => {
                return Err("alarm target time must be in the future");
            }
            (TimerKind::Alarm, Some(target)) => {
                self.target_at_millis = Some(target);
            }
            (_, None) => {}
        }
        self.note = note.filter(|note| !note.trim().is_empty());
        self.last_touched_at_millis = now;
        Ok(())
    }

    fn set_countdown_remaining(&mut self, remaining_millis: u64, now: u64) {
        self.duration_millis = Some(remaining_millis);
        self.accumulated_millis = 0;
        self.run_started_at_millis = (self.status == TimerStatus::Running).then_some(now);
        self.last_touched_at_millis = now;
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimerView {
    pub id: u64,
    pub kind: TimerKind,
    pub status: TimerStatus,
    pub note: Option<String>,
    pub value_millis: u64,
    #[serde(default)]
    pub target_at_millis: Option<u64>,
    #[serde(default)]
    pub bell: bool,
    #[serde(default)]
    pub created_at_millis: u64,
}

impl TimerView {
    pub fn from_task(task: &TimerTask, now: u64) -> Self {
        Self {
            id: task.id,
            kind: task.kind,
            status: task.status,
            note: task.note.clone(),
            value_millis: task.display_millis(now),
            target_at_millis: task.target_at_millis,
            bell: task.bell,
            created_at_millis: task.created_at_millis,
        }
    }
}

pub fn now_millis() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis()
        .try_into()
        .unwrap_or(u64::MAX)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn countdown_uses_a_deadline_instead_of_ticks() {
        let task = TimerTask::new(1, TimerKind::Countdown, Some(15_000), None, 1_000);
        assert_eq!(task.display_millis(6_250), 9_750);
        assert!(!task.is_due(15_999));
        assert!(task.is_due(16_000));
    }

    #[test]
    fn pause_and_resume_preserve_elapsed_time() {
        let mut task = TimerTask::new(1, TimerKind::Stopwatch, None, None, 1_000);
        task.pause(6_000).unwrap();
        assert_eq!(task.display_millis(20_000), 5_000);
        task.resume(20_000).unwrap();
        assert_eq!(task.display_millis(23_000), 8_000);
    }

    #[test]
    fn stop_has_kind_specific_final_state() {
        let mut stopwatch = TimerTask::new(1, TimerKind::Stopwatch, None, None, 0);
        stopwatch.stop(1_000).unwrap();
        assert_eq!(stopwatch.status, TimerStatus::Stopped);

        let mut countdown = TimerTask::new(2, TimerKind::Countdown, Some(5_000), None, 0);
        countdown.stop(1_000).unwrap();
        assert_eq!(countdown.status, TimerStatus::Cancelled);
    }

    #[test]
    fn alarm_tracks_an_absolute_deadline_and_cannot_pause() {
        let mut alarm = TimerTask::new_alarm(3, 10_000, Some("breakfast".to_owned()), 1_000);
        assert_eq!(alarm.display_millis(4_000), 6_000);
        assert!(!alarm.is_due(9_999));
        assert!(alarm.is_due(10_000));
        assert_eq!(
            alarm.pause(5_000),
            Err("alarms cannot be paused; cancel the alarm instead")
        );
        alarm.stop(5_000).unwrap();
        assert_eq!(alarm.status, TimerStatus::Cancelled);
        assert_eq!(alarm.display_millis(9_000), 5_000);
    }

    #[test]
    fn old_state_without_alarm_target_still_deserializes() {
        let task: TimerTask = serde_json::from_value(serde_json::json!({
            "id": 1,
            "kind": "countdown",
            "status": "running",
            "note": null,
            "duration_millis": 60000,
            "accumulated_millis": 0,
            "run_started_at_millis": 1000,
            "created_at_millis": 1000,
            "last_touched_at_millis": 1000,
            "finished_at_millis": null
        }))
        .unwrap();
        assert_eq!(task.kind, TimerKind::Countdown);
        assert_eq!(task.target_at_millis, None);
        assert!(!task.bell);
    }

    #[test]
    fn snooze_delays_countdowns_and_alarms_but_not_stopwatches() {
        let mut countdown = TimerTask::new(1, TimerKind::Countdown, Some(10_000), None, 1_000);
        countdown.snooze(5_000, 4_000).unwrap();
        assert_eq!(countdown.display_millis(4_000), 12_000);

        let mut alarm = TimerTask::new_alarm(2, 10_000, None, 1_000);
        alarm.snooze(5_000, 4_000).unwrap();
        assert_eq!(alarm.target_at_millis, Some(15_000));

        let mut stopwatch = TimerTask::new(3, TimerKind::Stopwatch, None, None, 1_000);
        assert_eq!(
            stopwatch.snooze(5_000, 4_000),
            Err("a stopwatch cannot be snoozed")
        );
    }

    #[test]
    fn edit_updates_note_and_kind_specific_schedule() {
        let mut countdown = TimerTask::new(1, TimerKind::Countdown, Some(10_000), None, 1_000);
        countdown
            .edit(Some("coffee".to_owned()), Some(30_000), 4_000)
            .unwrap();
        assert_eq!(countdown.note.as_deref(), Some("coffee"));
        assert_eq!(countdown.display_millis(4_000), 30_000);

        let mut alarm = TimerTask::new_alarm(2, 10_000, None, 1_000);
        alarm
            .edit(Some("breakfast".to_owned()), Some(20_000), 4_000)
            .unwrap();
        assert_eq!(alarm.note.as_deref(), Some("breakfast"));
        assert_eq!(alarm.target_at_millis, Some(20_000));
    }
}