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