| 1 | //! Uses the battery crate. |
| 2 | //! |
| 3 | //! Covers battery usage for: |
| 4 | //! - Linux 2.6.39+ |
| 5 | //! - MacOS 10.10+ |
| 6 | //! - iOS |
| 7 | //! - Windows 7+ |
| 8 | //! - FreeBSD |
| 9 | //! - DragonFlyBSD |
| 10 | //! |
| 11 | //! For more information, refer to the [starship_battery](https://github.com/starship/rust-battery) repo/docs. |
| 12 | |
| 13 | use starship_batteryrust-analyzer cargo starship-battery 0.11.1 crate/::{ |
| 14 | Batteryrust-analyzer cargo starship-battery 0.11.1 types/battery/Battery#, Managerrust-analyzer cargo starship-battery 0.11.1 types/manager/Manager#, Staterust-analyzer cargo starship-battery 0.11.1 types/state/State#, |
| 15 | unitsrust-analyzer cargo starship-battery 0.11.1 units/::{powerrust-analyzer cargo uom 0.38.0 si/power/::wattrust-analyzer cargo uom 0.38.0 si/power/watt#, ratiorust-analyzer cargo uom 0.38.0 si/ratio/::percentrust-analyzer cargo uom 0.38.0 si/ratio/percent#, timerust-analyzer cargo uom 0.38.0 si/time/::secondrust-analyzer cargo uom 0.38.0 si/time/second#}, |
| 16 | }; |
| 17 | |
| 18 | /// Battery state. |
| 19 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 20 | pub enum BatteryStaterust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState# { |
| 21 | Chargingrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Charging# { |
| 22 | /// Time to full in seconds. |
| 23 | time_to_fullrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Charging#time_to_full.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u32>, |
| 24 | }, |
| 25 | Dischargingrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Discharging# { |
| 26 | /// Time to empty in seconds. |
| 27 | time_to_emptyrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Discharging#time_to_empty.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u32>, |
| 28 | }, |
| 29 | Emptyrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Empty#, |
| 30 | Fullrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Full#, |
| 31 | Unknownrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Unknown#, |
| 32 | } |
| 33 | |
| 34 | impl BatteryStaterust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState# { |
| 35 | /// Return the string representation. |
| 36 | pub fn as_strrust-analyzer cargo bottom 0.14.9 collection/batteries/impl#[BatteryState]as_str().(&self) -> &'static str { |
| 37 | match self { |
| 38 | BatteryStaterust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#::Chargingrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Charging# { .. } => "Charging", |
| 39 | BatteryStaterust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#::Dischargingrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Discharging# { .. } => "Discharging", |
| 40 | BatteryStaterust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#::Emptyrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Empty# => "Empty", |
| 41 | BatteryStaterust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#::Fullrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Full# => "Full", |
| 42 | BatteryStaterust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#::Unknownrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Unknown# => "Unknown", |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 48 | pub struct BatteryDatarust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryData# { |
| 49 | /// Current charge percent. |
| 50 | pub charge_percentrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryData#charge_percent.: f64, |
| 51 | /// Power consumption, in watts. |
| 52 | pub power_consumptionrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryData#power_consumption.: f64, |
| 53 | /// Reported battery health. |
| 54 | pub health_percentrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryData#health_percent.: f64, |
| 55 | /// The current battery "state" (e.g. is it full, charging, etc.). |
| 56 | pub staterust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryData#state.: BatteryStaterust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#, |
| 57 | } |
| 58 | |
| 59 | impl BatteryDatarust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryData# { |
| 60 | pub fn watt_consumptionrust-analyzer cargo bottom 0.14.9 collection/batteries/impl#[BatteryData]watt_consumption().(&self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 61 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:.2}W", self.power_consumptionrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryData#power_consumption.) |
| 62 | } |
| 63 | |
| 64 | pub fn healthrust-analyzer cargo bottom 0.14.9 collection/batteries/impl#[BatteryData]health().(&self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 65 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:.2}%", self.health_percentrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryData#health_percent.) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | pub fn refresh_batteriesrust-analyzer cargo bottom 0.14.9 collection/batteries/refresh_batteries().(manager: &Managerrust-analyzer cargo starship-battery 0.11.1 types/manager/Manager#, batteries: &mut [Batteryrust-analyzer cargo starship-battery 0.11.1 types/battery/Battery#]) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<BatteryDatarust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryData#> { |
| 70 | batteries |
| 71 | .iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().() |
| 72 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|battery| { |
| 73 | if manager.refreshrust-analyzer cargo starship-battery 0.11.1 types/manager/impl#[Manager]refresh().(battery).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().() { |
| 74 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(BatteryDatarust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryData# { |
| 75 | charge_percentrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryData#charge_percent.: f64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[f64][`From<f32>`]from().(battery.state_of_chargerust-analyzer cargo starship-battery 0.11.1 types/battery/impl#[Battery]state_of_charge().().getrust-analyzer cargo uom 0.38.0 si/ratio/impl#[`super::Quantity<dyn Dimension<L = typenum::Z0, M = typenum::Z0, T = typenum::Z0, I = typenum::Z0, Th = typenum::Z0, N = typenum::Z0, J = typenum::Z0, Kind = dyn Kind + 'static> + 'static, U, V>`]get().::<percentrust-analyzer cargo uom 0.38.0 si/ratio/percent#>()), |
| 76 | power_consumptionrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryData#power_consumption.: f64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[f64][`From<f32>`]from().(battery.energy_raterust-analyzer cargo starship-battery 0.11.1 types/battery/impl#[Battery]energy_rate().().getrust-analyzer cargo uom 0.38.0 si/power/impl#[`super::Quantity<dyn Dimension<L = typenum::PInt<typenum::UInt<typenum::UInt<typenum::UTerm, typenum::B1>, typenum::B0>>, M = typenum::PInt<typenum::UInt<typenum::UTerm, typenum::B1>>, T = typenum::NInt<typenum::UInt<typenum::UInt<typenum::UTerm, typenum::B1>, typenum::B1>>, I = typenum::Z0, Th = typenum::Z0, N = typenum::Z0, J = typenum::Z0, Kind = dyn Kind + 'static> + 'static, U, V>`]get().::<wattrust-analyzer cargo uom 0.38.0 si/power/watt#>()), |
| 77 | health_percentrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryData#health_percent.: f64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[f64][`From<f32>`]from().(battery.state_of_healthrust-analyzer cargo starship-battery 0.11.1 types/battery/impl#[Battery]state_of_health().().getrust-analyzer cargo uom 0.38.0 si/ratio/impl#[`super::Quantity<dyn Dimension<L = typenum::Z0, M = typenum::Z0, T = typenum::Z0, I = typenum::Z0, Th = typenum::Z0, N = typenum::Z0, J = typenum::Z0, Kind = dyn Kind + 'static> + 'static, U, V>`]get().::<percentrust-analyzer cargo uom 0.38.0 si/ratio/percent#>()), |
| 78 | staterust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryData#state.: match battery.staterust-analyzer cargo starship-battery 0.11.1 types/battery/impl#[Battery]state().() { |
| 79 | Staterust-analyzer cargo starship-battery 0.11.1 types/state/State#::Unknownrust-analyzer cargo starship-battery 0.11.1 types/state/State#Unknown# => BatteryStaterust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#::Unknownrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Unknown#, |
| 80 | Staterust-analyzer cargo starship-battery 0.11.1 types/state/State#::Chargingrust-analyzer cargo starship-battery 0.11.1 types/state/State#Charging# => BatteryStaterust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#::Chargingrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Charging# { |
| 81 | time_to_fullrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Charging#time_to_full.: { |
| 82 | let optional_time = battery.time_to_fullrust-analyzer cargo starship-battery 0.11.1 types/battery/impl#[Battery]time_to_full().(); |
| 83 | optional_time.maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|time| f64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[f64][`From<f32>`]from().(time.getrust-analyzer cargo uom 0.38.0 si/time/impl#[`super::Quantity<dyn Dimension<L = typenum::Z0, M = typenum::Z0, T = typenum::PInt<typenum::UInt<typenum::UTerm, typenum::B1>>, I = typenum::Z0, Th = typenum::Z0, N = typenum::Z0, J = typenum::Z0, Kind = dyn Kind + 'static> + 'static, U, V>`]get().::<secondrust-analyzer cargo uom 0.38.0 si/time/second#>()) as u32) |
| 84 | }, |
| 85 | }, |
| 86 | Staterust-analyzer cargo starship-battery 0.11.1 types/state/State#::Dischargingrust-analyzer cargo starship-battery 0.11.1 types/state/State#Discharging# => BatteryStaterust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#::Dischargingrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Discharging# { |
| 87 | time_to_emptyrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Discharging#time_to_empty.: { |
| 88 | let optional_time = battery.time_to_emptyrust-analyzer cargo starship-battery 0.11.1 types/battery/impl#[Battery]time_to_empty().(); |
| 89 | optional_time.maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|time| f64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[f64][`From<f32>`]from().(time.getrust-analyzer cargo uom 0.38.0 si/time/impl#[`super::Quantity<dyn Dimension<L = typenum::Z0, M = typenum::Z0, T = typenum::PInt<typenum::UInt<typenum::UTerm, typenum::B1>>, I = typenum::Z0, Th = typenum::Z0, N = typenum::Z0, J = typenum::Z0, Kind = dyn Kind + 'static> + 'static, U, V>`]get().::<secondrust-analyzer cargo uom 0.38.0 si/time/second#>()) as u32) |
| 90 | }, |
| 91 | }, |
| 92 | Staterust-analyzer cargo starship-battery 0.11.1 types/state/State#::Emptyrust-analyzer cargo starship-battery 0.11.1 types/state/State#Empty# => BatteryStaterust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#::Emptyrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Empty#, |
| 93 | Staterust-analyzer cargo starship-battery 0.11.1 types/state/State#::Fullrust-analyzer cargo starship-battery 0.11.1 types/state/State#Full# => BatteryStaterust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#::Fullrust-analyzer cargo bottom 0.14.9 collection/batteries/BatteryState#Full#, |
| 94 | }, |
| 95 | }) |
| 96 | } else { |
| 97 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 98 | } |
| 99 | }) |
| 100 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>() |
| 101 | } |