diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 6f29e36..4889b32 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -3,9 +3,9 @@ pub mod error; pub mod hardware; pub mod model_registry; pub mod paths; +pub mod power; pub mod process_watch; pub mod recommendation; -pub mod power; pub mod tuning; pub mod types; diff --git a/crates/core/src/power.rs b/crates/core/src/power.rs index 5712b61..44e5d7b 100644 --- a/crates/core/src/power.rs +++ b/crates/core/src/power.rs @@ -7,6 +7,11 @@ //! now; native probes (IOPSGetProvidingPowerSourceType, //! GetSystemPowerStatus) are deferred. +use std::fs; +use std::path::Path; +use std::sync::{Mutex, OnceLock}; +use std::time::{Duration, Instant}; + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum PowerState { OnAc, @@ -14,11 +19,6 @@ pub enum PowerState { Unknown, } -use std::fs; -use std::path::Path; -use std::sync::{Mutex, OnceLock}; -use std::time::{Duration, Instant}; - /// Parse a `/sys/class/power_supply/`-style directory and decide /// whether the machine is on AC, on battery, or in an unknown state. /// @@ -27,9 +27,15 @@ use std::time::{Duration, Instant}; /// - Else any entry with `type == Battery` → OnBattery. /// - Else → Unknown. /// -/// Failures (missing dir, unreadable files, malformed contents) all -/// fall through to Unknown rather than panicking. `Unknown` is treated -/// as `OnAc` by the caller, which preserves today's pre-clamp +/// Top-level failures (missing dir, unreadable supply_dir) return +/// Unknown without panicking. Per-entry failures (unreadable +/// `type`/`online` file inside an individual supply, e.g. permission +/// denied or non-UTF-8 content) cause that entry to be silently +/// skipped via `read_trimmed().unwrap_or_default()` — which on a +/// stuck-AC machine could produce OnBattery if the Mains entry was +/// the unreadable one. In practice sysfs entries are world-readable, +/// so this is a theoretical hazard rather than a real one. `Unknown` +/// is treated as `OnAc` by the caller, preserving today's pre-clamp /// behaviour on platforms or distros where the probe doesn't fire. pub fn parse_power_state_from_dir(supply_dir: &Path) -> PowerState { let read_dir = match fs::read_dir(supply_dir) {