Skip to content

Commit

Permalink
OnceLock (#16)
Browse files Browse the repository at this point in the history
* Replace lazy_static with std::sync::OnceLock
  • Loading branch information
a1ecbr0wn committed Oct 3, 2023
1 parent 6e99071 commit 70411be
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
5 changes: 2 additions & 3 deletions alienware/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "alienware"
version = "1.0.9"
version = "1.0.10"
edition = "2021"
authors = ["Alec Brown <[email protected]>"]
description = "API to control the lights on an Alienware Alpha R1/R2"
Expand All @@ -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"
16 changes: 7 additions & 9 deletions alienware/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -299,16 +299,15 @@ impl Alienware {

/// Parses a single setting sysfs file
fn parse_sys_file(&self, file_name: &str) -> std::io::Result<Option<String>> {
lazy_static! {
static ref RE: Regex = Regex::new(r"\[([^)]+)\]").unwrap();
}
static RE: OnceLock<Regex> = 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),
Expand All @@ -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<Regex> = 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];
Expand Down
7 changes: 3 additions & 4 deletions alienware_cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "alienware_cli"
version = "1.0.9"
version = "1.0.10"
edition = "2021"
authors = ["Alec Brown <[email protected]>"]
description = "Command Line app to control the lights on an Alienware Alpha R1/R2"
Expand All @@ -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"
21 changes: 10 additions & 11 deletions alienware_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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);
}

Expand Down Expand Up @@ -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<Regex> = 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];
Expand Down

0 comments on commit 70411be

Please sign in to comment.