Skip to main content

aidaemon_core/
mandate_card.rs

1//! The owner's confirmation card for a mandate as it travels in
2//! [`crate::OwnerApprovalRequest::warnings`]: five essentials the owner reads
3//! at a glance (each a `"<label>: <text>"` line with a label from
4//! [`Essential`]), then every other line as a detail. Here, below both the
5//! work crate that builds it and the channels that render it.
6
7/// One of the five things the owner needs to approve a mandate.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum Essential {
10    What,
11    HowOften,
12    CanDo,
13    TellsYouWhen,
14    Ends,
15}
16
17impl Essential {
18    /// In card order.
19    pub const ALL: [Self; 5] = [
20        Self::What,
21        Self::HowOften,
22        Self::CanDo,
23        Self::TellsYouWhen,
24        Self::Ends,
25    ];
26
27    /// The label the line starts with, as the owner reads it.
28    pub const fn label(self) -> &'static str {
29        match self {
30            Self::What => "What",
31            Self::HowOften => "How often",
32            Self::CanDo => "Can do",
33            Self::TellsYouWhen => "Tells you when",
34            Self::Ends => "Ends",
35        }
36    }
37
38    /// The essential a card line carries, and its text; `None` for a detail.
39    pub fn parse_line(line: &str) -> Option<(Self, &str)> {
40        Self::ALL.into_iter().find_map(|essential| {
41            line.strip_prefix(essential.label())
42                .and_then(|rest| rest.strip_prefix(':'))
43                .map(|text| (essential, text.trim()))
44        })
45    }
46}
47
48/// A mandate confirmation as the owner sees it.
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct MandateConfirmationCard {
51    pub what: String,
52    pub how_often: String,
53    pub can_do: String,
54    pub tells_you_when: String,
55    pub ends: String,
56    /// Everything else, one `"<label>: <text>"` line each, for a "Details"
57    /// section.
58    pub details: Vec<String>,
59}
60
61impl MandateConfirmationCard {
62    /// The essentials in card order.
63    pub fn essentials(&self) -> [(Essential, &str); 5] {
64        [
65            (Essential::What, self.what.as_str()),
66            (Essential::HowOften, self.how_often.as_str()),
67            (Essential::CanDo, self.can_do.as_str()),
68            (Essential::TellsYouWhen, self.tells_you_when.as_str()),
69            (Essential::Ends, self.ends.as_str()),
70        ]
71    }
72
73    /// The card as approval-request lines: the essentials, then the details.
74    pub fn into_lines(self) -> Vec<String> {
75        let mut lines = self
76            .essentials()
77            .into_iter()
78            .map(|(essential, text)| format!("{}: {text}", essential.label()))
79            .collect::<Vec<_>>();
80        lines.extend(self.details);
81        lines
82    }
83
84    /// The card a list of approval-request lines carries; `None` unless every
85    /// essential is there.
86    pub fn from_lines(lines: &[String]) -> Option<Self> {
87        let mut essentials: [Option<String>; 5] = Default::default();
88        let mut details = Vec::new();
89        for line in lines {
90            match Essential::parse_line(line) {
91                Some((essential, text)) => {
92                    let slot = Essential::ALL
93                        .iter()
94                        .position(|candidate| *candidate == essential)
95                        .expect("every essential is listed");
96                    essentials[slot] = Some(text.to_string());
97                }
98                None => details.push(line.clone()),
99            }
100        }
101        let [what, how_often, can_do, tells_you_when, ends] = essentials;
102        Some(Self {
103            what: what?,
104            how_often: how_often?,
105            can_do: can_do?,
106            tells_you_when: tells_you_when?,
107            ends: ends?,
108            details,
109        })
110    }
111}