mirror of
https://github.com/RGBCube/superfreq
synced 2025-07-27 17:07:44 +00:00
util/error: init
This commit is contained in:
parent
e6015cf0f1
commit
7d6164c2fe
6 changed files with 81 additions and 73 deletions
40
src/cpu.rs
40
src/cpu.rs
|
@ -1,46 +1,8 @@
|
||||||
use crate::core::{GovernorOverrideMode, TurboSetting};
|
use crate::core::{GovernorOverrideMode, TurboSetting};
|
||||||
|
use crate::util::error::ControlError;
|
||||||
use core::str;
|
use core::str;
|
||||||
use std::{fs, io, path::Path, string::ToString};
|
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<io::Error> 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 {}
|
impl std::error::Error for ControlError {}
|
||||||
|
|
||||||
pub type Result<T, E = ControlError> = std::result::Result<T, E>;
|
pub type Result<T, E = ControlError> = std::result::Result<T, E>;
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use crate::config::{AppConfig, ProfileConfig};
|
use crate::config::{AppConfig, ProfileConfig};
|
||||||
use crate::core::{OperationalMode, SystemReport, TurboSetting};
|
use crate::core::{OperationalMode, SystemReport, TurboSetting};
|
||||||
use crate::cpu::{self, ControlError};
|
use crate::cpu::{self};
|
||||||
|
use crate::util::error::ControlError;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum EngineError {
|
pub enum EngineError {
|
||||||
|
@ -66,8 +67,8 @@ pub fn determine_and_apply_settings(
|
||||||
// If no batteries, assume AC power (desktop).
|
// If no batteries, assume AC power (desktop).
|
||||||
// Otherwise, check the ac_connected status from the (first) battery.
|
// Otherwise, check the ac_connected status from the (first) battery.
|
||||||
// XXX: This relies on the setting ac_connected in BatteryInfo being set correctly.
|
// XXX: This relies on the setting ac_connected in BatteryInfo being set correctly.
|
||||||
let on_ac_power = report.batteries.is_empty()
|
let on_ac_power =
|
||||||
|| report.batteries.first().is_some_and(|b| b.ac_connected);
|
report.batteries.is_empty() || report.batteries.first().is_some_and(|b| b.ac_connected);
|
||||||
|
|
||||||
if on_ac_power {
|
if on_ac_power {
|
||||||
println!("Engine: On AC power, selecting Charger profile.");
|
println!("Engine: On AC power, selecting Charger profile.");
|
||||||
|
|
|
@ -5,9 +5,11 @@ mod cpu;
|
||||||
mod daemon;
|
mod daemon;
|
||||||
mod engine;
|
mod engine;
|
||||||
mod monitor;
|
mod monitor;
|
||||||
|
mod util;
|
||||||
|
|
||||||
use crate::config::AppConfig;
|
use crate::config::AppConfig;
|
||||||
use crate::core::{GovernorOverrideMode, TurboSetting};
|
use crate::core::{GovernorOverrideMode, TurboSetting};
|
||||||
|
use crate::util::error::ControlError;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
|
@ -216,8 +218,8 @@ fn main() {
|
||||||
// For example, check if e.downcast_ref::<cpu::ControlError>() matches PermissionDenied
|
// For example, check if e.downcast_ref::<cpu::ControlError>() matches PermissionDenied
|
||||||
// and print a more specific message like "Try running with sudo."
|
// and print a more specific message like "Try running with sudo."
|
||||||
// We'll revisit this in the future once CPU logic is more stable.
|
// We'll revisit this in the future once CPU logic is more stable.
|
||||||
if let Some(control_error) = e.downcast_ref::<cpu::ControlError>() {
|
if let Some(control_error) = e.downcast_ref::<ControlError>() {
|
||||||
if matches!(control_error, cpu::ControlError::PermissionDenied(_)) {
|
if matches!(control_error, ControlError::PermissionDenied(_)) {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"Hint: This operation may require administrator privileges (e.g., run with sudo)."
|
"Hint: This operation may require administrator privileges (e.g., run with sudo)."
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
use crate::config::AppConfig;
|
use crate::config::AppConfig;
|
||||||
use crate::core::{BatteryInfo, CpuCoreInfo, CpuGlobalInfo, SystemInfo, SystemLoad, SystemReport};
|
use crate::core::{BatteryInfo, CpuCoreInfo, CpuGlobalInfo, SystemInfo, SystemLoad, SystemReport};
|
||||||
|
use crate::util::error::ControlError;
|
||||||
|
use crate::util::error::SysMonitorError;
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
fs, io,
|
fs, io,
|
||||||
|
@ -10,35 +12,6 @@ use std::{
|
||||||
time::SystemTime,
|
time::SystemTime,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum SysMonitorError {
|
|
||||||
Io(io::Error),
|
|
||||||
ReadError(String),
|
|
||||||
ParseError(String),
|
|
||||||
ProcStatParseError(String),
|
|
||||||
NotAvailable(String),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<io::Error> 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 {}
|
impl std::error::Error for SysMonitorError {}
|
||||||
|
|
||||||
pub type Result<T, E = SysMonitorError> = std::result::Result<T, E>;
|
pub type Result<T, E = SysMonitorError> = std::result::Result<T, E>;
|
||||||
|
|
69
src/util/error.rs
Normal file
69
src/util/error.rs
Normal file
|
@ -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<io::Error> 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<io::Error> 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}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
src/util/mod.rs
Normal file
1
src/util/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod error;
|
Loading…
Add table
Add a link
Reference in a new issue