diff --git a/Cargo.lock b/Cargo.lock index 788b83e..abefa37 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -335,9 +335,9 @@ dependencies = [ [[package]] name = "either" -version = "1.6.1" +version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "either_n" diff --git a/pisugar-server/src/cmds.rs b/pisugar-server/src/cmds.rs index f046337..cdfb8da 100644 --- a/pisugar-server/src/cmds.rs +++ b/pisugar-server/src/cmds.rs @@ -59,7 +59,7 @@ pub enum Cmds { SetButtonEnable { mode: ButtonMode, - enable: bool, + enable: BoolValue, }, SetButtonShell { @@ -161,10 +161,38 @@ pub struct BatteryRangeArgs { pub max: f32, } -#[derive(Debug, Args, PartialEq)] +#[derive(Debug, Args, PartialEq, Clone)] pub struct BoolArg { #[arg(action = ArgAction::Set)] - pub enable: bool, + pub enable: BoolValue, +} + +impl BoolArg { + const TRUE: Self = Self { + enable: BoolValue(true), + }; + const FALSE: Self = Self { + enable: BoolValue(false), + }; + + pub fn value(&self) -> bool { + *self == Self::TRUE + } +} + +#[derive(Debug, PartialEq, Clone)] +pub struct BoolValue(pub bool); + +impl From for BoolValue { + fn from(value: String) -> Self { + if let Ok(b) = bool::from_str(&value) { + return Self(b); + } + if let Ok(n) = u32::from_str(&value) { + return Self(n != 0); + } + return Self(false); + } } #[cfg(test)] @@ -182,8 +210,9 @@ mod tests { #[case("get button_enable single", Cmds::Get(GetCmds::ButtonEnable{ mode: ButtonMode::Single }))] #[case("get button_shell long", Cmds::Get(GetCmds::ButtonShell { mode: ButtonMode::Long } ))] #[case("set_battery_charging_range 30.0,80.0", Cmds::SetBatteryChargingRange{ range: vec![30.0, 80.0]})] - #[case("set_battery_output true", Cmds::SetBatteryOutput(BoolArg{ enable: true }))] - #[case("set_battery_output false", Cmds::SetBatteryOutput(BoolArg { enable: false }))] + #[case("set_battery_output true", Cmds::SetBatteryOutput(BoolArg::TRUE))] + #[case("set_battery_output false", Cmds::SetBatteryOutput(BoolArg::FALSE))] + #[case("set_button_enable single 1", Cmds::SetButtonEnable { mode: ButtonMode::Single, enable: BoolValue(true) })] #[case("set_button_shell single echo hello", Cmds::SetButtonShell { mode: ButtonMode::Single, shell: vec!["echo".to_string(), "hello".to_string()] })] #[case("set_soft_poweroff_shell shutdown -a", Cmds::SetSoftPoweroffShell { shell: vec!["shutdown".to_string(), "-a".to_string()] })] #[case("set_soft_poweroff_shell bash \"shutdown -a\"", Cmds::SetSoftPoweroffShell { shell: vec!["bash".to_string(), "shutdown -a".to_string()] })] diff --git a/pisugar-server/src/main.rs b/pisugar-server/src/main.rs index 4fb62d5..48710cd 100644 --- a/pisugar-server/src/main.rs +++ b/pisugar-server/src/main.rs @@ -15,7 +15,7 @@ use std::str::FromStr; use anyhow::{anyhow, bail, Result}; use chrono::prelude::*; use clap::{Arg, ArgAction, Command}; -use cmds::{BoolArg, ButtonMode, Cmds}; +use cmds::{ButtonMode, Cmds}; use digest_auth::{AuthContext, AuthorizationHeader, Charset, Qop, WwwAuthenticateHeader}; use env_logger::Env; use futures::prelude::*; @@ -168,18 +168,18 @@ fn handle_request(core: Arc>, req: &str) -> String { core.set_charging_range(charging_range) .map(|_| format!("{}: done\n", parts[0])) } - Cmds::SetBatteryInputProtect(BoolArg { enable }) => core - .toggle_input_protected(*enable) + Cmds::SetBatteryInputProtect(b) => core + .toggle_input_protected(b.value()) .map(|_| format!("{}: done\n", parts[0])), - Cmds::SetBatteryOutput(BoolArg { enable }) => core - .toggle_output_enabled(*enable) + Cmds::SetBatteryOutput(b) => core + .toggle_output_enabled(b.value()) .map(|_| format!("{}: done\n", parts[0])), Cmds::SetFullChargeDuration { seconds } => { core.config_mut().full_charge_duration = Some(*seconds); core.save_config().map(|_| format!("{}: done\n", parts[0])) } - Cmds::SetAllowCharging(BoolArg { enable }) => core - .toggle_allow_charging(*enable) + Cmds::SetAllowCharging(b) => core + .toggle_allow_charging(b.value()) .map(|_| format!("{}: done\n", parts[0])), Cmds::RtcClearFlag => core.clear_alarm_flag().map(|_| format!("{}: done\n", parts[0])), Cmds::RtcPi2rtc => core.write_time(Local::now()).map(|_| format!("{}: done\n", parts[0])), @@ -256,9 +256,9 @@ fn handle_request(core: Arc>, req: &str) -> String { .map(|_| format!("{}: wakeup after 1 min 30 sec\n", parts[0])), Cmds::SetButtonEnable { mode, enable } => { match *mode { - ButtonMode::Single => core.config_mut().single_tap_enable = *enable, - ButtonMode::Double => core.config_mut().double_tap_enable = *enable, - ButtonMode::Long => core.config_mut().long_tap_enable = *enable, + ButtonMode::Single => core.config_mut().single_tap_enable = enable.0, + ButtonMode::Double => core.config_mut().double_tap_enable = enable.0, + ButtonMode::Long => core.config_mut().long_tap_enable = enable.0, } if let Err(e) = core.save_config() { log::error!("{}", e); @@ -277,8 +277,8 @@ fn handle_request(core: Arc>, req: &str) -> String { } Ok(format!("{}: done\n", parts[0])) } - Cmds::SetAutoPowerOn(BoolArg { enable }) => core - .toggle_auto_power_on(*enable) + Cmds::SetAutoPowerOn(b) => core + .toggle_auto_power_on(b.value()) .map(|_| format!("{}: done\n", parts[0])), Cmds::SetAuth { username, password } => { if let (Some(username), Some(password)) = (username, password) { @@ -291,11 +291,11 @@ fn handle_request(core: Arc>, req: &str) -> String { core.save_config().map(|_| format!("{}: done\n", parts[0])) } Cmds::ForceShutdown => core.force_shutdown().map(|_| format!("{}: done\n", parts[0])), - Cmds::SetAntiMistouch(BoolArg { enable }) => core - .toggle_anti_mistouch(*enable) + Cmds::SetAntiMistouch(b) => core + .toggle_anti_mistouch(b.value()) .map(|_| format!("{}: done\n", parts[0])), - Cmds::SetSoftPoweroff(BoolArg { enable }) => core - .toggle_soft_poweroff(*enable) + Cmds::SetSoftPoweroff(b) => core + .toggle_soft_poweroff(b.value()) .map(|_| format!("{}: done\n", parts[0])), Cmds::SetSoftPoweroffShell { shell } => { let script = shell.join(" "); @@ -306,8 +306,8 @@ fn handle_request(core: Arc>, req: &str) -> String { }; core.save_config().map(|_| format!("{}: done\n", parts[0])) } - Cmds::SetInputProtect(BoolArg { enable }) => core - .toggle_input_protected(*enable) + Cmds::SetInputProtect(b) => core + .toggle_input_protected(b.value()) .map(|_| format!("{}: done\n", parts[0])), }; @@ -319,7 +319,7 @@ fn handle_request(core: Arc>, req: &str) -> String { r } Err(e) => { - log::warn!("{}", e); + log::warn!("Request: {}, error: {}", req, e); err } }