use core::fmt;
use crate::signal::Signal;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ShutdownReason {
Signal(Signal),
Requested,
Forced,
Timeout,
Error,
}
impl ShutdownReason {
#[must_use]
pub const fn description(self) -> &'static str {
match self {
Self::Signal(_) => "signal",
Self::Requested => "requested",
Self::Forced => "forced",
Self::Timeout => "timeout",
Self::Error => "error",
}
}
#[must_use]
pub const fn is_signal(self) -> bool {
matches!(self, Self::Signal(_))
}
}
impl fmt::Display for ShutdownReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Signal(s) => write!(f, "Signal({s})"),
Self::Requested => f.write_str("Requested"),
Self::Forced => f.write_str("Forced"),
Self::Timeout => f.write_str("Timeout"),
Self::Error => f.write_str("Error"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn description_labels() {
assert_eq!(ShutdownReason::Requested.description(), "requested");
assert_eq!(ShutdownReason::Forced.description(), "forced");
assert_eq!(ShutdownReason::Timeout.description(), "timeout");
assert_eq!(ShutdownReason::Error.description(), "error");
assert_eq!(
ShutdownReason::Signal(Signal::Terminate).description(),
"signal"
);
}
#[test]
fn is_signal_only_for_signal_variant() {
assert!(ShutdownReason::Signal(Signal::Interrupt).is_signal());
assert!(!ShutdownReason::Requested.is_signal());
assert!(!ShutdownReason::Forced.is_signal());
}
#[test]
fn display_renders_signal_label() {
let s = format!("{}", ShutdownReason::Signal(Signal::Terminate));
assert!(s.starts_with("Signal("));
}
}