1use rskit_hook::{Event, EventType};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum LifecycleEventType {
9 BeforeStart,
11 AfterStart,
13 BeforeStop,
15 AfterStop,
17}
18
19impl LifecycleEventType {
20 #[must_use]
22 pub const fn as_str(self) -> &'static str {
23 match self {
24 Self::BeforeStart => "bootstrap:before_start",
25 Self::AfterStart => "bootstrap:after_start",
26 Self::BeforeStop => "bootstrap:before_stop",
27 Self::AfterStop => "bootstrap:after_stop",
28 }
29 }
30}
31
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub struct LifecycleEvent {
35 kind: LifecycleEventType,
36}
37
38impl LifecycleEvent {
39 #[must_use]
41 pub const fn new(kind: LifecycleEventType) -> Self {
42 Self { kind }
43 }
44
45 #[must_use]
47 pub const fn kind(&self) -> LifecycleEventType {
48 self.kind
49 }
50}
51
52impl Event for LifecycleEvent {
53 fn event_type(&self) -> EventType {
54 EventType::new(self.kind.as_str())
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use rskit_hook::Event as _;
61
62 use super::*;
63
64 #[test]
65 fn lifecycle_event_types_expose_stable_labels_and_event_types() {
66 assert_eq!(
67 LifecycleEventType::BeforeStart.as_str(),
68 "bootstrap:before_start"
69 );
70 assert_eq!(
71 LifecycleEventType::AfterStart.as_str(),
72 "bootstrap:after_start"
73 );
74 assert_eq!(
75 LifecycleEventType::BeforeStop.as_str(),
76 "bootstrap:before_stop"
77 );
78 assert_eq!(
79 LifecycleEventType::AfterStop.as_str(),
80 "bootstrap:after_stop"
81 );
82
83 let event = LifecycleEvent::new(LifecycleEventType::AfterStop);
84 assert_eq!(event.kind(), LifecycleEventType::AfterStop);
85 assert_eq!(event.event_type(), EventType::new("bootstrap:after_stop"));
86 }
87}