Skip to main content

laminar_connectors/
health.rs

1use std::fmt;
2
3/// Health status of a connector.
4#[derive(Debug, Clone, PartialEq, Eq, Default)]
5pub enum HealthStatus {
6    /// Connector is healthy and operating normally.
7    Healthy,
8
9    /// Connector is degraded but still operational.
10    /// Contains a description of the degradation.
11    Degraded(String),
12
13    /// Connector is unhealthy and not processing data.
14    /// Contains a description of the failure.
15    Unhealthy(String),
16
17    /// Health status is unknown (e.g., connector not yet started).
18    #[default]
19    Unknown,
20}
21
22impl HealthStatus {
23    /// Returns `true` if the connector is healthy.
24    #[must_use]
25    pub fn is_healthy(&self) -> bool {
26        matches!(self, HealthStatus::Healthy)
27    }
28
29    /// Returns `true` if the connector can still process data.
30    #[must_use]
31    pub fn is_operational(&self) -> bool {
32        matches!(self, HealthStatus::Healthy | HealthStatus::Degraded(_))
33    }
34}
35
36impl fmt::Display for HealthStatus {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        match self {
39            HealthStatus::Healthy => write!(f, "Healthy"),
40            HealthStatus::Degraded(msg) => write!(f, "Degraded: {msg}"),
41            HealthStatus::Unhealthy(msg) => write!(f, "Unhealthy: {msg}"),
42            HealthStatus::Unknown => write!(f, "Unknown"),
43        }
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_health_status_checks() {
53        assert!(HealthStatus::Healthy.is_healthy());
54        assert!(HealthStatus::Healthy.is_operational());
55
56        let degraded = HealthStatus::Degraded("high latency".into());
57        assert!(!degraded.is_healthy());
58        assert!(degraded.is_operational());
59
60        let unhealthy = HealthStatus::Unhealthy("connection lost".into());
61        assert!(!unhealthy.is_healthy());
62        assert!(!unhealthy.is_operational());
63
64        assert!(!HealthStatus::Unknown.is_healthy());
65        assert!(!HealthStatus::Unknown.is_operational());
66    }
67
68    #[test]
69    fn test_health_status_display() {
70        assert_eq!(HealthStatus::Healthy.to_string(), "Healthy");
71        assert!(HealthStatus::Degraded("slow".into())
72            .to_string()
73            .contains("slow"));
74        assert!(HealthStatus::Unhealthy("down".into())
75            .to_string()
76            .contains("down"));
77    }
78}