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}