Skip to main content

laminar_connectors/
health.rs

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