Skip to main content

use_event_status/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4use core::fmt;
5
6#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
7pub enum EventStatus {
8    #[default]
9    Pending,
10    Emitted,
11    Received,
12    Handled,
13    Ignored,
14    Failed,
15}
16
17impl EventStatus {
18    pub const fn as_str(self) -> &'static str {
19        match self {
20            Self::Pending => "pending",
21            Self::Emitted => "emitted",
22            Self::Received => "received",
23            Self::Handled => "handled",
24            Self::Ignored => "ignored",
25            Self::Failed => "failed",
26        }
27    }
28
29    pub const fn is_terminal(self) -> bool {
30        matches!(self, Self::Handled | Self::Ignored | Self::Failed)
31    }
32
33    pub const fn is_failure(self) -> bool {
34        matches!(self, Self::Failed)
35    }
36}
37
38impl fmt::Display for EventStatus {
39    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
40        formatter.write_str((*self).as_str())
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::EventStatus;
47
48    #[test]
49    fn exposes_status_labels() {
50        assert_eq!(EventStatus::Pending.as_str(), "pending");
51        assert_eq!(EventStatus::Handled.to_string(), "handled");
52    }
53
54    #[test]
55    fn identifies_terminal_statuses() {
56        assert!(!EventStatus::Pending.is_terminal());
57        assert!(EventStatus::Handled.is_terminal());
58        assert!(EventStatus::Ignored.is_terminal());
59        assert!(EventStatus::Failed.is_terminal());
60    }
61
62    #[test]
63    fn identifies_failure_status() {
64        assert!(!EventStatus::Handled.is_failure());
65        assert!(EventStatus::Failed.is_failure());
66    }
67}