jolt_platform/
types.rs

1//! Shared types for battery and power monitoring.
2
3use std::fmt;
4
5/// Battery charging state.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
7pub enum ChargeState {
8    /// Battery is actively charging
9    Charging,
10    /// Battery is discharging (on battery power)
11    Discharging,
12    /// Battery is full
13    Full,
14    /// External power connected but not charging (e.g., charge limit reached)
15    NotCharging,
16    /// State cannot be determined
17    #[default]
18    Unknown,
19}
20
21impl ChargeState {
22    /// Returns a human-readable label for the charge state.
23    pub fn label(&self) -> &'static str {
24        match self {
25            ChargeState::Charging => "Charging",
26            ChargeState::Discharging => "On Battery",
27            ChargeState::Full => "Full",
28            ChargeState::NotCharging => "Not Charging",
29            ChargeState::Unknown => "Unknown",
30        }
31    }
32
33    /// Returns true if the battery is currently charging.
34    pub fn is_charging(&self) -> bool {
35        matches!(self, ChargeState::Charging)
36    }
37
38    /// Returns true if external power is connected.
39    pub fn is_plugged_in(&self) -> bool {
40        matches!(
41            self,
42            ChargeState::Charging | ChargeState::Full | ChargeState::NotCharging
43        )
44    }
45}
46
47impl fmt::Display for ChargeState {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        write!(f, "{}", self.label())
50    }
51}
52
53impl From<starship_battery::State> for ChargeState {
54    fn from(state: starship_battery::State) -> Self {
55        match state {
56            starship_battery::State::Charging => ChargeState::Charging,
57            starship_battery::State::Discharging => ChargeState::Discharging,
58            starship_battery::State::Empty => ChargeState::Discharging,
59            starship_battery::State::Full => ChargeState::Full,
60            starship_battery::State::Unknown => ChargeState::Unknown,
61        }
62    }
63}
64
65/// System power mode.
66#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
67pub enum PowerMode {
68    /// Low power / battery saver mode
69    LowPower,
70    /// Automatic / balanced mode
71    #[default]
72    Automatic,
73    /// High performance mode
74    HighPerformance,
75    /// Mode cannot be determined
76    Unknown,
77}
78
79impl PowerMode {
80    /// Returns a human-readable label for the power mode.
81    pub fn label(&self) -> &'static str {
82        match self {
83            PowerMode::LowPower => "Low Power",
84            PowerMode::Automatic => "Automatic",
85            PowerMode::HighPerformance => "High Performance",
86            PowerMode::Unknown => "Unknown",
87        }
88    }
89}
90
91impl fmt::Display for PowerMode {
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        write!(f, "{}", self.label())
94    }
95}
96
97/// Battery technology/chemistry type.
98#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
99pub enum BatteryTechnology {
100    /// Lithium-ion
101    LithiumIon,
102    /// Lithium-polymer
103    LithiumPolymer,
104    /// Nickel-metal hydride
105    NickelMetalHydride,
106    /// Nickel-cadmium
107    NickelCadmium,
108    /// Lead-acid
109    LeadAcid,
110    /// Unknown or unsupported technology
111    #[default]
112    Unknown,
113}
114
115impl BatteryTechnology {
116    /// Returns a human-readable label for the battery technology.
117    pub fn label(&self) -> &'static str {
118        match self {
119            BatteryTechnology::LithiumIon => "Li-ion",
120            BatteryTechnology::LithiumPolymer => "Li-poly",
121            BatteryTechnology::NickelMetalHydride => "NiMH",
122            BatteryTechnology::NickelCadmium => "NiCd",
123            BatteryTechnology::LeadAcid => "Lead-acid",
124            BatteryTechnology::Unknown => "Unknown",
125        }
126    }
127
128    /// Returns a longer description of the battery technology.
129    pub fn description(&self) -> &'static str {
130        match self {
131            BatteryTechnology::LithiumIon => "Lithium-ion",
132            BatteryTechnology::LithiumPolymer => "Lithium-polymer",
133            BatteryTechnology::NickelMetalHydride => "Nickel-metal hydride",
134            BatteryTechnology::NickelCadmium => "Nickel-cadmium",
135            BatteryTechnology::LeadAcid => "Lead-acid",
136            BatteryTechnology::Unknown => "Unknown",
137        }
138    }
139}
140
141impl fmt::Display for BatteryTechnology {
142    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143        write!(f, "{}", self.label())
144    }
145}
146
147impl From<starship_battery::Technology> for BatteryTechnology {
148    fn from(tech: starship_battery::Technology) -> Self {
149        match tech {
150            starship_battery::Technology::LithiumIon => BatteryTechnology::LithiumIon,
151            starship_battery::Technology::LithiumPolymer => BatteryTechnology::LithiumPolymer,
152            starship_battery::Technology::NickelMetalHydride => {
153                BatteryTechnology::NickelMetalHydride
154            }
155            starship_battery::Technology::NickelCadmium => BatteryTechnology::NickelCadmium,
156            starship_battery::Technology::LeadAcid => BatteryTechnology::LeadAcid,
157            starship_battery::Technology::Unknown => BatteryTechnology::Unknown,
158            _ => BatteryTechnology::Unknown,
159        }
160    }
161}
162
163#[cfg(test)]
164mod tests {
165    use super::*;
166
167    #[test]
168    fn test_charge_state_labels() {
169        assert_eq!(ChargeState::Charging.label(), "Charging");
170        assert_eq!(ChargeState::Discharging.label(), "On Battery");
171        assert_eq!(ChargeState::Full.label(), "Full");
172        assert_eq!(ChargeState::NotCharging.label(), "Not Charging");
173        assert_eq!(ChargeState::Unknown.label(), "Unknown");
174    }
175
176    #[test]
177    fn test_charge_state_is_plugged_in() {
178        assert!(ChargeState::Charging.is_plugged_in());
179        assert!(ChargeState::Full.is_plugged_in());
180        assert!(ChargeState::NotCharging.is_plugged_in());
181        assert!(!ChargeState::Discharging.is_plugged_in());
182        assert!(!ChargeState::Unknown.is_plugged_in());
183    }
184
185    #[test]
186    fn test_battery_state_conversion() {
187        assert_eq!(
188            ChargeState::from(starship_battery::State::Charging),
189            ChargeState::Charging
190        );
191        assert_eq!(
192            ChargeState::from(starship_battery::State::Discharging),
193            ChargeState::Discharging
194        );
195        assert_eq!(
196            ChargeState::from(starship_battery::State::Full),
197            ChargeState::Full
198        );
199        assert_eq!(
200            ChargeState::from(starship_battery::State::Empty),
201            ChargeState::Discharging
202        );
203        assert_eq!(
204            ChargeState::from(starship_battery::State::Unknown),
205            ChargeState::Unknown
206        );
207    }
208
209    #[test]
210    fn test_power_mode_labels() {
211        assert_eq!(PowerMode::LowPower.label(), "Low Power");
212        assert_eq!(PowerMode::Automatic.label(), "Automatic");
213        assert_eq!(PowerMode::HighPerformance.label(), "High Performance");
214        assert_eq!(PowerMode::Unknown.label(), "Unknown");
215    }
216}