Skip to main content

use_event_kind/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4use core::fmt;
5
6#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
7pub enum EventKind {
8    Created,
9    Updated,
10    Deleted,
11    Started,
12    Finished,
13    Failed,
14    Received,
15    Emitted,
16    Custom(String),
17}
18
19impl EventKind {
20    pub fn custom(value: impl Into<String>) -> Self {
21        Self::Custom(value.into())
22    }
23
24    pub fn as_str(&self) -> &str {
25        match self {
26            Self::Created => "created",
27            Self::Updated => "updated",
28            Self::Deleted => "deleted",
29            Self::Started => "started",
30            Self::Finished => "finished",
31            Self::Failed => "failed",
32            Self::Received => "received",
33            Self::Emitted => "emitted",
34            Self::Custom(value) => value.as_str(),
35        }
36    }
37}
38
39impl fmt::Display for EventKind {
40    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
41        formatter.write_str(self.as_str())
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::EventKind;
48
49    #[test]
50    fn exposes_standard_kind_labels() {
51        assert_eq!(EventKind::Created.as_str(), "created");
52        assert_eq!(EventKind::Started.to_string(), "started");
53        assert_eq!(EventKind::Failed.as_str(), "failed");
54    }
55
56    #[test]
57    fn supports_custom_kind_labels() {
58        let kind = EventKind::custom("checkpoint");
59
60        assert_eq!(kind.as_str(), "checkpoint");
61        assert_eq!(kind.to_string(), "checkpoint");
62    }
63}