Skip to main content

rskit_bootstrap/
hooks.rs

1//! Bootstrap lifecycle phase metadata.
2
3use rskit_hook::{Event, EventType};
4
5/// Lifecycle phases emitted by bootstrap.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum LifecycleEventType {
9    /// Emitted before components start.
10    BeforeStart,
11    /// Emitted after components start and readiness checks pass.
12    AfterStart,
13    /// Emitted before components stop.
14    BeforeStop,
15    /// Emitted after components stop.
16    AfterStop,
17}
18
19impl LifecycleEventType {
20    /// Return the stable lifecycle phase label.
21    #[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/// Lifecycle event metadata for observers.
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub struct LifecycleEvent {
35    kind: LifecycleEventType,
36}
37
38impl LifecycleEvent {
39    /// Create a lifecycle event for the given phase.
40    #[must_use]
41    pub const fn new(kind: LifecycleEventType) -> Self {
42        Self { kind }
43    }
44
45    /// Return the lifecycle phase.
46    #[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}