chore(core): tidy review nits — alphabetical lib.rs, import placement, docstring

End-of-phase cleanup of items flagged across review passes for tasks
1.1, 1.2, and 1.3:

1. crates/core/src/lib.rs: pub mod power moved to alphabetical
   position (between paths and process_watch).
2. crates/core/src/power.rs: use statements moved to top of file
   (above the PowerState enum) per Rust convention.
3. crates/core/src/power.rs: parse_power_state_from_dir docstring
   tightened to acknowledge that read_dir.flatten() silently skips
   per-entry errors. Theoretical hazard only on world-readable
   sysfs, but the previous wording overstated the guarantee.

No behavioural change. All 40 magnotia-core tests still green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
jars
2026-05-09 12:44:09 +01:00
parent a58c2f0994
commit 4f3c063008
2 changed files with 15 additions and 9 deletions

View File

@@ -3,9 +3,9 @@ pub mod error;
pub mod hardware; pub mod hardware;
pub mod model_registry; pub mod model_registry;
pub mod paths; pub mod paths;
pub mod power;
pub mod process_watch; pub mod process_watch;
pub mod recommendation; pub mod recommendation;
pub mod power;
pub mod tuning; pub mod tuning;
pub mod types; pub mod types;

View File

@@ -7,6 +7,11 @@
//! now; native probes (IOPSGetProvidingPowerSourceType, //! now; native probes (IOPSGetProvidingPowerSourceType,
//! GetSystemPowerStatus) are deferred. //! 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)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PowerState { pub enum PowerState {
OnAc, OnAc,
@@ -14,11 +19,6 @@ pub enum PowerState {
Unknown, 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 /// Parse a `/sys/class/power_supply/`-style directory and decide
/// whether the machine is on AC, on battery, or in an unknown state. /// 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 any entry with `type == Battery` → OnBattery.
/// - Else → Unknown. /// - Else → Unknown.
/// ///
/// Failures (missing dir, unreadable files, malformed contents) all /// Top-level failures (missing dir, unreadable supply_dir) return
/// fall through to Unknown rather than panicking. `Unknown` is treated /// Unknown without panicking. Per-entry failures (unreadable
/// as `OnAc` by the caller, which preserves today's pre-clamp /// `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. /// behaviour on platforms or distros where the probe doesn't fire.
pub fn parse_power_state_from_dir(supply_dir: &Path) -> PowerState { pub fn parse_power_state_from_dir(supply_dir: &Path) -> PowerState {
let read_dir = match fs::read_dir(supply_dir) { let read_dir = match fs::read_dir(supply_dir) {