Skip to main content

libspot_rs/
status.rs

1//! Status codes for SPOT operations
2
3/// Status codes returned by SPOT operations that match the C implementation exactly
4///
5/// # Serialization
6///
7/// When the `serde` feature is enabled, this enum can be serialized and deserialized.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub enum SpotStatus {
11    /// Data is normal
12    Normal = 0,
13    /// Data is in the tail (excess)
14    Excess = 1,
15    /// Data is beyond the anomaly threshold
16    Anomaly = 2,
17}
18
19impl From<i32> for SpotStatus {
20    fn from(code: i32) -> Self {
21        match code {
22            0 => SpotStatus::Normal,
23            1 => SpotStatus::Excess,
24            2 => SpotStatus::Anomaly,
25            _ => SpotStatus::Normal, // Default fallback
26        }
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn test_spot_status_values() {
36        // Test that enum values match expected integers
37        assert_eq!(SpotStatus::Normal as i32, 0);
38        assert_eq!(SpotStatus::Excess as i32, 1);
39        assert_eq!(SpotStatus::Anomaly as i32, 2);
40    }
41
42    #[test]
43    fn test_spot_status_from_i32() {
44        // Test conversion from C int values
45        assert_eq!(SpotStatus::from(0), SpotStatus::Normal);
46        assert_eq!(SpotStatus::from(1), SpotStatus::Excess);
47        assert_eq!(SpotStatus::from(2), SpotStatus::Anomaly);
48
49        // Test default fallback for unknown values
50        assert_eq!(SpotStatus::from(-1), SpotStatus::Normal);
51        assert_eq!(SpotStatus::from(99), SpotStatus::Normal);
52    }
53}