Skip to main content

mxr_rules/
action.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4/// Actions a rule can perform on a matching message.
5#[derive(Debug, Clone, Serialize, Deserialize)]
6#[serde(tag = "type", rename_all = "snake_case")]
7pub enum RuleAction {
8    AddLabel { label: String },
9    RemoveLabel { label: String },
10    Archive,
11    Trash,
12    Star,
13    MarkRead,
14    MarkUnread,
15    Snooze { duration: SnoozeDuration },
16    /// Run external command with message JSON on stdin.
17    ShellHook { command: String },
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
21#[serde(tag = "type", rename_all = "snake_case")]
22pub enum SnoozeDuration {
23    Hours { count: u32 },
24    Days { count: u32 },
25    Until { date: DateTime<Utc> },
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn actions_roundtrip_through_json() {
34        let actions = vec![
35            RuleAction::Archive,
36            RuleAction::AddLabel {
37                label: "important".into(),
38            },
39            RuleAction::ShellHook {
40                command: "notify-send 'New mail'".into(),
41            },
42            RuleAction::Snooze {
43                duration: SnoozeDuration::Hours { count: 4 },
44            },
45        ];
46
47        let json = serde_json::to_string(&actions).unwrap();
48        let parsed: Vec<RuleAction> = serde_json::from_str(&json).unwrap();
49        assert_eq!(parsed.len(), 4);
50    }
51
52    #[test]
53    fn snooze_duration_variants_serialize() {
54        let durations = vec![
55            SnoozeDuration::Hours { count: 2 },
56            SnoozeDuration::Days { count: 7 },
57            SnoozeDuration::Until {
58                date: chrono::Utc::now(),
59            },
60        ];
61
62        for d in durations {
63            let json = serde_json::to_string(&d).unwrap();
64            let _: SnoozeDuration = serde_json::from_str(&json).unwrap();
65        }
66    }
67}