Skip to main content

greentic_component/
lifecycle.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
4pub struct Lifecycle {
5    pub init: bool,
6    pub health: bool,
7    pub shutdown: bool,
8}
9
10impl Lifecycle {
11    pub fn is_noop(&self) -> bool {
12        !(self.init || self.health || self.shutdown)
13    }
14}
15
16#[cfg(test)]
17mod tests {
18    use super::Lifecycle;
19
20    #[test]
21    fn default_lifecycle_is_noop() {
22        assert!(Lifecycle::default().is_noop());
23    }
24
25    #[test]
26    fn any_enabled_hook_makes_lifecycle_non_noop() {
27        assert!(
28            !Lifecycle {
29                init: true,
30                ..Lifecycle::default()
31            }
32            .is_noop()
33        );
34        assert!(
35            !Lifecycle {
36                health: true,
37                ..Lifecycle::default()
38            }
39            .is_noop()
40        );
41        assert!(
42            !Lifecycle {
43                shutdown: true,
44                ..Lifecycle::default()
45            }
46            .is_noop()
47        );
48    }
49}