From 4e03679209dd0bf824b60d7226909155b68cea18 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Fri, 16 May 2025 03:18:50 +0300 Subject: [PATCH] cli: use Clap's value parser for cheaper validation --- src/main.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 471b18e..0b0f1ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ mod util; use crate::config::AppConfig; use crate::core::{GovernorOverrideMode, TurboSetting}; use crate::util::error::ControlError; -use clap::Parser; +use clap::{Parser, value_parser}; use env_logger::Builder; use log::{debug, error, info}; use std::sync::Once; @@ -81,8 +81,10 @@ enum Commands { /// Set battery charge thresholds to extend battery lifespan SetBatteryThresholds { /// Percentage at which charging starts (when below this value) + #[clap(value_parser = value_parser!(u8).range(0..=99))] start_threshold: u8, /// Percentage at which charging stops (when it reaches this value) + #[clap(value_parser = value_parser!(u8).range(1..=100))] stop_threshold: u8, }, } @@ -409,7 +411,7 @@ fn main() { start_threshold, stop_threshold, }) => { - // Basic validation to provide proper error messages at the CLI level + // We only need to check if start < stop since the range validation is handled by Clap if start_threshold >= stop_threshold { error!( "Start threshold ({start_threshold}) must be less than stop threshold ({stop_threshold})" @@ -417,16 +419,6 @@ fn main() { Err(Box::new(ControlError::InvalidValueError(format!( "Start threshold ({start_threshold}) must be less than stop threshold ({stop_threshold})" ))) as Box) - } else if stop_threshold > 100 { - error!("Stop threshold ({stop_threshold}) cannot exceed 100%"); - Err(Box::new(ControlError::InvalidValueError(format!( - "Stop threshold ({stop_threshold}) cannot exceed 100%" - ))) as Box) - } else if start_threshold == 0 || stop_threshold == 0 { - error!("Thresholds must be greater than 0%"); - Err(Box::new(ControlError::InvalidValueError( - "Thresholds must be greater than 0%".to_string(), - )) as Box) } else { info!( "Setting battery thresholds: start at {start_threshold}%, stop at {stop_threshold}%"