#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Liveness {
Live,
NothingContacted,
}
pub fn verdict(attempted: usize, succeeded: usize) -> Liveness {
if attempted == 0 || succeeded > 0 {
Liveness::Live
} else {
Liveness::NothingContacted
}
}
#[derive(Debug, Default, Clone, Copy)]
pub struct Probes {
attempted: usize,
succeeded: usize,
}
impl Probes {
pub fn record<T, E>(&mut self, outcome: Result<T, E>) -> Option<T> {
self.attempted += 1;
match outcome {
Ok(value) => {
self.succeeded += 1;
Some(value)
}
Err(_) => None,
}
}
pub fn verdict(&self) -> Liveness {
verdict(self.attempted, self.succeeded)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandOutput {
pub stdout: String,
pub liveness: Liveness,
}
impl CommandOutput {
pub fn live(stdout: impl Into<String>) -> Self {
Self {
stdout: stdout.into(),
liveness: Liveness::Live,
}
}
pub fn new(stdout: impl Into<String>, liveness: Liveness) -> Self {
Self {
stdout: stdout.into(),
liveness,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Disposition {
Success,
AcceptedUnvalidated,
WarnUnvalidated,
RejectUnvalidated,
}
impl Disposition {
pub fn prints_stdout(self) -> bool {
!matches!(self, Self::RejectUnvalidated)
}
pub fn prints_banner(self) -> bool {
matches!(self, Self::WarnUnvalidated | Self::RejectUnvalidated)
}
pub fn prints_note(self) -> bool {
matches!(self, Self::AcceptedUnvalidated)
}
pub fn exit_code(self) -> u8 {
match self {
Self::Success | Self::AcceptedUnvalidated => 0,
Self::RejectUnvalidated => 1,
Self::WarnUnvalidated => 3,
}
}
}
pub fn disposition(liveness: Liveness, offline: bool, require_live: bool) -> Disposition {
match liveness {
Liveness::Live => Disposition::Success,
Liveness::NothingContacted => {
if require_live {
Disposition::RejectUnvalidated
} else if offline {
Disposition::AcceptedUnvalidated
} else {
Disposition::WarnUnvalidated
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn total_failure_is_not_live() {
assert_eq!(verdict(4, 0), Liveness::NothingContacted);
}
#[test]
fn one_success_out_of_four_is_live() {
assert_eq!(verdict(4, 1), Liveness::Live);
}
#[test]
fn all_successes_are_live() {
assert_eq!(verdict(4, 4), Liveness::Live);
}
#[test]
fn nothing_attempted_is_live() {
assert_eq!(verdict(0, 0), Liveness::Live);
}
#[test]
fn probes_counts_both_arms() {
let mut probes = Probes::default();
assert_eq!(probes.record::<u8, ()>(Ok(7)), Some(7));
assert_eq!(probes.record::<u8, ()>(Err(())), None);
assert_eq!(probes.verdict(), Liveness::Live);
}
#[test]
fn probes_all_failing_is_nothing_contacted() {
let mut probes = Probes::default();
for _ in 0..3 {
assert_eq!(probes.record::<u8, ()>(Err(())), None);
}
assert_eq!(probes.verdict(), Liveness::NothingContacted);
}
#[test]
fn empty_probes_is_live() {
assert_eq!(Probes::default().verdict(), Liveness::Live);
}
#[test]
fn command_output_live_helper() {
let out = CommandOutput::live("Playing (Kitchen)");
assert_eq!(out.stdout, "Playing (Kitchen)");
assert_eq!(out.liveness, Liveness::Live);
}
#[test]
fn live_prints_and_exits_zero() {
let d = disposition(Liveness::Live, false, false);
assert_eq!(d, Disposition::Success);
assert!(d.prints_stdout());
assert!(!d.prints_banner());
assert!(!d.prints_note());
assert_eq!(d.exit_code(), 0);
}
#[test]
fn live_ignores_both_flags() {
for (offline, require_live) in [(true, false), (false, true)] {
let d = disposition(Liveness::Live, offline, require_live);
assert_eq!(
d,
Disposition::Success,
"offline={offline} require_live={require_live}"
);
assert_eq!(d.exit_code(), 0);
}
}
#[test]
fn nothing_contacted_with_offline_prints_and_exits_zero() {
let d = disposition(Liveness::NothingContacted, true, false);
assert_eq!(d, Disposition::AcceptedUnvalidated);
assert!(d.prints_stdout());
assert!(!d.prints_banner());
assert!(d.prints_note());
assert_eq!(d.exit_code(), 0);
}
#[test]
fn nothing_contacted_by_default_prints_and_exits_three() {
let d = disposition(Liveness::NothingContacted, false, false);
assert_eq!(d, Disposition::WarnUnvalidated);
assert!(d.prints_stdout());
assert!(d.prints_banner());
assert!(!d.prints_note());
assert_eq!(d.exit_code(), 3);
}
#[test]
fn nothing_contacted_with_require_live_suppresses_stdout_and_exits_one() {
let d = disposition(Liveness::NothingContacted, false, true);
assert_eq!(d, Disposition::RejectUnvalidated);
assert!(!d.prints_stdout());
assert!(d.prints_banner());
assert_eq!(d.exit_code(), 1);
}
#[test]
fn require_live_wins_over_offline() {
assert_eq!(
disposition(Liveness::NothingContacted, true, true),
Disposition::RejectUnvalidated
);
}
#[test]
fn total_failure_never_exits_zero_without_opt_in() {
assert_ne!(
disposition(verdict(10, 0), false, false).exit_code(),
0,
"ten failed probes reported as success"
);
}
}