perf: inline WiFi connect/hotspot args to eliminate Vec<String> allocation
This commit is contained in:
@@ -36,25 +36,6 @@ impl WifiPlugin {
|
||||
Self { ctx: None }
|
||||
}
|
||||
|
||||
fn build_connect_args(ssid: &str, password: &str) -> Vec<String> {
|
||||
let mut args: Vec<String> = ["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());
|
||||
}
|
||||
args
|
||||
}
|
||||
|
||||
fn build_hotspot_args(ssid: &str, password: &str) -> Vec<String> {
|
||||
["device", "wifi", "hotspot", "ssid", ssid, "password", password]
|
||||
.into_iter()
|
||||
.map(String::from)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn run_nmcli(args: &[impl AsRef<std::ffi::OsStr> + std::fmt::Debug]) -> Result<String> {
|
||||
let output = Command::new("nmcli")
|
||||
.args(args)
|
||||
@@ -189,7 +170,12 @@ impl WifiPlugin {
|
||||
}
|
||||
|
||||
fn connect_network(&self, ssid: &str, password: &str) -> Result<serde_json::Value> {
|
||||
let output = Self::run_nmcli(&Self::build_connect_args(ssid, password))?;
|
||||
let mut args = vec!["device", "wifi", "connect", ssid];
|
||||
if !password.trim().is_empty() {
|
||||
args.push("password");
|
||||
args.push(password);
|
||||
}
|
||||
let output = Self::run_nmcli(&args)?;
|
||||
|
||||
Ok(json!({
|
||||
"ok": true,
|
||||
@@ -260,7 +246,7 @@ impl WifiPlugin {
|
||||
}
|
||||
|
||||
fn ap_start(&self, ssid: &str, password: &str) -> Result<serde_json::Value> {
|
||||
let output = Self::run_nmcli(&Self::build_hotspot_args(ssid, password))?;
|
||||
let output = Self::run_nmcli(&["device", "wifi", "hotspot", "ssid", ssid, "password", password])?;
|
||||
|
||||
Ok(json!({
|
||||
"ok": true,
|
||||
@@ -362,40 +348,4 @@ mod tests {
|
||||
|
||||
assert_eq!(fields, vec!["wlan0", "wifi", "connected", "Office:LAN"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connect_args_preserve_special_characters() {
|
||||
let args =
|
||||
WifiPlugin::build_connect_args(r#"ssid \"qa\" demo"#, r#"p@ss\\word with spaces"#);
|
||||
|
||||
assert_eq!(
|
||||
args,
|
||||
vec![
|
||||
"device",
|
||||
"wifi",
|
||||
"connect",
|
||||
r#"ssid \"qa\" demo"#,
|
||||
"password",
|
||||
r#"p@ss\\word with spaces"#,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hotspot_args_preserve_special_characters() {
|
||||
let args = WifiPlugin::build_hotspot_args("Showen AP", r#"\\quoted pass\\"#);
|
||||
|
||||
assert_eq!(
|
||||
args,
|
||||
vec![
|
||||
"device",
|
||||
"wifi",
|
||||
"hotspot",
|
||||
"ssid",
|
||||
"Showen AP",
|
||||
"password",
|
||||
r#"\\quoted pass\\"#,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user