From 150b6ff30bcf47fb61fdde515b309002fcd303cf Mon Sep 17 00:00:00 2001 From: crapStone Date: Sat, 16 Jul 2022 16:40:53 +0200 Subject: [PATCH] removed unnecessary unwraps, closes #4 --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cli.rs | 2 +- src/controllers.rs | 16 +++++++++++----- src/main.rs | 21 +++++++++++++++------ 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 792b304..96541aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -91,7 +91,7 @@ dependencies = [ [[package]] name = "lamp" -version = "0.3.0" +version = "0.3.1" dependencies = [ "clap", "clap_complete", diff --git a/Cargo.toml b/Cargo.toml index 10719ff..396c25a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ [package] name = "lamp" -version = "0.3.0" +version = "0.3.1" authors = ["crapStone "] license = "GPL-3.0-or-later" description = "A Linux backlight utility inspired by acpibacklight" diff --git a/src/cli.rs b/src/cli.rs index 1d250b8..039bbbf 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -64,7 +64,7 @@ pub fn build_cli() -> App<'static> { Arg::with_name("controller") .short('c') .long("controller") - .help("Select device to control") + .help("Select device to control, defaults to the first device found") .value_name("DEVICE") .takes_value(true), ) diff --git a/src/controllers.rs b/src/controllers.rs index cc49b85..d4c447d 100644 --- a/src/controllers.rs +++ b/src/controllers.rs @@ -5,7 +5,7 @@ use std::collections::HashMap; use std::fs::{File, OpenOptions}; -use std::io::prelude::*; +use std::io::{self, prelude::*}; use std::path::{Path, PathBuf}; use std::process::exit; @@ -174,15 +174,15 @@ fn read_file_to_int(path: PathBuf) -> u32 { /// Searches through all paths in `SYS_PATHS` and creates a `HashMap` with the name and absolute path. /// /// It returns a `Tuple` of the default backlight name and the `HashMap`. -pub fn get_controllers() -> (String, HashMap) { +pub fn get_controllers() -> Result<(String, HashMap), io::Error> { let mut controllers: HashMap = HashMap::new(); let mut default = None; for path in SYS_PATHS { if Path::new(path).exists() { - for name in Path::new(path).read_dir().unwrap() { - let name = name.unwrap().path(); + for name in Path::new(path).read_dir()? { + let name = name?.path(); let key = String::from(name.file_name().unwrap().to_str().unwrap()); if default.is_none() { @@ -194,5 +194,11 @@ pub fn get_controllers() -> (String, HashMap) { } } - (default.unwrap(), controllers) + Ok(( + default.unwrap_or_else(|| { + eprintln!("no devices found"); + exit(exitcode::OSFILE) + }), + controllers, + )) } diff --git a/src/main.rs b/src/main.rs index 45a2d0a..52e3180 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,10 @@ fn main() { let app = build_cli(); let matches = app.get_matches(); - let (default_ctrl, ctrls) = controllers::get_controllers(); + let (default_ctrl, ctrls) = controllers::get_controllers().unwrap_or_else(|why| { + eprintln!("an error occured when reading devices: '{why}'"); + exit(exitcode::IOERR) + }); let p = match matches.value_of("controller") { Some(ctrl) => { @@ -48,14 +51,14 @@ fn main() { } exit(exitcode::OK); } else if let Some(value) = matches.value_of("set") { - let new_value = value.parse::().unwrap(); + let new_value = str_to_int(value); controller.set_brightness(new_value); } else if let Some(value) = matches.value_of("inc") { - let new_value = controller.get_brightness() + value.parse::().unwrap(); + let new_value = controller.get_brightness() + str_to_int(value); controller.set_brightness(new_value.min(controller.get_max_brightness())); } else if let Some(value) = matches.value_of("dec") { - let new_value = controller.get_brightness() - value.parse::().unwrap(); - controller.set_brightness(new_value.max(0)); + let new_value = controller.get_brightness() - str_to_int(value); + controller.set_brightness(new_value); } else if matches.is_present("get") { println!("{}", controller.get_brightness()); } else if matches.is_present("zero") { @@ -65,8 +68,14 @@ fn main() { } else { build_cli().print_long_help().unwrap(); } +} - exit(exitcode::OK); +#[inline(always)] +fn str_to_int(value: &str) -> u32 { + value.parse().unwrap_or_else(|_| { + eprintln!("cannot parse '{value}' as positive integer"); + exit(exitcode::DATAERR); + }) } // https://xkcd.com/2200/