aboutsummaryrefslogtreecommitdiff
path: root/crates/dinggerd/src/importance.rs
blob: 2480d3ef7d6b10cb426db2bf1eff9d85a545b254 (plain)
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
//! An agent's importance, set by the user: high, normal (the default), or
//! low. It orders agents within a status (the list, and so the jump), and
//! a low agent makes no sound.
//!
//! It belongs to the agent's conversation: pi's extension saves it in the
//! session and reports it (`/importance`); other agents get it from
//! `dingger importance`. The daemon writes it to the pane option
//! `@dingger_importance` (unset for normal), which goes with the agent.

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum Importance {
    Low,
    #[default]
    Normal,
    High,
}

impl Importance {
    pub fn parse(value: &str) -> Option<Importance> {
        match value.trim() {
            "low" => Some(Importance::Low),
            "normal" => Some(Importance::Normal),
            "high" => Some(Importance::High),
            _ => None,
        }
    }

    /// A pane option's value: unset (empty) or anything unknown is normal.
    pub fn from_option(value: &str) -> Importance {
        Importance::parse(value).unwrap_or_default()
    }

    pub fn label(self) -> &'static str {
        match self {
            Importance::Low => "low",
            Importance::Normal => "normal",
            Importance::High => "high",
        }
    }

    /// The pane option's value: normal leaves it unset.
    pub fn option_value(self) -> &'static str {
        match self {
            Importance::Normal => "",
            other => other.label(),
        }
    }
}

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

    #[test]
    fn levels_order_and_round_trip_through_the_option() {
        assert!(Importance::High > Importance::Normal && Importance::Normal > Importance::Low);
        for level in [Importance::Low, Importance::Normal, Importance::High] {
            assert_eq!(Importance::from_option(level.option_value()), level);
            assert_eq!(Importance::parse(level.label()), Some(level));
        }
        assert_eq!(Importance::from_option(""), Importance::Normal, "unset is normal");
        assert_eq!(Importance::from_option("urgent"), Importance::Normal);
        assert_eq!(Importance::parse("urgent"), None);
    }
}