diff --git a/src/plugins/wifi/mod.rs b/src/plugins/wifi/mod.rs index d569753..b3582f8 100644 --- a/src/plugins/wifi/mod.rs +++ b/src/plugins/wifi/mod.rs @@ -36,12 +36,11 @@ impl WifiPlugin { Self { ctx: None } } - fn nmcli_args(parts: &[&str]) -> Vec { - parts.iter().map(|part| (*part).to_string()).collect() - } - fn build_connect_args(ssid: &str, password: &str) -> Vec { - let mut args = Self::nmcli_args(&["device", "wifi", "connect", ssid]); + let mut args: Vec = ["device", "wifi", "connect", ssid] + .into_iter() + .map(String::from) + .collect(); if !password.trim().is_empty() { args.push("password".to_string()); args.push(password.to_string()); @@ -50,18 +49,13 @@ impl WifiPlugin { } fn build_hotspot_args(ssid: &str, password: &str) -> Vec { - vec![ - "device".to_string(), - "wifi".to_string(), - "hotspot".to_string(), - "ssid".to_string(), - ssid.to_string(), - "password".to_string(), - password.to_string(), - ] + ["device", "wifi", "hotspot", "ssid", ssid, "password", password] + .into_iter() + .map(String::from) + .collect() } - fn run_nmcli(args: &[String]) -> Result { + fn run_nmcli(args: &[impl AsRef + std::fmt::Debug]) -> Result { let output = Command::new("nmcli") .args(args) .output() @@ -145,9 +139,9 @@ impl WifiPlugin { } fn scan_networks(&self) -> Result { - Self::run_nmcli(&Self::nmcli_args(&["device", "wifi", "rescan"]))?; + Self::run_nmcli(&["device", "wifi", "rescan"])?; thread::sleep(Duration::from_secs(2)); - let output = Self::run_nmcli(&Self::nmcli_args(&[ + let output = Self::run_nmcli(&[ "--terse", "--escape", "yes", @@ -156,7 +150,7 @@ impl WifiPlugin { "device", "wifi", "list", - ]))?; + ])?; let networks = output .lines() @@ -206,7 +200,7 @@ impl WifiPlugin { } fn status(&self) -> Result { - let device_output = Self::run_nmcli(&Self::nmcli_args(&[ + let device_output = Self::run_nmcli(&[ "--terse", "--escape", "yes", @@ -214,8 +208,8 @@ impl WifiPlugin { "DEVICE,TYPE,STATE,CONNECTION", "device", "status", - ]))?; - let ip_output = Self::run_nmcli(&Self::nmcli_args(&[ + ])?; + let ip_output = Self::run_nmcli(&[ "--terse", "--escape", "yes", @@ -223,7 +217,7 @@ impl WifiPlugin { "DEVICE,IP4.ADDRESS", "device", "show", - ]))?; + ])?; let mut ip_map: HashMap> = HashMap::new(); for line in ip_output.lines().filter(|line| !line.trim().is_empty()) { @@ -277,7 +271,7 @@ impl WifiPlugin { } fn ap_stop(&self) -> Result { - let active = Self::run_nmcli(&Self::nmcli_args(&[ + let active = Self::run_nmcli(&[ "--terse", "--escape", "yes", @@ -286,14 +280,14 @@ impl WifiPlugin { "connection", "show", "--active", - ]))?; + ])?;; let hotspot_name = active .lines() .map(str::trim) .find(|name| *name == "hotspot") .ok_or_else(|| anyhow!("active hotspot connection 'hotspot' not found"))?; - let output = Self::run_nmcli(&Self::nmcli_args(&["connection", "down", hotspot_name]))?; + let output = Self::run_nmcli(&["connection", "down", hotspot_name])?; Ok(json!({ "ok": true,