diff --git a/src/cpu.rs b/src/cpu.rs index e97d9e1..36f9667 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -1,46 +1,8 @@ use crate::core::{GovernorOverrideMode, TurboSetting}; +use crate::util::error::ControlError; use core::str; use std::{fs, io, path::Path, string::ToString}; -#[derive(Debug)] -pub enum ControlError { - Io(io::Error), - WriteError(String), - InvalidValueError(String), - NotSupported(String), - PermissionDenied(String), - InvalidProfile(String), -} - -impl From for ControlError { - fn from(err: io::Error) -> Self { - match err.kind() { - io::ErrorKind::PermissionDenied => Self::PermissionDenied(err.to_string()), - _ => Self::Io(err), - } - } -} - -impl std::fmt::Display for ControlError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::Io(e) => write!(f, "I/O error: {e}"), - Self::WriteError(s) => write!(f, "Failed to write to sysfs path: {s}"), - Self::InvalidValueError(s) => write!(f, "Invalid value for setting: {s}"), - Self::NotSupported(s) => write!(f, "Control action not supported: {s}"), - Self::PermissionDenied(s) => { - write!(f, "Permission denied: {s}. Try running with sudo.") - } - Self::InvalidProfile(s) => { - write!( - f, - "Invalid platform control profile {s} supplied, please provide a valid one." - ) - } - } - } -} - impl std::error::Error for ControlError {} pub type Result = std::result::Result; diff --git a/src/engine.rs b/src/engine.rs index a047196..4f031d2 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1,6 +1,7 @@ use crate::config::{AppConfig, ProfileConfig}; use crate::core::{OperationalMode, SystemReport, TurboSetting}; -use crate::cpu::{self, ControlError}; +use crate::cpu::{self}; +use crate::util::error::ControlError; #[derive(Debug)] pub enum EngineError { @@ -66,8 +67,8 @@ pub fn determine_and_apply_settings( // If no batteries, assume AC power (desktop). // Otherwise, check the ac_connected status from the (first) battery. // XXX: This relies on the setting ac_connected in BatteryInfo being set correctly. - let on_ac_power = report.batteries.is_empty() - || report.batteries.first().is_some_and(|b| b.ac_connected); + let on_ac_power = + report.batteries.is_empty() || report.batteries.first().is_some_and(|b| b.ac_connected); if on_ac_power { println!("Engine: On AC power, selecting Charger profile."); diff --git a/src/main.rs b/src/main.rs index f114a3f..83fb35c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,9 +5,11 @@ mod cpu; mod daemon; mod engine; mod monitor; +mod util; use crate::config::AppConfig; use crate::core::{GovernorOverrideMode, TurboSetting}; +use crate::util::error::ControlError; use clap::Parser; #[derive(Parser, Debug)] @@ -216,8 +218,8 @@ fn main() { // For example, check if e.downcast_ref::() matches PermissionDenied // and print a more specific message like "Try running with sudo." // We'll revisit this in the future once CPU logic is more stable. - if let Some(control_error) = e.downcast_ref::() { - if matches!(control_error, cpu::ControlError::PermissionDenied(_)) { + if let Some(control_error) = e.downcast_ref::() { + if matches!(control_error, ControlError::PermissionDenied(_)) { eprintln!( "Hint: This operation may require administrator privileges (e.g., run with sudo)." ); diff --git a/src/monitor.rs b/src/monitor.rs index a7918b3..08bdfd5 100644 --- a/src/monitor.rs +++ b/src/monitor.rs @@ -1,5 +1,7 @@ use crate::config::AppConfig; use crate::core::{BatteryInfo, CpuCoreInfo, CpuGlobalInfo, SystemInfo, SystemLoad, SystemReport}; +use crate::util::error::ControlError; +use crate::util::error::SysMonitorError; use std::{ collections::HashMap, fs, io, @@ -10,35 +12,6 @@ use std::{ time::SystemTime, }; -#[derive(Debug)] -pub enum SysMonitorError { - Io(io::Error), - ReadError(String), - ParseError(String), - ProcStatParseError(String), - NotAvailable(String), -} - -impl From for SysMonitorError { - fn from(err: io::Error) -> Self { - Self::Io(err) - } -} - -impl std::fmt::Display for SysMonitorError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::Io(e) => write!(f, "I/O error: {e}"), - Self::ReadError(s) => write!(f, "Failed to read sysfs path: {s}"), - Self::ParseError(s) => write!(f, "Failed to parse value: {s}"), - Self::ProcStatParseError(s) => { - write!(f, "Failed to parse /proc/stat: {s}") - } - Self::NotAvailable(s) => write!(f, "Information not available: {s}"), - } - } -} - impl std::error::Error for SysMonitorError {} pub type Result = std::result::Result; diff --git a/src/util/error.rs b/src/util/error.rs new file mode 100644 index 0000000..1dc49a9 --- /dev/null +++ b/src/util/error.rs @@ -0,0 +1,69 @@ +use std::io; + +#[derive(Debug)] +pub enum ControlError { + Io(io::Error), + WriteError(String), + InvalidValueError(String), + NotSupported(String), + PermissionDenied(String), + InvalidProfile(String), +} + +impl From for ControlError { + fn from(err: io::Error) -> Self { + match err.kind() { + io::ErrorKind::PermissionDenied => Self::PermissionDenied(err.to_string()), + _ => Self::Io(err), + } + } +} + +impl std::fmt::Display for ControlError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Io(e) => write!(f, "I/O error: {e}"), + Self::WriteError(s) => write!(f, "Failed to write to sysfs path: {s}"), + Self::InvalidValueError(s) => write!(f, "Invalid value for setting: {s}"), + Self::NotSupported(s) => write!(f, "Control action not supported: {s}"), + Self::PermissionDenied(s) => { + write!(f, "Permission denied: {s}. Try running with sudo.") + } + Self::InvalidProfile(s) => { + write!( + f, + "Invalid platform control profile {s} supplied, please provide a valid one." + ) + } + } + } +} + +#[derive(Debug)] +pub enum SysMonitorError { + Io(io::Error), + ReadError(String), + ParseError(String), + ProcStatParseError(String), + NotAvailable(String), +} + +impl From for SysMonitorError { + fn from(err: io::Error) -> Self { + Self::Io(err) + } +} + +impl std::fmt::Display for SysMonitorError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Io(e) => write!(f, "I/O error: {e}"), + Self::ReadError(s) => write!(f, "Failed to read sysfs path: {s}"), + Self::ParseError(s) => write!(f, "Failed to parse value: {s}"), + Self::ProcStatParseError(s) => { + write!(f, "Failed to parse /proc/stat: {s}") + } + Self::NotAvailable(s) => write!(f, "Information not available: {s}"), + } + } +} diff --git a/src/util/mod.rs b/src/util/mod.rs new file mode 100644 index 0000000..a91e735 --- /dev/null +++ b/src/util/mod.rs @@ -0,0 +1 @@ +pub mod error;