aidaemon_core/
mandate_card.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum Essential {
10 What,
11 HowOften,
12 CanDo,
13 TellsYouWhen,
14 Ends,
15}
16
17impl Essential {
18 pub const ALL: [Self; 5] = [
20 Self::What,
21 Self::HowOften,
22 Self::CanDo,
23 Self::TellsYouWhen,
24 Self::Ends,
25 ];
26
27 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 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#[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 pub details: Vec<String>,
59}
60
61impl MandateConfirmationCard {
62 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 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 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}