mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-01-19 00:57:53 +00:00
removed unnecessary unwraps, closes #4
This commit is contained in:
parent
4624a98039
commit
150b6ff30b
5 changed files with 29 additions and 14 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -91,7 +91,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "lamp"
|
||||
version = "0.3.0"
|
||||
version = "0.3.1"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"clap_complete",
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
[package]
|
||||
name = "lamp"
|
||||
version = "0.3.0"
|
||||
version = "0.3.1"
|
||||
authors = ["crapStone <crapstone01@gmail.com>"]
|
||||
license = "GPL-3.0-or-later"
|
||||
description = "A Linux backlight utility inspired by acpibacklight"
|
||||
|
|
|
@ -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),
|
||||
)
|
||||
|
|
|
@ -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<String, PathBuf>) {
|
||||
pub fn get_controllers() -> Result<(String, HashMap<String, PathBuf>), io::Error> {
|
||||
let mut controllers: HashMap<String, PathBuf> = 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<String, PathBuf>) {
|
|||
}
|
||||
}
|
||||
|
||||
(default.unwrap(), controllers)
|
||||
Ok((
|
||||
default.unwrap_or_else(|| {
|
||||
eprintln!("no devices found");
|
||||
exit(exitcode::OSFILE)
|
||||
}),
|
||||
controllers,
|
||||
))
|
||||
}
|
||||
|
|
21
src/main.rs
21
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::<u32>().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::<u32>().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::<u32>().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/
|
||||
|
|
Loading…
Reference in a new issue