diff --git a/.cargo-husky/hooks/pre-commit b/.cargo-husky/hooks/pre-commit new file mode 100755 index 00000000..76bf12b1 --- /dev/null +++ b/.cargo-husky/hooks/pre-commit @@ -0,0 +1,12 @@ +#!/bin/sh + +set -e + +echo '+cargo +nightly fmt --all -- --check' +cargo +nightly fmt --all -- --check +echo '+cargo clippy --all -- -D warnings' +cargo clippy --all -- -D warnings +echo '+cargo test --all' +cargo test --all +echo '+cargo cranky' +cargo cranky \ No newline at end of file diff --git a/.cargo-husky/hooks/pre-push b/.cargo-husky/hooks/pre-push new file mode 100755 index 00000000..b47ed1ad --- /dev/null +++ b/.cargo-husky/hooks/pre-push @@ -0,0 +1,10 @@ +#!/bin/sh + +set -e + +echo '+cargo +nightly fmt --all -- --check' +cargo +nightly fmt --all -- --check +echo '+cargo clippy --all -- -D warnings' +cargo clippy --all -- -D warnings +echo '+cargo cranky' +cargo cranky \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 1d8e7733..84c805bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -151,6 +151,7 @@ checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" name = "asusctl" version = "4.6.0-rc1" dependencies = [ + "cargo-husky", "daemon", "gif", "glam", @@ -504,6 +505,12 @@ dependencies = [ "vec_map", ] +[[package]] +name = "cargo-husky" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b02b629252fe8ef6460461409564e2c21d0c8e77e0944f3d189ff06c4e932ad" + [[package]] name = "cc" version = "1.0.78" @@ -659,8 +666,9 @@ dependencies = [ [[package]] name = "config-traits" -version = "0.1.0" +version = "4.6.0-rc1" dependencies = [ + "cargo-husky", "log", "ron", "serde", @@ -793,6 +801,7 @@ name = "daemon" version = "4.6.0-rc1" dependencies = [ "async-trait", + "cargo-husky", "concat-idents", "config-traits", "env_logger", @@ -815,6 +824,7 @@ dependencies = [ name = "daemon-user" version = "4.6.0-rc1" dependencies = [ + "cargo-husky", "config-traits", "dirs", "env_logger", @@ -2709,6 +2719,7 @@ dependencies = [ name = "rog-control-center" version = "4.6.0-rc1" dependencies = [ + "cargo-husky", "daemon", "dirs", "eframe", @@ -2740,6 +2751,7 @@ dependencies = [ name = "rog_anime" version = "4.6.0-rc1" dependencies = [ + "cargo-husky", "gif", "glam", "log", @@ -2756,6 +2768,7 @@ dependencies = [ name = "rog_aura" version = "4.6.0-rc1" dependencies = [ + "cargo-husky", "log", "ron", "serde", @@ -2768,6 +2781,7 @@ dependencies = [ name = "rog_dbus" version = "4.6.0-rc1" dependencies = [ + "cargo-husky", "rog_anime", "rog_aura", "rog_platform", @@ -2779,6 +2793,7 @@ dependencies = [ name = "rog_platform" version = "4.6.0-rc1" dependencies = [ + "cargo-husky", "concat-idents", "inotify", "log", @@ -2795,6 +2810,7 @@ dependencies = [ name = "rog_profiles" version = "4.6.0-rc1" dependencies = [ + "cargo-husky", "serde", "serde_derive", "udev 0.7.0", diff --git a/Cargo.toml b/Cargo.toml index 82156752..5c77e0ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,3 +52,8 @@ opt-level = 1 [profile.bench] debug = false opt-level = 3 + +[workspace.dependencies.cargo-husky] +version = "1" +default-features = false +features = ["user-hooks"] \ No newline at end of file diff --git a/Cranky.toml b/Cranky.toml index 34342341..49cdddf3 100644 --- a/Cranky.toml +++ b/Cranky.toml @@ -1,7 +1,7 @@ # https://github.com/ericseppanen/cargo-cranky # cargo install cargo-cranky && cargo cranky -warn = [ +error = [ "clippy::all", "clippy::await_holding_lock", "clippy::bool_to_int_with_if", diff --git a/README.md b/README.md index c6e03a99..1362be64 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,10 @@ You may also need to activate the service for debian install. If running Pop!_OS Run `sudo make uninstall` in the source repo, and remove `/etc/asusd/`. +# Contributing + +See `CONTRIBUTING.md`. Additionally, also do `cargo clean` and `cargo test` on first checkout to ensure the commit hooks are used (via `cargo-husky`). + # OTHER ## Supporting more laptops diff --git a/asusctl/Cargo.toml b/asusctl/Cargo.toml index f3c5a8bc..1f554ed7 100644 --- a/asusctl/Cargo.toml +++ b/asusctl/Cargo.toml @@ -22,3 +22,5 @@ gif.workspace = true tinybmp.workspace = true glam.workspace = true rog_dbus = { path = "../rog-dbus" } + +cargo-husky.workspace = true \ No newline at end of file diff --git a/asusctl/src/main.rs b/asusctl/src/main.rs index 71249256..ea20c754 100644 --- a/asusctl/src/main.rs +++ b/asusctl/src/main.rs @@ -36,15 +36,14 @@ fn main() { ..Default::default() }, Err(err) => { - eprintln!("source {}", err); - std::process::exit(2); + panic!("source {}", err); } }; let (dbus, _) = RogDbusClientBlocking::new() .map_err(|e| { print_error_help(&e, None); - std::process::exit(3); + panic!("Could not start dbus client"); }) .unwrap(); @@ -54,7 +53,7 @@ fn main() { .supported_functions() .map_err(|e| { print_error_help(&e, None); - std::process::exit(4); + panic!("Could not start dbus proxy"); }) .unwrap(); @@ -260,7 +259,7 @@ fn handle_anime( if let Some(lst) = image.self_command_list() { println!("\n{}", lst); } - std::process::exit(1); + return Ok(()); } verify_brightness(image.bright); @@ -283,7 +282,7 @@ fn handle_anime( if let Some(lst) = image.self_command_list() { println!("\n{}", lst); } - std::process::exit(1); + return Ok(()); } verify_brightness(image.bright); @@ -304,7 +303,7 @@ fn handle_anime( if let Some(lst) = gif.self_command_list() { println!("\n{}", lst); } - std::process::exit(1); + return Ok(()); } verify_brightness(gif.bright); @@ -338,7 +337,7 @@ fn handle_anime( if let Some(lst) = gif.self_command_list() { println!("\n{}", lst); } - std::process::exit(1); + return Ok(()); } verify_brightness(gif.bright); @@ -374,7 +373,6 @@ fn verify_brightness(brightness: f32) { "Image and global brightness must be between 0.0 and 1.0 (inclusive), was {}", brightness ); - std::process::exit(1); } } @@ -391,7 +389,7 @@ fn handle_led_mode( println!("Commands available"); if let Some(cmdlist) = LedModeCommand::command_list() { - let commands: Vec = cmdlist.lines().map(|s| s.to_string()).collect(); + let commands: Vec = cmdlist.lines().map(|s| s.to_owned()).collect(); for command in commands.iter().filter(|command| { for mode in &supported.basic_modes { if command @@ -662,7 +660,7 @@ fn handle_profile( if let Some(lst) = cmd.self_command_list() { println!("\n{}", lst); } - std::process::exit(1); + return Ok(()); } if cmd.next { @@ -706,14 +704,14 @@ fn handle_fan_curve( if let Some(lst) = cmd.self_command_list() { println!("\n{}", lst); } - std::process::exit(1); + return Ok(()); } if (cmd.enabled.is_some() || cmd.fan.is_some() || cmd.data.is_some()) && cmd.mod_profile.is_none() { println!("--enabled, --fan, and --data options require --mod-profile"); - std::process::exit(666); + return Ok(()); } if cmd.get_enabled { diff --git a/config-traits/Cargo.toml b/config-traits/Cargo.toml index 75d7ec65..74a504d2 100644 --- a/config-traits/Cargo.toml +++ b/config-traits/Cargo.toml @@ -1,9 +1,9 @@ [package] name = "config-traits" -version = "0.1.0" +license = "MPL-2.0" +authors = ["Luke D Jones "] edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +version.workspace = true [dependencies] serde.workspace = true @@ -12,4 +12,7 @@ serde_json.workspace = true toml.workspace = true ron.workspace = true -log.workspace = true \ No newline at end of file +log.workspace = true + +[dev-dependencies] +cargo-husky.workspace = true \ No newline at end of file diff --git a/config-traits/README.md b/config-traits/README.md new file mode 100644 index 00000000..781f41ff --- /dev/null +++ b/config-traits/README.md @@ -0,0 +1,10 @@ +# config-traits + +`config_traits` is a crate that broke out from the requirement to manage various +different config files, including parsing from different formats and updating +them from previous versions where fields or names are changed in some way. + +The end canonical file format is `.ron` as this supports rust types well, and includes +the ability to add commenting, and is less verbose than `json`. Currently the crate will +also try to parse from `json` and `toml` if the `ron` parsing fails, then update to `ron` +format. \ No newline at end of file diff --git a/config-traits/src/lib.rs b/config-traits/src/lib.rs index 63e9ba00..b70f4c6f 100644 --- a/config-traits/src/lib.rs +++ b/config-traits/src/lib.rs @@ -1,4 +1,14 @@ -use std::fs::{create_dir, File, OpenOptions}; +//! `config_traits` is a crate that broke out from the requirement to manage +//! various different config files, including parsing from different formats and +//! updating them from previous versions where fields or names are changed in +//! some way. +//! +//! The end canonical file format is `.ron` as this supports rust types well, +//! and includes the ability to add commenting, and is less verbose than `json`. +//! Currently the crate will also try to parse from `json` and `toml` if the +//! `ron` parsing fails, then update to `ron` format. + +use std::fs::{self, create_dir, File, OpenOptions}; use std::io::{Read, Write}; use std::path::PathBuf; @@ -49,7 +59,7 @@ where "Could not rename. Please remove {} then restart service: Error {}", self.file_name(), err - ) + ); }); do_rename = false; } @@ -68,7 +78,7 @@ where "Could not rename. Please remove {} then restart service: Error {}", self.file_name(), err - ) + ); }); } config @@ -88,18 +98,10 @@ where /// Open and parse the config file to self from ron format fn read(&mut self) { - let mut file = match OpenOptions::new().read(true).open(self.file_path()) { - Ok(data) => data, - Err(err) => { - error!("Error reading {:?}: {}", self.file_path(), err); - return; - } - }; - let mut buf = String::new(); - if let Ok(l) = file.read_to_string(&mut buf) { - if l == 0 { + if let Ok(data) = fs::read_to_string(self.file_path()) { + if data.is_empty() { warn!("File is empty {:?}", self.file_path()); - } else if let Ok(data) = ron::from_str(&buf) { + } else if let Ok(data) = ron::from_str(&data) { *self = data; } else { warn!("Could not deserialise {:?}", self.file_path()); @@ -137,13 +139,14 @@ where self.file_name(), self.file_name() ); - let cfg_old = self.file_path().to_string_lossy().to_string() + "-old"; + let mut cfg_old = self.file_path().to_string_lossy().to_string(); + cfg_old.push_str("-old"); std::fs::rename(self.file_path(), cfg_old).unwrap_or_else(|err| { error!( "Could not rename. Please remove {} then restart service: Error {}", self.file_name(), err - ) + ); }); } } @@ -206,7 +209,9 @@ macro_rules! std_config_load { self = data; } else if let Ok(data) = toml::from_str(&buf) { self = data; - } $(else if let Ok(data) = serde_json::from_str::<$generic>(&buf) { + } $(else if let Ok(data) = ron::from_str::<$generic>(&buf) { + self = data.into(); + } else if let Ok(data) = serde_json::from_str::<$generic>(&buf) { self = data.into(); } else if let Ok(data) = toml::from_str::<$generic>(&buf) { self = data.into(); diff --git a/daemon-user/Cargo.toml b/daemon-user/Cargo.toml index 1d00f785..0a51f551 100644 --- a/daemon-user/Cargo.toml +++ b/daemon-user/Cargo.toml @@ -33,4 +33,7 @@ zbus.workspace = true # cli and logging log.workspace = true -env_logger.workspace = true \ No newline at end of file +env_logger.workspace = true + +[dev-dependencies] +cargo-husky.workspace = true \ No newline at end of file diff --git a/daemon-user/src/daemon.rs b/daemon-user/src/daemon.rs index c2d7dc46..ca7fed28 100644 --- a/daemon-user/src/daemon.rs +++ b/daemon-user/src/daemon.rs @@ -1,5 +1,4 @@ -use std::fs::OpenOptions; -use std::io::{Read, Write}; +use std::io::Write; use std::path::PathBuf; use std::sync::atomic::AtomicBool; use std::sync::{Arc, Mutex}; @@ -78,17 +77,7 @@ fn main() -> Result<(), Box> { // if supported.keyboard_led.per_key_led_mode { if let Some(cfg) = config.active_aura { let mut aura_config = ConfigAura::new().set_name(cfg).load(); - - // Find and load a matching layout for laptop - let mut file = OpenOptions::new() - .read(true) - .open(PathBuf::from(BOARD_NAME)) - .map_err(|e| { - println!("{BOARD_NAME}, {e}"); - e - })?; - let mut board_name = String::new(); - file.read_to_string(&mut board_name)?; + // let baord_name = std::fs::read_to_string(BOARD_NAME)?; let led_support = LaptopLedData::get_data(); diff --git a/daemon-user/src/zbus_anime.rs b/daemon-user/src/zbus_anime.rs index d08c2185..793f373b 100644 --- a/daemon-user/src/zbus_anime.rs +++ b/daemon-user/src/zbus_anime.rs @@ -1,6 +1,6 @@ -//! # DBus interface proxy for: `org.asuslinux.Daemon` +//! # `DBus` interface proxy for: `org.asuslinux.Daemon` //! -//! This code was generated by `zbus-xmlgen` `1.0.0` from DBus introspection +//! This code was generated by `zbus-xmlgen` `1.0.0` from `DBus` introspection //! data. Source: `Interface '/org/asuslinux/Anime' from service //! 'org.asuslinux.Daemon' on session bus`. //! @@ -10,8 +10,8 @@ //! [Writing a client proxy](https://dbus.pages.freedesktop.org/zbus/client.html) //! section of the zbus documentation. //! -//! This DBus object implements -//! [standard DBus interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html), +//! This `DBus` object implements +//! [standard `DBus` interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html), //! (`org.freedesktop.DBus.*`) for which the following zbus proxies can be used: //! //! * [`zbus::fdo::PeerProxy`] diff --git a/daemon/Cargo.toml b/daemon/Cargo.toml index 4bad7dd7..1f64c790 100644 --- a/daemon/Cargo.toml +++ b/daemon/Cargo.toml @@ -44,4 +44,7 @@ sysfs-class.workspace = true # used for backlight control and baord ID concat-idents.workspace = true -systemd-zbus = "*" \ No newline at end of file +systemd-zbus = "*" + +[dev-dependencies] +cargo-husky.workspace = true \ No newline at end of file diff --git a/daemon/src/config.rs b/daemon/src/config.rs index f3e34551..f156cd4b 100644 --- a/daemon/src/config.rs +++ b/daemon/src/config.rs @@ -29,7 +29,7 @@ impl StdConfig for Config { } fn file_name(&self) -> String { - CONFIG_FILE.to_string() + CONFIG_FILE.to_owned() } } diff --git a/daemon/src/ctrl_anime/config.rs b/daemon/src/ctrl_anime/config.rs index 03db8567..e0fb9e6a 100644 --- a/daemon/src/ctrl_anime/config.rs +++ b/daemon/src/ctrl_anime/config.rs @@ -145,7 +145,7 @@ impl StdConfig for AnimeConfig { } fn file_name(&self) -> String { - CONFIG_FILE.to_string() + CONFIG_FILE.to_owned() } } diff --git a/daemon/src/ctrl_aura/config.rs b/daemon/src/ctrl_aura/config.rs index 8082c2b8..e641dd00 100644 --- a/daemon/src/ctrl_aura/config.rs +++ b/daemon/src/ctrl_aura/config.rs @@ -195,7 +195,7 @@ impl StdConfig for AuraConfig { } fn file_name(&self) -> String { - CONFIG_FILE.to_string() + CONFIG_FILE.to_owned() } } diff --git a/daemon/src/ctrl_aura/mod.rs b/daemon/src/ctrl_aura/mod.rs index 9c365c20..2da39c6e 100644 --- a/daemon/src/ctrl_aura/mod.rs +++ b/daemon/src/ctrl_aura/mod.rs @@ -1,4 +1,4 @@ pub mod config; pub mod controller; -/// Implements CtrlTask, Reloadable, ZbusRun +/// Implements `CtrlTask`, `Reloadable`, `ZbusRun` pub mod trait_impls; diff --git a/daemon/src/ctrl_platform.rs b/daemon/src/ctrl_platform.rs index e9c81e36..29b342cc 100644 --- a/daemon/src/ctrl_platform.rs +++ b/daemon/src/ctrl_platform.rs @@ -92,15 +92,8 @@ impl CtrlPlatform { } pub fn get_boot_sound() -> Result { - let path = ASUS_POST_LOGO_SOUND; - let mut file = OpenOptions::new() - .read(true) - .open(path) - .map_err(|err| RogError::Path(path.into(), err))?; - - let mut data = Vec::new(); - file.read_to_end(&mut data) - .map_err(|err| RogError::Read(path.into(), err))?; + let data = std::fs::read(ASUS_POST_LOGO_SOUND) + .map_err(|err| RogError::Read(ASUS_POST_LOGO_SOUND.into(), err))?; let idx = data.len() - 1; Ok(data[idx] as i8) @@ -115,6 +108,7 @@ impl CtrlPlatform { .map_err(|err| RogError::Path(path.into(), err))?; let mut data = Vec::new(); + #[allow(clippy::verbose_file_reads)] file.read_to_end(&mut data) .map_err(|err| RogError::Read(path.into(), err))?; diff --git a/daemon/src/ctrl_profiles/config.rs b/daemon/src/ctrl_profiles/config.rs index 4471bd92..19996282 100644 --- a/daemon/src/ctrl_profiles/config.rs +++ b/daemon/src/ctrl_profiles/config.rs @@ -28,7 +28,7 @@ impl StdConfig for ProfileConfig { } fn file_name(&self) -> String { - CONFIG_FILE.to_string() + CONFIG_FILE.to_owned() } } @@ -77,7 +77,7 @@ impl StdConfig for FanCurveConfig { } fn file_name(&self) -> String { - CONFIG_FAN_FILE.to_string() + CONFIG_FAN_FILE.to_owned() } } diff --git a/rog-anime/Cargo.toml b/rog-anime/Cargo.toml index f88e8251..35315d19 100644 --- a/rog-anime/Cargo.toml +++ b/rog-anime/Cargo.toml @@ -32,4 +32,7 @@ zbus = { workspace = true, optional = true } sysfs-class = { workspace = true, optional = true } -uhid-virt = "^0.0.5" \ No newline at end of file +uhid-virt = "^0.0.5" + +[dev-dependencies] +cargo-husky.workspace = true \ No newline at end of file diff --git a/rog-anime/src/image.rs b/rog-anime/src/image.rs index 6ee6fa5f..c6f4d186 100644 --- a/rog-anime/src/image.rs +++ b/rog-anime/src/image.rs @@ -141,7 +141,9 @@ impl AnimeImage { /// /// In relation to the display itself you should think of it as a full /// square grid, so `first_x` is the x position on that grid where the - /// LED is actually positioned in relation to the Y. ```text + /// LED is actually positioned in relation to the Y. + /// + /// ```text /// +------------+ /// | | /// | | diff --git a/rog-aura/Cargo.toml b/rog-aura/Cargo.toml index 5de63932..e495c9a3 100644 --- a/rog-aura/Cargo.toml +++ b/rog-aura/Cargo.toml @@ -27,4 +27,7 @@ log.workspace = true # Device control sysfs-class.workspace = true # used for backlight control and baord ID -ron = { version = "*", optional = true } \ No newline at end of file +ron = { version = "*", optional = true } + +[dev-dependencies] +cargo-husky.workspace = true \ No newline at end of file diff --git a/rog-aura/src/advanced.rs b/rog-aura/src/advanced.rs index fbaf669e..95be63fe 100644 --- a/rog-aura/src/advanced.rs +++ b/rog-aura/src/advanced.rs @@ -303,6 +303,7 @@ impl LedUsbPackets { fn rgb_for_led_code(&mut self, led_code: LedCode) -> Option<&mut [u8]> { let zoned = self.zoned; // Tuples are indexes in to array + #[allow(clippy::match_same_arms)] let (row, col) = match led_code { LedCode::VolDown => (0, 15), LedCode::VolUp => (0, 18), diff --git a/rog-aura/src/advanced_to_str.rs b/rog-aura/src/advanced_to_str.rs index 95fb1ac2..3555eed8 100644 --- a/rog-aura/src/advanced_to_str.rs +++ b/rog-aura/src/advanced_to_str.rs @@ -8,6 +8,7 @@ impl From for &str { impl From<&LedCode> for &str { fn from(k: &LedCode) -> Self { + #[allow(clippy::match_same_arms)] match k { LedCode::VolUp => "Volume Up", LedCode::VolDown => "Volume Down", diff --git a/rog-aura/src/aura_detection.rs b/rog-aura/src/aura_detection.rs index ae2451de..acdde152 100644 --- a/rog-aura/src/aura_detection.rs +++ b/rog-aura/src/aura_detection.rs @@ -1,6 +1,3 @@ -use std::fs::OpenOptions; -use std::io::Read; - use log::{error, info, warn}; use serde_derive::{Deserialize, Serialize}; @@ -76,39 +73,33 @@ impl LedSupportFile { let mut loaded = false; let mut data = LedSupportFile::default(); // Load user configs first so they are first to be checked - if let Ok(mut file) = OpenOptions::new().read(true).open(ASUS_LED_MODE_USER_CONF) { - let mut buf = String::new(); - if let Ok(l) = file.read_to_string(&mut buf) { - if l == 0 { - warn!("{} is empty", ASUS_LED_MODE_USER_CONF); - } else { - if let Ok(mut tmp) = ron::from_str::(&buf) { - data.0.append(&mut tmp.0); - } - info!( - "Loaded user-defined LED support data from {}", - ASUS_LED_MODE_USER_CONF - ); + if let Ok(file) = std::fs::read_to_string(ASUS_LED_MODE_USER_CONF) { + if file.is_empty() { + warn!("{} is empty", ASUS_LED_MODE_USER_CONF); + } else { + if let Ok(mut tmp) = ron::from_str::(&file) { + data.0.append(&mut tmp.0); } + info!( + "Loaded user-defined LED support data from {}", + ASUS_LED_MODE_USER_CONF + ); } } // Load and append the default LED support data - if let Ok(mut file) = OpenOptions::new().read(true).open(ASUS_LED_MODE_CONF) { - let mut buf = String::new(); - if let Ok(l) = file.read_to_string(&mut buf) { - if l == 0 { - warn!("{} is empty", ASUS_LED_MODE_CONF); - } else { - let mut tmp: LedSupportFile = ron::from_str(&buf) - .map_err(|e| error!("{e}")) - .unwrap_or_else(|_| panic!("Could not deserialise {}", ASUS_LED_MODE_CONF)); - data.0.append(&mut tmp.0); - loaded = true; - info!( - "Loaded default LED support data from {}", - ASUS_LED_MODE_CONF - ); - } + if let Ok(file) = std::fs::read_to_string(ASUS_LED_MODE_CONF) { + if file.is_empty() { + warn!("{} is empty", ASUS_LED_MODE_CONF); + } else { + let mut tmp: LedSupportFile = ron::from_str(&file) + .map_err(|e| error!("{e}")) + .unwrap_or_else(|_| panic!("Could not deserialise {}", ASUS_LED_MODE_CONF)); + data.0.append(&mut tmp.0); + loaded = true; + info!( + "Loaded default LED support data from {}", + ASUS_LED_MODE_CONF + ); } } data.0.sort_by(|a, b| a.board_name.cmp(&b.board_name)); @@ -125,7 +116,7 @@ impl LedSupportFile { #[cfg(test)] mod tests { use std::fs::OpenOptions; - use std::io::{Read, Write}; + use std::io::Write; use std::path::PathBuf; use ron::ser::PrettyConfig; @@ -155,9 +146,7 @@ mod tests { let mut data = PathBuf::from(env!("CARGO_MANIFEST_DIR")); data.push("data/aura_support.ron"); - let mut file = OpenOptions::new().read(true).open(&data).unwrap(); - let mut buf = String::new(); - file.read_to_string(&mut buf).unwrap(); + let buf = std::fs::read_to_string(&data).unwrap(); let tmp = ron::from_str::(&buf).unwrap(); diff --git a/rog-aura/src/builtin_modes.rs b/rog-aura/src/builtin_modes.rs index ddae1fe0..47f8467e 100644 --- a/rog-aura/src/builtin_modes.rs +++ b/rog-aura/src/builtin_modes.rs @@ -284,7 +284,8 @@ impl FromStr for AuraZone { } /// Default factory modes structure. This easily converts to an USB HID packet -/// with: ```rust +/// with: +/// ```rust /// // let bytes: [u8; LED_MSG_LEN] = mode.into(); /// ``` #[cfg_attr(feature = "dbus", derive(Type))] @@ -350,6 +351,7 @@ pub struct AuraParameters { pub direction: bool, } +#[allow(clippy::fn_params_excessive_bools)] impl AuraParameters { pub const fn new( zone: bool, diff --git a/rog-aura/src/effects/base.rs b/rog-aura/src/effects/base.rs index 32aae496..fce1b490 100644 --- a/rog-aura/src/effects/base.rs +++ b/rog-aura/src/effects/base.rs @@ -28,6 +28,6 @@ impl EffectState for InputBased { } fn set_led(&mut self, address: LedCode) { - self.led = address + self.led = address; } } diff --git a/rog-aura/src/layouts.rs b/rog-aura/src/layouts.rs index f540ae66..4a7d6044 100644 --- a/rog-aura/src/layouts.rs +++ b/rog-aura/src/layouts.rs @@ -2,8 +2,6 @@ //! editable config. use std::collections::{HashMap, HashSet}; -use std::fs::{self, OpenOptions}; -use std::io::Read; use std::path::{Path, PathBuf}; use std::slice::Iter; @@ -88,8 +86,8 @@ impl KeyShape { /// The first `Key` will determine the row height. /// /// Every row is considered to start a x=0, with the first row being y=0, -/// and following rows starting after the previous row_y+pad_top and -/// row_x+pad_left +/// and following rows starting after the previous `row_y + pad_top` and +/// `row_x + pad_left` #[derive(Debug, Deserialize, Serialize, Clone)] pub struct KeyRow { pad_left: f32, @@ -138,7 +136,7 @@ impl KeyRow { }; if h < height { - h = height + h = height; } } h @@ -192,15 +190,9 @@ pub struct KeyLayout { impl KeyLayout { pub fn from_file(path: &Path) -> Result { - let mut file = OpenOptions::new() - .read(true) - .open(path) + let buf: String = std::fs::read_to_string(path) .map_err(|e| Error::IoPath(path.to_string_lossy().to_string(), e))?; - let mut buf = String::new(); - let read_len = file - .read_to_string(&mut buf) - .map_err(|e| Error::IoPath(path.to_string_lossy().to_string(), e))?; - if read_len == 0 { + if buf.is_empty() { Err(Error::IoPath( path.to_string_lossy().to_string(), std::io::ErrorKind::InvalidData.into(), @@ -233,7 +225,7 @@ impl KeyLayout { } } - pub fn rows(&self) -> Iter { + pub fn rows(&self) -> Iter<'_, KeyRow> { self.key_rows.iter() } @@ -279,7 +271,7 @@ impl KeyLayout { for r in &self.key_rows { let tmp = r.width(); if width < tmp { - width = tmp + width = tmp; } } width @@ -305,7 +297,7 @@ impl KeyLayout { data_path.push("layouts"); let path = data_path.as_path(); let mut files = Vec::new(); - fs::read_dir(path) + std::fs::read_dir(path) .map_err(|e| { println!("{:?}, {e}", path); e @@ -471,7 +463,6 @@ mod tests { data_path.push("data"); data_path.push("layouts"); let path = data_path.as_path(); - let mut buf = String::new(); for p in fs::read_dir(path) .map_err(|e| { println!("{:?}, {e}", path); @@ -479,9 +470,7 @@ mod tests { }) .unwrap() { - let path = p.unwrap().path(); - let mut file = OpenOptions::new().read(true).open(&path).unwrap(); - file.read_to_string(&mut buf).unwrap(); + let mut buf = std::fs::read_to_string(p.unwrap().path()).unwrap(); let data: KeyLayout = ron::from_str(&buf).unwrap(); @@ -501,9 +490,10 @@ mod tests { } } - if !unused.is_empty() { - panic!("The layout {path:?} had unused shapes {unused:?}",); - } + assert!( + unused.is_empty(), + "The layout {path:?} had unused shapes {unused:?}", + ); buf.clear(); } @@ -527,9 +517,7 @@ mod tests { data_path.push("data"); data_path.push("aura_support.ron"); - let mut buf = String::new(); - let mut file = OpenOptions::new().read(true).open(&data_path).unwrap(); - file.read_to_string(&mut buf).unwrap(); + let mut buf = std::fs::read_to_string(&data_path).unwrap(); let data: LedSupportFile = ron::from_str(&buf).unwrap(); data_path.pop(); @@ -553,6 +541,7 @@ mod tests { ) }) .unwrap(); + #[allow(clippy::verbose_file_reads)] if let Err(e) = file.read_to_string(&mut buf) { panic!( "Error checking {data_path:?} for {} : {e:?}", diff --git a/rog-control-center/Cargo.toml b/rog-control-center/Cargo.toml index 5f4fcd7a..1b2c6896 100644 --- a/rog-control-center/Cargo.toml +++ b/rog-control-center/Cargo.toml @@ -41,3 +41,6 @@ png_pong.workspace = true nix = "^0.26.1" tempfile = "3.3.0" + +[dev-dependencies] +cargo-husky.workspace = true \ No newline at end of file diff --git a/rog-control-center/src/config.rs b/rog-control-center/src/config.rs index bad72070..fd9d4632 100644 --- a/rog-control-center/src/config.rs +++ b/rog-control-center/src/config.rs @@ -60,6 +60,8 @@ impl Config { let mut buf = String::new(); + // Lint to allow, because we want the above file behaviour + #[allow(clippy::verbose_file_reads)] if let Ok(read_len) = file.read_to_string(&mut buf) { if read_len == 0 { warn!("Zero len read of Config file"); diff --git a/rog-control-center/src/lib.rs b/rog-control-center/src/lib.rs index f03a7ca2..cc677ca2 100644 --- a/rog-control-center/src/lib.rs +++ b/rog-control-center/src/lib.rs @@ -81,6 +81,8 @@ pub fn on_tmp_dir_exists() -> Result { // First entry is the actual state if buf[0] == SHOWING_GUI { ipc_file.write_all(&[SHOWING_GUI])?; // Store state again as we drained the fifo + // Early exit is not an error and we don't want to pass back a dir + #[allow(clippy::exit)] exit(0); } else if buf[0] == SHOW_GUI { remove_dir_all(&path)?; @@ -89,7 +91,7 @@ pub fn on_tmp_dir_exists() -> Result { .rand_bytes(0) .tempdir(); } - exit(-1); + panic!("Invalid exit or app state"); } pub fn get_ipc_file() -> Result { diff --git a/rog-control-center/src/main.rs b/rog-control-center/src/main.rs index 89f8bd5e..046d4bd7 100644 --- a/rog-control-center/src/main.rs +++ b/rog-control-center/src/main.rs @@ -1,5 +1,4 @@ use std::env::args; -use std::fs::OpenOptions; use std::io::{Read, Write}; use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; @@ -101,15 +100,10 @@ fn main() -> Result<()> { let enabled_notifications = EnabledNotifications::tokio_mutex(&config); // Find and load a matching layout for laptop - let mut file = OpenOptions::new() - .read(true) - .open(PathBuf::from(BOARD_NAME)) - .map_err(|e| { - println!("DOH! {BOARD_NAME}, {e}"); - e - })?; - let mut board_name = String::new(); - file.read_to_string(&mut board_name)?; + let mut board_name = std::fs::read_to_string(BOARD_NAME).map_err(|e| { + println!("DOH! {BOARD_NAME}, {e}"); + e + })?; let mut led_support = LaptopLedData::get_data(); @@ -122,7 +116,7 @@ fn main() -> Result<()> { path.push("rog-aura"); path.push("data"); } - layouts = KeyLayout::layout_files(path.to_owned()).unwrap(); + layouts = KeyLayout::layout_files(path.clone()).unwrap(); if let Some(name) = &cli_parsed.board_name { if let Some(modes) = LedSupportFile::load_from_config() { @@ -130,7 +124,7 @@ fn main() -> Result<()> { led_support = data; } } - board_name = name.to_owned(); + board_name = name.clone(); for layout in &layouts { if layout .file_name() @@ -138,11 +132,11 @@ fn main() -> Result<()> { .to_string_lossy() .contains(&led_support.layout_name.to_lowercase()) { - layout_name = Some(layout.to_owned()); + layout_name = Some(layout.clone()); } } } else { - board_name = "GQ401QM".to_string() + board_name = "GQ401QM".to_owned(); }; if cli_parsed.layout_viewing { @@ -182,7 +176,7 @@ fn main() -> Result<()> { layout_name, layout, layouts, - enabled_notifications, + &enabled_notifications, &config, &supported, )?; @@ -222,7 +216,7 @@ fn setup_page_state_and_notifs( layout_testing: Option, keyboard_layout: KeyLayout, keyboard_layouts: Vec, - enabled_notifications: Arc>, + enabled_notifications: &Arc>, config: &Config, supported: &SupportedFunctions, ) -> Result>> { @@ -234,7 +228,7 @@ fn setup_page_state_and_notifs( supported, )?)); - start_notifications(config, page_states.clone(), enabled_notifications)?; + start_notifications(config, &page_states, enabled_notifications)?; Ok(page_states) } @@ -253,21 +247,15 @@ fn start_app(states: Arc>, native_options: NativeOptions) -> /// Bah.. the icon dosn't work on wayland anyway, but we'll leave it in for now. fn load_icon() -> IconData { let path = PathBuf::from(APP_ICON_PATH); - let mut buf = Vec::new(); let mut rgba = Vec::new(); let mut height = 512; let mut width = 512; if path.exists() { - if let Ok(mut file) = OpenOptions::new() - .read(true) - .open(path) + if let Ok(data) = std::fs::read(path) + .map_err(|e| error!("Error reading app icon: {e:?}")) .map_err(|e| error!("Error opening app icon: {e:?}")) { - file.read_to_end(&mut buf) - .map_err(|e| error!("Error reading app icon: {e:?}")) - .ok(); - - let data = std::io::Cursor::new(buf); + let data = std::io::Cursor::new(data); let decoder = png_pong::Decoder::new(data).unwrap().into_steps(); let png_pong::Step { raster, delay: _ } = decoder.last().unwrap().unwrap(); @@ -295,7 +283,7 @@ fn do_cli_help(parsed: &CliStart) -> bool { println!(); if let Some(cmdlist) = CliStart::command_list() { let commands: Vec = cmdlist.lines().map(|s| s.to_owned()).collect(); - for command in commands.iter() { + for command in &commands { println!("{}", command); } } diff --git a/rog-control-center/src/tray.rs b/rog-control-center/src/tray.rs index 6b3261e3..4b40a608 100644 --- a/rog-control-center/src/tray.rs +++ b/rog-control-center/src/tray.rs @@ -473,13 +473,11 @@ pub fn init_tray( }; std::thread::spawn(move || { - if gtk::init() - .map_err(|e| { - error!("ROGTray: gtk init {e}"); - e - }) - .is_err() - { + let gtk_init = gtk::init().map_err(|e| { + error!("ROGTray: gtk init {e}"); + e + }); + if gtk_init.is_err() { return; } // Make this the main thread for gtk debug!("init_tray gtk"); diff --git a/rog-control-center/src/update_and_notify.rs b/rog-control-center/src/update_and_notify.rs index d3662863..de918ac2 100644 --- a/rog-control-center/src/update_and_notify.rs +++ b/rog-control-center/src/update_and_notify.rs @@ -139,8 +139,8 @@ type SharedHandle = Arc>>; pub fn start_notifications( config: &Config, - page_states: Arc>, - enabled_notifications: Arc>, + page_states: &Arc>, + enabled_notifications: &Arc>, ) -> Result<()> { let last_notification: SharedHandle = Arc::new(Mutex::new(None)); @@ -556,7 +556,7 @@ fn do_mux_notification(message: &str, m: &GpuMode) -> Result<()> { } else if id == "__closed" { // TODO: cancel the switching } - }) + }); }); Ok(()) } diff --git a/rog-control-center/src/widgets/keyboard_layout.rs b/rog-control-center/src/widgets/keyboard_layout.rs index 67385d7d..8dd5c2e6 100644 --- a/rog-control-center/src/widgets/keyboard_layout.rs +++ b/rog-control-center/src/widgets/keyboard_layout.rs @@ -25,7 +25,7 @@ pub fn keyboard( AdvancedAuraType::None => (false, keyboard_layout.max_width(), false), AdvancedAuraType::Zoned(zones) => { let width = if let Some(row) = keyboard_layout.rows_ref().get(2) { - row.width() as f32 + row.width() } else { 0.0 }; diff --git a/rog-dbus/Cargo.toml b/rog-dbus/Cargo.toml index 85618500..b3118d92 100644 --- a/rog-dbus/Cargo.toml +++ b/rog-dbus/Cargo.toml @@ -15,3 +15,6 @@ rog_aura = { path = "../rog-aura" } rog_profiles = { path = "../rog-profiles" } rog_platform = { path = "../rog-platform" } zbus.workspace = true + +[dev-dependencies] +cargo-husky.workspace = true \ No newline at end of file diff --git a/rog-dbus/src/zbus_led.rs b/rog-dbus/src/zbus_led.rs index 17f2bdd2..f13a20c3 100644 --- a/rog-dbus/src/zbus_led.rs +++ b/rog-dbus/src/zbus_led.rs @@ -1,6 +1,6 @@ -//! # DBus interface proxy for: `org.asuslinux.Daemon` +//! # `DBus` interface proxy for: `org.asuslinux.Daemon` //! -//! This code was generated by `zbus-xmlgen` `1.0.0` from DBus introspection +//! This code was generated by `zbus-xmlgen` `1.0.0` from `DBus` introspection //! data. Source: `Interface '/org/asuslinux/Aura' from service //! 'org.asuslinux.Daemon' on system bus`. //! @@ -10,8 +10,8 @@ //! [Writing a client proxy](https://zeenix.pages.freedesktop.org/zbus/client.html) //! section of the zbus documentation. //! -//! This DBus object implements -//! [standard DBus interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html), +//! This `DBus` object implements +//! [standard `DBus` interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html), //! (`org.freedesktop.DBus.*`) for which the following zbus proxies can be used: //! //! * [`zbus::fdo::PeerProxy`] diff --git a/rog-dbus/src/zbus_platform.rs b/rog-dbus/src/zbus_platform.rs index b59e8f5f..807eef48 100644 --- a/rog-dbus/src/zbus_platform.rs +++ b/rog-dbus/src/zbus_platform.rs @@ -1,6 +1,6 @@ -//! # DBus interface proxy for: `org.asuslinux.Daemon` +//! # `DBus` interface proxy for: `org.asuslinux.Daemon` //! -//! This code was generated by `zbus-xmlgen` `1.0.0` from DBus introspection +//! This code was generated by `zbus-xmlgen` `1.0.0` from `DBus` introspection //! data. Source: `Interface '/org/asuslinux/Platform' from service //! 'org.asuslinux.Daemon' on system bus`. //! @@ -10,8 +10,8 @@ //! [Writing a client proxy](https://zeenix.pages.freedesktop.org/zbus/client.html) //! section of the zbus documentation. //! -//! This DBus object implements -//! [standard DBus interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html), +//! This `DBus` object implements +//! [standard `DBus` interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html), //! (`org.freedesktop.DBus.*`) for which the following zbus proxies can be used: //! //! * [`zbus::fdo::PropertiesProxy`] diff --git a/rog-dbus/src/zbus_power.rs b/rog-dbus/src/zbus_power.rs index 3a16e34e..44833019 100644 --- a/rog-dbus/src/zbus_power.rs +++ b/rog-dbus/src/zbus_power.rs @@ -1,6 +1,6 @@ -//! # DBus interface proxy for: `org.asuslinux.Daemon` +//! # `DBus` interface proxy for: `org.asuslinux.Daemon` //! -//! This code was generated by `zbus-xmlgen` `1.0.0` from DBus introspection +//! This code was generated by `zbus-xmlgen` `1.0.0` from `DBus` introspection //! data. Source: `Interface '/org/asuslinux/Charge' from service //! 'org.asuslinux.Daemon' on system bus`. //! @@ -10,8 +10,8 @@ //! [Writing a client proxy](https://zeenix.pages.freedesktop.org/zbus/client.html) //! section of the zbus documentation. //! -//! This DBus object implements -//! [standard DBus interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html), +//! This `DBus` object implements +//! [standard `DBus` interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html), //! (`org.freedesktop.DBus.*`) for which the following zbus proxies can be used: //! //! * [`zbus::fdo::PropertiesProxy`] diff --git a/rog-dbus/src/zbus_profile.rs b/rog-dbus/src/zbus_profile.rs index 24a58b5d..d438f4e5 100644 --- a/rog-dbus/src/zbus_profile.rs +++ b/rog-dbus/src/zbus_profile.rs @@ -1,6 +1,6 @@ -//! # DBus interface proxy for: `org.asuslinux.Daemon` +//! # `DBus` interface proxy for: `org.asuslinux.Daemon` //! -//! This code was generated by `zbus-xmlgen` `1.0.0` from DBus introspection +//! This code was generated by `zbus-xmlgen` `1.0.0` from `DBus` introspection //! data. Source: `Interface '/org/asuslinux/Profile' from service //! 'org.asuslinux.Daemon' on system bus`. //! @@ -10,8 +10,8 @@ //! [Writing a client proxy](https://zeenix.pages.freedesktop.org/zbus/client.html) //! section of the zbus documentation. //! -//! This DBus object implements -//! [standard DBus interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html), +//! This `DBus object implements +//! [standard DBus` interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html), //! (`org.freedesktop.DBus.*`) for which the following zbus proxies can be used: //! //! * [`zbus::fdo::IntrospectableProxy`] diff --git a/rog-dbus/src/zbus_supported.rs b/rog-dbus/src/zbus_supported.rs index 4862ca2c..f4bc912a 100644 --- a/rog-dbus/src/zbus_supported.rs +++ b/rog-dbus/src/zbus_supported.rs @@ -1,6 +1,6 @@ -//! # DBus interface proxy for: `org.asuslinux.Daemon` +//! # `DBus` interface proxy for: `org.asuslinux.Daemon` //! -//! This code was generated by `zbus-xmlgen` `1.0.0` from DBus introspection +//! This code was generated by `zbus-xmlgen` `1.0.0` from `DBus` introspection //! data. Source: `Interface '/org/asuslinux/Supported' from service //! 'org.asuslinux.Daemon' on system bus`. //! @@ -10,8 +10,8 @@ //! [Writing a client proxy](https://zeenix.pages.freedesktop.org/zbus/client.html) //! section of the zbus documentation. //! -//! This DBus object implements -//! [standard DBus interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html), +//! This `DBus` object implements +//! [standard `DBus` interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html), //! (`org.freedesktop.DBus.*`) for which the following zbus proxies can be used: //! //! * [`zbus::fdo::PeerProxy`] diff --git a/rog-platform/Cargo.toml b/rog-platform/Cargo.toml index af2b29e1..b412a887 100644 --- a/rog-platform/Cargo.toml +++ b/rog-platform/Cargo.toml @@ -16,3 +16,6 @@ udev.workspace = true inotify.workspace = true rusb.workspace = true + +[dev-dependencies] +cargo-husky.workspace = true \ No newline at end of file diff --git a/rog-platform/src/lib.rs b/rog-platform/src/lib.rs index 4666158a..e4bfbccb 100644 --- a/rog-platform/src/lib.rs +++ b/rog-platform/src/lib.rs @@ -51,7 +51,7 @@ pub fn write_attr_bool(device: &mut Device, attr: &str, value: bool) -> Result<( pub fn read_attr_u8(device: &Device, attr_name: &str) -> Result { if let Some(value) = device.attribute_value(attr_name) { let tmp = value.to_string_lossy(); - return tmp.parse::().map_err(|_| PlatformError::ParseNum); + return tmp.parse::().map_err(|_e| PlatformError::ParseNum); } Err(PlatformError::AttrNotFound(attr_name.to_owned())) } @@ -114,41 +114,3 @@ mod tests { assert_eq!(tmp, &[1, 2, 3, 4, 5]); } } - -// pub fn find_led_node(id_product: &str) -> Result { -// let mut enumerator = udev::Enumerator::new().map_err(|err| { -// warn!("{}", err); -// PlatformError::Udev("enumerator failed".into(), err) -// })?; -// enumerator.match_subsystem("hidraw").map_err(|err| { -// warn!("{}", err); -// PlatformError::Udev("match_subsystem failed".into(), err) -// })?; - -// for device in enumerator.scan_devices().map_err(|err| { -// warn!("{}", err); -// PlatformError::Udev("scan_devices failed".into(), err) -// })? { -// if let Some(parent) = device -// .parent_with_subsystem_devtype("usb", "usb_device") -// .map_err(|err| { -// warn!("{}", err); -// PlatformError::Udev("parent_with_subsystem_devtype -// failed".into(), err) })? -// { -// if parent -// .attribute_value("idProduct") -// .ok_or_else(|| PlatformError::NotFound("LED -// idProduct".into()))? == id_product -// { -// if let Some(dev_node) = device.devnode() { -// info!("Using device at: {:?} for LED control", dev_node); -// return Ok(device); -// } -// } -// } -// } -// Err(PlatformError::MissingFunction( -// "ASUS LED device node not found".into(), -// )) -// } diff --git a/rog-profiles/Cargo.toml b/rog-profiles/Cargo.toml index 2af824d1..0023850a 100644 --- a/rog-profiles/Cargo.toml +++ b/rog-profiles/Cargo.toml @@ -15,3 +15,6 @@ serde.workspace = true serde_derive.workspace = true zbus = { workspace = true, optional = true } + +[dev-dependencies] +cargo-husky.workspace = true \ No newline at end of file diff --git a/rog-profiles/src/error.rs b/rog-profiles/src/error.rs index 34e01948..5594b5cd 100644 --- a/rog-profiles/src/error.rs +++ b/rog-profiles/src/error.rs @@ -19,7 +19,7 @@ pub enum ProfileError { impl fmt::Display for ProfileError { // This trait requires `fmt` with this exact signature. - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ProfileError::Path(path, error) => write!(f, "Path {}: {}", path, error), ProfileError::Read(path, error) => write!(f, "Read {}: {}", path, error), diff --git a/rog-profiles/src/fan_curve_set.rs b/rog-profiles/src/fan_curve_set.rs index fb47881a..8db6b651 100644 --- a/rog-profiles/src/fan_curve_set.rs +++ b/rog-profiles/src/fan_curve_set.rs @@ -7,7 +7,7 @@ use crate::error::ProfileError; use crate::FanCurvePU; pub(crate) fn pwm_str(fan: char, index: usize) -> String { - let mut buf = "pwm1_auto_point1_pwm".to_string(); + let mut buf = "pwm1_auto_point1_pwm".to_owned(); unsafe { let tmp = buf.as_bytes_mut(); tmp[3] = fan as u8; @@ -17,7 +17,7 @@ pub(crate) fn pwm_str(fan: char, index: usize) -> String { } pub(crate) fn temp_str(fan: char, index: usize) -> String { - let mut buf = "pwm1_auto_point1_temp".to_string(); + let mut buf = "pwm1_auto_point1_temp".to_owned(); unsafe { let tmp = buf.as_bytes_mut(); tmp[3] = fan as u8; @@ -146,10 +146,10 @@ impl CurveData { for attr in device.attributes() { let tmp = attr.name().to_string_lossy(); if tmp.starts_with("pwm1") && tmp.ends_with("_temp") { - Self::set_val_from_attr(tmp.as_ref(), device, &mut self.temp) + Self::set_val_from_attr(tmp.as_ref(), device, &mut self.temp); } if tmp.starts_with("pwm1") && tmp.ends_with("_pwm") { - Self::set_val_from_attr(tmp.as_ref(), device, &mut self.pwm) + Self::set_val_from_attr(tmp.as_ref(), device, &mut self.pwm); } } } diff --git a/rog-profiles/src/lib.rs b/rog-profiles/src/lib.rs index 269c2fa0..e7242fd7 100644 --- a/rog-profiles/src/lib.rs +++ b/rog-profiles/src/lib.rs @@ -2,8 +2,8 @@ pub mod error; pub mod fan_curve_set; use std::fmt::Display; -use std::fs::OpenOptions; -use std::io::{Read, Write}; +use std::fs::{self, OpenOptions}; +use std::io::Write; use std::path::Path; use error::ProfileError; @@ -49,18 +49,12 @@ impl Profile { } pub fn get_active_profile() -> Result { - let mut file = OpenOptions::new().read(true).open(PLATFORM_PROFILE)?; - - let mut buf = String::new(); - file.read_to_string(&mut buf)?; + let buf = fs::read_to_string(PLATFORM_PROFILE)?; Ok(buf.as_str().into()) } pub fn get_profile_names() -> Result, ProfileError> { - let mut file = OpenOptions::new().read(true).open(PLATFORM_PROFILES)?; - - let mut buf = String::new(); - file.read_to_string(&mut buf)?; + let buf = fs::read_to_string(PLATFORM_PROFILES)?; Ok(buf.rsplit(' ').map(|p| p.into()).collect()) }