From 242c98256500f5be059fa20e6596e8dbf607e3f5 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Tue, 21 Jan 2020 17:22:17 +0200 Subject: [PATCH 1/7] Begin working on more graceful error handling NOTE: This does not work currently Signed-off-by: Marko Korhonen --- src/config.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index d32b172..e077d12 100644 --- a/src/config.rs +++ b/src/config.rs @@ -39,15 +39,33 @@ pub fn load_xdg() -> Config { "{} does not exist, writing default configuration", path.display() ); + match fs::write(&path, toml::to_string(&Config::default()).unwrap()) { Ok(()) => println!("Default config saved to {}", path.display()), Err(err) => eprintln!("Failed to write default config: {}", err), }; + Config::default() } else { - let toml = fs::read(&path).unwrap(); - let config: Config = toml::from_str(&toml).unwrap(); - config + let toml: String; + + match fs::read(&path) { + Ok(string) => toml = string, + Err(err) => { + eprintln!("Failed to read config: {}", err); + eprintln!("Using default config"); + return Config::default(); + } + } + + match toml::from_str(&toml) { + Ok(parsed_conf) = parsed_conf, + Err(err) => { + eprintln!("Failed to parse config: {}", err); + eprintln!("Using default config"); + config + } + } } } @@ -56,7 +74,9 @@ pub fn load_user(path: PathBuf) -> Config { panic!("{} does not exist", path.display()); } else { let toml = fs::read(&path).unwrap(); - let config: Config = toml::from_str(&toml).unwrap(); + match toml::from_str(&toml) { + Ok(parsed_conf) -> + } config } } From afe45729881a15aee2d217bfd5280ebf691a4bb3 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Tue, 21 Jan 2020 20:59:06 +0200 Subject: [PATCH 2/7] Added --copy-config and set order within help --- src/cli.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/cli.rs b/src/cli.rs index 0f69a8c..f525130 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -12,6 +12,7 @@ pub fn get_args() -> clap::ArgMatches<'static> { .long("dim") .short("d") .takes_value(false) + .display_order(1) .help("Dims the screen to idle level set in configuration"), ) .arg( @@ -19,13 +20,22 @@ pub fn get_args() -> clap::ArgMatches<'static> { .long("resume") .short("r") .takes_value(false) + .display_order(2) .help("Sets the backlight to the value it was before dimming"), ) + .arg( + Arg::with_name("copy-config") + .long("copy-config") + .takes_value(false) + .display_order(3) + .help("Copies the default config file to $XDG_CONFIG_HOME/lqsd"), + ) .arg( Arg::with_name("config") .long("config") - .short("c") .takes_value(true) + .number_of_values(1) + .value_name("FILE") .help("Sets a custom config file"), ) .get_matches() From c1878b1fab4b07b22a04f372781a272942f94245 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Tue, 21 Jan 2020 20:59:29 +0200 Subject: [PATCH 3/7] Better error handling Now when the configuration file fails to read/parse, we use default config --- src/config.rs | 74 +++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 40 deletions(-) diff --git a/src/config.rs b/src/config.rs index e077d12..34bc986 100644 --- a/src/config.rs +++ b/src/config.rs @@ -24,59 +24,53 @@ impl Default for Config { } } -fn xdg_config() -> PathBuf { +fn read_parse(path: PathBuf) -> Config { + let mut toml = String::from(""); + + match fs::read(&path) { + Ok(result) => toml = result, + Err(err) => { + eprintln!("Failed to read config: {}", err); + println!("Using default config"); + return Config::default(); + } + } + + match toml::from_str(&toml) { + Ok(result) => return result, + Err(err) => { + eprintln!("Failed to read config: {}", err); + println!("Using default config"); + return Config::default(); + } + } +} + +fn xdg_path() -> PathBuf { BaseDirectories::with_prefix("lqsd") .expect("cannot create configuration directory") .place_config_file("config.toml") .unwrap() } +pub fn copy_config() { + let path = xdg_path(); + match fs::write(&path, toml::to_string(&Config::default()).unwrap()) { + Ok(()) => println!("Default config saved to {}", path.display()), + Err(err) => eprintln!("Failed to write default config: {}", err), + }; +} + pub fn load_xdg() -> Config { - let path = xdg_config(); + let path = xdg_path(); if !path.exists() { - println!( - "{} does not exist, writing default configuration", - path.display() - ); - - match fs::write(&path, toml::to_string(&Config::default()).unwrap()) { - Ok(()) => println!("Default config saved to {}", path.display()), - Err(err) => eprintln!("Failed to write default config: {}", err), - }; - Config::default() } else { - let toml: String; - - match fs::read(&path) { - Ok(string) => toml = string, - Err(err) => { - eprintln!("Failed to read config: {}", err); - eprintln!("Using default config"); - return Config::default(); - } - } - - match toml::from_str(&toml) { - Ok(parsed_conf) = parsed_conf, - Err(err) => { - eprintln!("Failed to parse config: {}", err); - eprintln!("Using default config"); - config - } - } + read_parse(xdg_path()) } } pub fn load_user(path: PathBuf) -> Config { - if !path.exists() { - panic!("{} does not exist", path.display()); - } else { - let toml = fs::read(&path).unwrap(); - match toml::from_str(&toml) { - Ok(parsed_conf) -> - } - config - } + read_parse(path) } From 1a016546d817e0bf2d4f62777621ba047c1b9d05 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Tue, 21 Jan 2020 21:00:09 +0200 Subject: [PATCH 4/7] Improve error handling --- src/main.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index a51992c..132ef72 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,23 +58,42 @@ fn main() { } else { conf = config::load_xdg(); } + let dim_speed = time::Duration::from_millis(conf.dim_speed); let resume_speed = time::Duration::from_millis(conf.resume_speed); if args.is_present("dim") { let current_brightness = get_brightness().to_string(); match fs::write(&conf.resume_file, current_brightness) { - Ok(()) => println!("Current brightness written to resume file"), + Ok(()) => println!( + "Current brightness written to resume file at {}", + conf.resume_file.display() + ), Err(err) => eprintln!("Error writing brightness to resume file: {}", err), } transition(conf.idle_level, dim_speed); } + if args.is_present("resume") { - let old_brightness: i32 = fs::read(&conf.resume_file).unwrap().parse().unwrap(); + let old_brightness: i32; + + match fs::read(&conf.resume_file) { + Ok(result) => old_brightness = result.parse().unwrap(), + Err(err) => { + eprintln!("Failed to read resume_file: {}", err); + old_brightness = 100; + } + } + transition(old_brightness, resume_speed); + match fs::remove(&conf.resume_file) { Ok(()) => println!("Resume file removed"), Err(err) => eprintln!("Failed to remove resume file: {}", err), } } + + if args.is_present("copy-config") { + config::copy_config(); + } } From 46092dfe3af207425a376acdc9927820534c5325 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Tue, 21 Jan 2020 21:43:18 +0200 Subject: [PATCH 5/7] Use default value if key is missing --- src/config.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/config.rs b/src/config.rs index 34bc986..5ed933c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -6,6 +6,7 @@ use std::path::PathBuf; use xdg::BaseDirectories; #[derive(Deserialize, Serialize)] +#[serde(default)] pub struct Config { pub resume_file: PathBuf, pub idle_level: i32, From 28f1bc021acda7a9f99d0e112c21b992880c001d Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Tue, 21 Jan 2020 21:50:03 +0200 Subject: [PATCH 6/7] Ignore unused variable --- src/config.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/config.rs b/src/config.rs index 5ed933c..6be1d43 100644 --- a/src/config.rs +++ b/src/config.rs @@ -26,10 +26,10 @@ impl Default for Config { } fn read_parse(path: PathBuf) -> Config { - let mut toml = String::from(""); + let mut _toml = String::from(""); match fs::read(&path) { - Ok(result) => toml = result, + Ok(result) => _toml = result, Err(err) => { eprintln!("Failed to read config: {}", err); println!("Using default config"); @@ -37,7 +37,7 @@ fn read_parse(path: PathBuf) -> Config { } } - match toml::from_str(&toml) { + match toml::from_str(&_toml) { Ok(result) => return result, Err(err) => { eprintln!("Failed to read config: {}", err); From 9740b62d5d51712d135d5c30c031bf885f5a0a2c Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Tue, 21 Jan 2020 21:50:19 +0200 Subject: [PATCH 7/7] Update usage --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 928f346..e6e3801 100644 --- a/README.md +++ b/README.md @@ -9,13 +9,14 @@ USAGE: lqsd [FLAGS] [OPTIONS] FLAGS: - -d, --dim Dims the screen to idle level set in configuration - -h, --help Prints help information - -r, --resume Sets the backlight to the value it was before dimming - -V, --version Prints version information + -d, --dim Dims the screen to idle level set in configuration + -r, --resume Sets the backlight to the value it was before dimming + --copy-config Copies the default config file to $XDG_CONFIG_HOME/lqsd + -h, --help Prints help information + -V, --version Prints version information OPTIONS: - -c, --config Sets a custom config file + --config Sets a custom config file ``` The configuration file resides at `~/.config/lqsd/config.toml`. There you can set these values: