From 0973952eeedcf490c1377ed5a3e5c6c43e1a18fd Mon Sep 17 00:00:00 2001 From: Kurtis Dinelle Date: Mon, 19 Jan 2026 10:35:46 -0800 Subject: [PATCH] Implement TryFrom for ACPI types --- embedded-batteries/src/acpi.rs | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/embedded-batteries/src/acpi.rs b/embedded-batteries/src/acpi.rs index 2993ebc..b4164cf 100644 --- a/embedded-batteries/src/acpi.rs +++ b/embedded-batteries/src/acpi.rs @@ -189,6 +189,17 @@ impl From for u32 { } } +impl TryFrom for PowerUnit { + type Error = (); + fn try_from(value: u32) -> Result { + match value { + 0 => Ok(Self::MilliWatts), + 1 => Ok(Self::MilliAmps), + _ => Err(()), + } + } +} + /// Battery Technology. #[repr(u32)] #[derive(Default, Copy, Clone, PartialEq, Eq, IntoBytes, Immutable)] @@ -210,6 +221,17 @@ impl From for u32 { } } +impl TryFrom for BatteryTechnology { + type Error = (); + fn try_from(value: u32) -> Result { + match value { + 0 => Ok(Self::Primary), + 1 => Ok(Self::Secondary), + _ => Err(()), + } + } +} + /// Battery Swapping Capability. #[derive(Default, Copy, Clone, PartialEq, Eq, IntoBytes, Immutable)] #[repr(u32)] @@ -234,6 +256,18 @@ impl From for u32 { } } +impl TryFrom for BatterySwapCapability { + type Error = (); + fn try_from(value: u32) -> Result { + match value { + 0 => Ok(Self::NonSwappable), + 1 => Ok(Self::ColdSwappable), + 2 => Ok(Self::HotSwappable), + _ => Err(()), + } + } +} + /// PSR: Power Source Status. /// /// Represents whether a power source (e.g., AC adapter) is currently online or offline. @@ -272,6 +306,17 @@ impl From for u32 { } } +impl TryFrom for PowerSource { + type Error = (); + fn try_from(value: u32) -> Result { + match value { + 0 => Ok(Self::Offline), + 1 => Ok(Self::Online), + _ => Err(()), + } + } +} + /// PIF: Power Source Information. /// /// Represents static information about a power source device. This information @@ -451,6 +496,18 @@ pub enum ThresholdId { SustainablePeakPower = 2, } +impl TryFrom for ThresholdId { + type Error = (); + fn try_from(value: u32) -> Result { + match value { + 0 => Ok(Self::ClearAll), + 1 => Ok(Self::InstantaneousPeakPower), + 2 => Ok(Self::SustainablePeakPower), + _ => Err(()), + } + } +} + /// Return codes for BPT operations. #[repr(u32)] #[derive(Copy, Clone, PartialEq, Eq)]