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]]
|
[[package]]
|
||||||
name = "lamp"
|
name = "lamp"
|
||||||
version = "0.3.0"
|
version = "0.3.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"clap_complete",
|
"clap_complete",
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "lamp"
|
name = "lamp"
|
||||||
version = "0.3.0"
|
version = "0.3.1"
|
||||||
authors = ["crapStone <crapstone01@gmail.com>"]
|
authors = ["crapStone <crapstone01@gmail.com>"]
|
||||||
license = "GPL-3.0-or-later"
|
license = "GPL-3.0-or-later"
|
||||||
description = "A Linux backlight utility inspired by acpibacklight"
|
description = "A Linux backlight utility inspired by acpibacklight"
|
||||||
|
|
|
@ -64,7 +64,7 @@ pub fn build_cli() -> App<'static> {
|
||||||
Arg::with_name("controller")
|
Arg::with_name("controller")
|
||||||
.short('c')
|
.short('c')
|
||||||
.long("controller")
|
.long("controller")
|
||||||
.help("Select device to control")
|
.help("Select device to control, defaults to the first device found")
|
||||||
.value_name("DEVICE")
|
.value_name("DEVICE")
|
||||||
.takes_value(true),
|
.takes_value(true),
|
||||||
)
|
)
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::{File, OpenOptions};
|
use std::fs::{File, OpenOptions};
|
||||||
use std::io::prelude::*;
|
use std::io::{self, prelude::*};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::exit;
|
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.
|
/// 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`.
|
/// 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 controllers: HashMap<String, PathBuf> = HashMap::new();
|
||||||
|
|
||||||
let mut default = None;
|
let mut default = None;
|
||||||
|
|
||||||
for path in SYS_PATHS {
|
for path in SYS_PATHS {
|
||||||
if Path::new(path).exists() {
|
if Path::new(path).exists() {
|
||||||
for name in Path::new(path).read_dir().unwrap() {
|
for name in Path::new(path).read_dir()? {
|
||||||
let name = name.unwrap().path();
|
let name = name?.path();
|
||||||
let key = String::from(name.file_name().unwrap().to_str().unwrap());
|
let key = String::from(name.file_name().unwrap().to_str().unwrap());
|
||||||
|
|
||||||
if default.is_none() {
|
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 app = build_cli();
|
||||||
let matches = app.get_matches();
|
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") {
|
let p = match matches.value_of("controller") {
|
||||||
Some(ctrl) => {
|
Some(ctrl) => {
|
||||||
|
@ -48,14 +51,14 @@ fn main() {
|
||||||
}
|
}
|
||||||
exit(exitcode::OK);
|
exit(exitcode::OK);
|
||||||
} else if let Some(value) = matches.value_of("set") {
|
} 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);
|
controller.set_brightness(new_value);
|
||||||
} else if let Some(value) = matches.value_of("inc") {
|
} 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()));
|
controller.set_brightness(new_value.min(controller.get_max_brightness()));
|
||||||
} else if let Some(value) = matches.value_of("dec") {
|
} else if let Some(value) = matches.value_of("dec") {
|
||||||
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.max(0));
|
controller.set_brightness(new_value);
|
||||||
} else if matches.is_present("get") {
|
} else if matches.is_present("get") {
|
||||||
println!("{}", controller.get_brightness());
|
println!("{}", controller.get_brightness());
|
||||||
} else if matches.is_present("zero") {
|
} else if matches.is_present("zero") {
|
||||||
|
@ -65,8 +68,14 @@ fn main() {
|
||||||
} else {
|
} else {
|
||||||
build_cli().print_long_help().unwrap();
|
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/
|
// https://xkcd.com/2200/
|
||||||
|
|
Loading…
Reference in a new issue