Patch-Source: https://github.com/smasher164/pw-volume/pull/1 -- From db433e6a49ae73fa7c2f4db5ba349000979de419 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Fri, 11 Feb 2022 02:49:21 +0100 Subject: [PATCH] Replace regex-based validation of decimal % with much simpler impl Function `is_decimal_percentage` evaluates as valid the same inputs as the original regex-based and in addition to that accepts abbreviated decimal notation `.25%` (= 0.25%). This reduces the size of the resulting binary by ~58% (1.4 -> 0.6 MiB when compiling with `lto = true`, `opt-level = "z"` and `panic = "abort"`). diff --git a/src/main.rs b/src/main.rs index 93cf77c..062e028 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ use clap::{App, AppSettings, Arg, SubCommand}; -use regex::Regex; use serde::{Deserialize, Serialize}; use serde_json::Value; use std::{process, process::Command}; @@ -132,9 +131,15 @@ struct PipeWireCommand { channel_volumes: Option>, } +fn is_decimal_percentage(value: &str) -> bool { + value + .strip_suffix("%") + .and_then(|value| value.parse::().ok()) + .is_some() +} + fn main() { // parse cli flags - let decimal = Regex::new(r"^(\+|-)?\d+(\.\d*)?%$").unwrap(); let matches = App::new("pw-volume") .about("Basic interface to PipeWire volume controls") .settings(&[ @@ -167,7 +172,7 @@ fn main() { .required(true) .allow_hyphen_values(true) .validator(move |s| { - if decimal.is_match(&s) { + if is_decimal_percentage(&s) { Ok(()) } else { Err(format!(r#""{}" is not a decimal percentage"#, s)) diff --git a/Cargo.lock b/Cargo.lock index f362c54..0bffa70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,15 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "aho-corasick" -version = "0.7.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" -dependencies = [ - "memchr", -] - [[package]] name = "ansi_term" version = "0.11.0" @@ -73,12 +64,6 @@ version = "0.2.107" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fbe5e23404da5b4f555ef85ebed98fb4083e55a00c317800bc2a50ede9f3d219" -[[package]] -name = "memchr" -version = "2.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" - [[package]] name = "proc-macro2" version = "1.0.32" @@ -93,7 +78,6 @@ name = "pw-volume" version = "0.1.0" dependencies = [ "clap", - "regex", "serde", "serde_json", ] @@ -107,23 +91,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "regex" -version = "1.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.6.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" - [[package]] name = "ryu" version = "1.0.5" diff --git a/Cargo.toml b/Cargo.toml index 37005e9..701186b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,4 @@ edition = "2018" [dependencies] serde = { version = "1.0.130", features = ["derive"] } serde_json = "1.0.70" -regex = "1.4.6" -clap = "2.33.3" \ No newline at end of file +clap = "2.33.3" -- 2.16.4