diff --git a/alienware/Cargo.toml b/alienware/Cargo.toml index 67b7752..1e4c841 100644 --- a/alienware/Cargo.toml +++ b/alienware/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "alienware" -version = "1.0.9" +version = "1.0.10" edition = "2021" authors = ["Alec Brown "] description = "API to control the lights on an Alienware Alpha R1/R2" @@ -16,5 +16,4 @@ rust-version = "1.58" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -regex = "1.7.1" -lazy_static = "1.4.0" +regex = "1" diff --git a/alienware/src/lib.rs b/alienware/src/lib.rs index aed75ee..31b5c7d 100644 --- a/alienware/src/lib.rs +++ b/alienware/src/lib.rs @@ -1,10 +1,10 @@ -use lazy_static::lazy_static; use regex::Regex; use std::collections::HashMap; use std::fmt; use std::fs::File; use std::io::prelude::*; use std::path::{Path, PathBuf}; +use std::sync::OnceLock; /// The possible sources of the HDMI output port #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -299,16 +299,15 @@ impl Alienware { /// Parses a single setting sysfs file fn parse_sys_file(&self, file_name: &str) -> std::io::Result> { - lazy_static! { - static ref RE: Regex = Regex::new(r"\[([^)]+)\]").unwrap(); - } + static RE: OnceLock = OnceLock::new(); + let re = RE.get_or_init(|| Regex::new(r"\[([^)]+)\]").unwrap()); let mut path_buf = PathBuf::new(); path_buf.push(&self.platform); path_buf.push(file_name); let mut file = File::open(path_buf.as_path())?; let mut contents = String::new(); file.read_to_string(&mut contents).unwrap(); - let caps = RE.captures(contents.as_str()).unwrap(); + let caps = re.captures(contents.as_str()).unwrap(); match caps.len() > 0 { true => Ok(Some(caps[1].to_string())), false => Ok(None), @@ -317,16 +316,15 @@ impl Alienware { /// Parses a sysfs file that holds an RGB setting fn parse_sys_rgb_file(&self, file_name: &str) -> std::io::Result<(u8, u8, u8)> { - lazy_static! { - static ref RE: Regex = Regex::new(r"^red: (\d+), green: (\d+), blue: (\d+)").unwrap(); - } + static RE: OnceLock = OnceLock::new(); + let re = RE.get_or_init(|| Regex::new(r"^red: (\d+), green: (\d+), blue: (\d+)").unwrap()); let mut path_buf = PathBuf::new(); path_buf.push(&self.platform); path_buf.push(file_name); let mut file = File::open(path_buf)?; let mut contents = String::new(); file.read_to_string(&mut contents).unwrap(); - match RE.captures(contents.as_str()) { + match re.captures(contents.as_str()) { Some(caps) if caps.len() == 4 => { let red = &caps[1]; let green = &caps[2]; diff --git a/alienware_cli/Cargo.toml b/alienware_cli/Cargo.toml index 48bd508..086f391 100644 --- a/alienware_cli/Cargo.toml +++ b/alienware_cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "alienware_cli" -version = "1.0.9" +version = "1.0.10" edition = "2021" authors = ["Alec Brown "] description = "Command Line app to control the lights on an Alienware Alpha R1/R2" @@ -19,9 +19,8 @@ path = "src/main.rs" name = "alienware-cli" [dependencies] -alienware = { path = "../alienware", version = "1.0.9"} -clap = { version = "4.1", features = ["derive"] } +alienware = { path = "../alienware", version = "1.0.10"} +clap = { version = "4.4", features = ["derive"] } json = "0.12.4" regex = "1" -lazy_static = "1.4.0" snapcraft = "0.3" diff --git a/alienware_cli/src/main.rs b/alienware_cli/src/main.rs index c820b5e..9916de9 100644 --- a/alienware_cli/src/main.rs +++ b/alienware_cli/src/main.rs @@ -3,25 +3,25 @@ mod cli; use alienware::{Alienware, Zone}; use clap::Parser; use json::object; -use lazy_static::lazy_static; use regex::Regex; use snapcraft::in_snap; use std::io::ErrorKind; use std::process::{exit, Command}; +use std::sync::OnceLock; fn main() { - lazy_static! { - static ref DESCRIPTION: String = format!( + static DESCRIPTION: OnceLock = OnceLock::new(); + let desc = DESCRIPTION.get_or_init(|| { + format!( "alienware-cli v{}: Command Line app to control the lights on an Alienware Alpha R1/R2", env!("CARGO_PKG_VERSION") - ); - } + ) + }); let options = cli::Options::parse(); - // parse_arguments(DESCRIPTION.as_str(), &mut options); if options.version { - println!("{}", DESCRIPTION.as_str()); + println!("{}", desc.as_str()); exit(0); } @@ -178,10 +178,9 @@ fn parse_rgb_string(input: &str) -> (u8, u8, u8) { "blue" => (0u8, 0u8, 15u8), "magenta" => (15u8, 0u8, 15u8), _ => { - lazy_static! { - static ref RE: Regex = Regex::new(r"(\d+) (\d+) (\d+)").unwrap(); - } - match RE.captures(input.as_str()) { + static RE: OnceLock = OnceLock::new(); + let re = RE.get_or_init(|| Regex::new(r"(\d+) (\d+) (\d+)").unwrap()); + match re.captures(input.as_str()) { Some(caps) => { if caps.len() == 4 { let red = &caps[1];