From 760d5be8ebae854b193b1f95d37262acc9207771 Mon Sep 17 00:00:00 2001 From: Luke D Jones Date: Tue, 11 Aug 2020 14:36:37 +1200 Subject: [PATCH] Handle config errors better --- asus-nb-ctrl/src/config.rs | 51 +++++++++++++++++++-------------- asus-nb-ctrl/src/ctrl_charge.rs | 2 +- asus-nb-ctrl/src/ctrl_leds.rs | 6 ++-- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/asus-nb-ctrl/src/config.rs b/asus-nb-ctrl/src/config.rs index c1d26ae5..3b385893 100644 --- a/asus-nb-ctrl/src/config.rs +++ b/asus-nb-ctrl/src/config.rs @@ -1,4 +1,5 @@ use asus_nb::aura_modes::AuraModes; +use log::{error, warn}; use serde_derive::{Deserialize, Serialize}; use std::fs::{File, OpenOptions}; use std::io::{Read, Write}; @@ -9,7 +10,7 @@ pub static CONFIG_PATH: &str = "/etc/asusd/asusd.conf"; pub struct Config { pub power_profile: u8, pub bat_charge_limit: u8, - pub kbd_boot_brightness: u8, + pub kbd_led_brightness: u8, pub kbd_backlight_mode: u8, pub kbd_backlight_modes: Vec, pub power_profiles: FanModeProfile, @@ -24,42 +25,48 @@ impl Config { .write(true) .create(true) .open(&CONFIG_PATH) - .expect("config file error"); + .unwrap(); // okay to cause panic here let mut buf = String::new(); if let Ok(l) = file.read_to_string(&mut buf) { if l == 0 { - // create a default config here - let mut c = Config::default(); - c.bat_charge_limit = 100; - c.kbd_backlight_mode = 0; - c.kbd_boot_brightness = 1; - - for n in supported_led_modes { - c.kbd_backlight_modes.push(AuraModes::from(*n)) - } - - // Should be okay to unwrap this as is since it is a Default - let json = serde_json::to_string_pretty(&c).unwrap(); - file.write_all(json.as_bytes()) - .unwrap_or_else(|_| panic!("Could not deserialise {}", CONFIG_PATH)); - self = c; + self = Config::create_default(&mut file, &supported_led_modes); } else { - self = serde_json::from_str(&buf) - .unwrap_or_else(|_| panic!("Could not deserialise {}", CONFIG_PATH)); + self = serde_json::from_str(&buf).unwrap_or_else(|_| { + warn!("Could not deserialise {}", CONFIG_PATH); + Config::create_default(&mut file, &supported_led_modes) + }); } } self } + fn create_default(file: &mut File, supported_led_modes: &[u8]) -> Self { + // create a default config here + let mut c = Config::default(); + c.bat_charge_limit = 100; + c.kbd_backlight_mode = 0; + c.kbd_led_brightness = 1; + + for n in supported_led_modes { + c.kbd_backlight_modes.push(AuraModes::from(*n)) + } + + // Should be okay to unwrap this as is since it is a Default + let json = serde_json::to_string_pretty(&c).unwrap(); + file.write_all(json.as_bytes()) + .unwrap_or_else(|_| panic!("Could not write {}", CONFIG_PATH)); + c + } + pub fn read(&mut self) { let mut file = OpenOptions::new() .read(true) .open(&CONFIG_PATH) - .expect("config file error"); + .unwrap_or_else(|err| panic!("Error reading {}: {}", CONFIG_PATH, err)); let mut buf = String::new(); if let Ok(l) = file.read_to_string(&mut buf) { if l == 0 { - panic!("Missing {}", CONFIG_PATH); + warn!("File is empty {}", CONFIG_PATH); } else { let x: Config = serde_json::from_str(&buf) .unwrap_or_else(|_| panic!("Could not deserialise {}", CONFIG_PATH)); @@ -72,7 +79,7 @@ impl Config { let mut file = File::create(CONFIG_PATH).expect("Couldn't overwrite config"); let json = serde_json::to_string_pretty(self).expect("Parse config to JSON failed"); file.write_all(json.as_bytes()) - .expect("Saving config failed"); + .unwrap_or_else(|err| error!("Could not write config: {}", err)); } pub fn set_mode_data(&mut self, mode: AuraModes) { diff --git a/asus-nb-ctrl/src/ctrl_charge.rs b/asus-nb-ctrl/src/ctrl_charge.rs index 6b803eae..94de51a5 100644 --- a/asus-nb-ctrl/src/ctrl_charge.rs +++ b/asus-nb-ctrl/src/ctrl_charge.rs @@ -86,7 +86,7 @@ impl CtrlCharge { file.write_all(limit.to_string().as_bytes()) .unwrap_or_else(|err| error!("Could not write to {}, {:?}", BAT_CHARGE_PATH, err)); info!("Battery charge limit: {}", limit); - + config.read(); config.bat_charge_limit = limit; config.write(); diff --git a/asus-nb-ctrl/src/ctrl_leds.rs b/asus-nb-ctrl/src/ctrl_leds.rs index 6ccd3c0a..8d3bb4c7 100644 --- a/asus-nb-ctrl/src/ctrl_leds.rs +++ b/asus-nb-ctrl/src/ctrl_leds.rs @@ -119,7 +119,7 @@ impl crate::Controller for CtrlKbdBacklight { } // Reload brightness - let bright = config.kbd_boot_brightness; + let bright = config.kbd_led_brightness; let bytes = aura_brightness_bytes(bright); self.write_bytes(&bytes).await?; info!("Reloaded last used brightness"); @@ -165,7 +165,7 @@ impl CtrlKbdBacklight { if let Some(num) = char::from(buf[0]).to_digit(10) { if config.power_profile != num as u8 { config.read(); - config.kbd_boot_brightness = num as u8; + config.kbd_led_brightness = num as u8; config.write(); } return Ok(()); @@ -225,7 +225,7 @@ impl CtrlKbdBacklight { let bytes: [u8; LED_MSG_LEN] = (&mode).into(); self.write_bytes(&bytes).await?; config.read(); - config.kbd_boot_brightness = n; + config.kbd_led_brightness = n; config.write(); info!("LED brightness set to {:#?}", n); }