#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Essential {
What,
HowOften,
CanDo,
TellsYouWhen,
Ends,
}
impl Essential {
pub const ALL: [Self; 5] = [
Self::What,
Self::HowOften,
Self::CanDo,
Self::TellsYouWhen,
Self::Ends,
];
pub const fn label(self) -> &'static str {
match self {
Self::What => "What",
Self::HowOften => "How often",
Self::CanDo => "Can do",
Self::TellsYouWhen => "Tells you when",
Self::Ends => "Ends",
}
}
pub fn parse_line(line: &str) -> Option<(Self, &str)> {
Self::ALL.into_iter().find_map(|essential| {
line.strip_prefix(essential.label())
.and_then(|rest| rest.strip_prefix(':'))
.map(|text| (essential, text.trim()))
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MandateConfirmationCard {
pub what: String,
pub how_often: String,
pub can_do: String,
pub tells_you_when: String,
pub ends: String,
pub details: Vec<String>,
}
impl MandateConfirmationCard {
pub fn essentials(&self) -> [(Essential, &str); 5] {
[
(Essential::What, self.what.as_str()),
(Essential::HowOften, self.how_often.as_str()),
(Essential::CanDo, self.can_do.as_str()),
(Essential::TellsYouWhen, self.tells_you_when.as_str()),
(Essential::Ends, self.ends.as_str()),
]
}
pub fn into_lines(self) -> Vec<String> {
let mut lines = self
.essentials()
.into_iter()
.map(|(essential, text)| format!("{}: {text}", essential.label()))
.collect::<Vec<_>>();
lines.extend(self.details);
lines
}
pub fn from_lines(lines: &[String]) -> Option<Self> {
let mut essentials: [Option<String>; 5] = Default::default();
let mut details = Vec::new();
for line in lines {
match Essential::parse_line(line) {
Some((essential, text)) => {
let slot = Essential::ALL
.iter()
.position(|candidate| *candidate == essential)
.expect("every essential is listed");
essentials[slot] = Some(text.to_string());
}
None => details.push(line.clone()),
}
}
let [what, how_often, can_do, tells_you_when, ends] = essentials;
Some(Self {
what: what?,
how_often: how_often?,
can_do: can_do?,
tells_you_when: tells_you_when?,
ends: ends?,
details,
})
}
}