#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AppleEvent {
AppNotRunning,
TimedOut,
Other(i32),
}
impl AppleEvent {
pub(crate) fn of<'a>(lines: impl IntoIterator<Item = &'a String>) -> Option<AppleEvent> {
let mut last = None;
for line in lines {
for (at, _) in line.match_indices("(-") {
let rest = line.get(at + 1..).unwrap_or_default();
if let Some(n) = rest.find(')').and_then(|end| rest.get(..end)).and_then(|n| n.parse::<i32>().ok()) {
last = Some(n);
}
}
}
Some(match last? {
-600 => AppleEvent::AppNotRunning,
-1712 => AppleEvent::TimedOut,
other => AppleEvent::Other(other),
})
}
pub(crate) fn number(self) -> i32 {
match self {
AppleEvent::AppNotRunning => -600,
AppleEvent::TimedOut => -1712,
AppleEvent::Other(n) => n,
}
}
pub(crate) fn passes(self) -> bool {
matches!(self, AppleEvent::AppNotRunning | AppleEvent::TimedOut)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn of(text: &str) -> Option<AppleEvent> {
let lines: Vec<String> = text.lines().map(str::to_owned).collect();
AppleEvent::of(&lines)
}
#[test]
fn an_apple_event_error_is_read_by_its_number() {
assert_eq!(
of(include_str!("fixtures/osascript/vagrant_utm-forwarded-ports-not-running.txt")),
Some(AppleEvent::AppNotRunning)
);
assert_eq!(
of(include_str!("fixtures/osascript/vagrant_utm-forwarded-ports-timed-out.txt")),
Some(AppleEvent::TimedOut)
);
assert_eq!(
of("erreur d’exécution : Non autorisé à envoyer des Apple Events à UTM. (-1743)"),
Some(AppleEvent::Other(-1743))
);
assert_eq!(of("exit status 1"), None);
assert!(AppleEvent::AppNotRunning.passes() && AppleEvent::TimedOut.passes() && !AppleEvent::Other(-1743).passes());
}
}