From 58f2ddde05fad68e1b48df0e7447a6885a0aa38d Mon Sep 17 00:00:00 2001 From: glyph Date: Mon, 3 Jan 2022 11:55:37 +0200 Subject: [PATCH] replace String with str and remove unnecessary structs --- peach-network/src/network.rs | 46 +++++++++++------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/peach-network/src/network.rs b/peach-network/src/network.rs index c8aff0c..96b7334 100644 --- a/peach-network/src/network.rs +++ b/peach-network/src/network.rs @@ -32,15 +32,6 @@ use serde::{Deserialize, Serialize}; use crate::error::NetworkError; use crate::utils; -/// Network SSID. -#[derive(Debug)] -#[cfg_attr(feature = "miniserde_support", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] -pub struct Network { - /// Service Set Identifier (SSID). - pub ssid: String, -} - /// Access point data retrieved via scan. #[derive(Debug)] #[cfg_attr(feature = "miniserde_support", derive(Serialize, Deserialize))] @@ -114,17 +105,6 @@ pub struct Traffic { pub transmitted: u64, } -/// SSID and password for a wireless access point. -#[derive(Debug)] -#[cfg_attr(feature = "miniserde_support", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] -pub struct WiFi { - /// SSID. - pub ssid: String, - /// Password. - pub pass: String, -} - /* GET - Methods for retrieving data */ /// Retrieve list of available wireless access points for a given network @@ -310,7 +290,7 @@ pub fn rssi_percent(iface: &str) -> Result, NetworkError> { /// of all saved networks. If no network credentials are found, a `None` type /// is returned in the `Result`. In the event of an error, a `NetworkError` is /// returned in the `Result`. -pub fn saved_networks() -> Result>, NetworkError> { +pub fn saved_networks() -> Result>, NetworkError> { let mut wpa = wpactrl::WpaCtrl::builder().open()?; let networks = wpa.request("LIST_NETWORKS")?; let mut ssids = Vec::new(); @@ -319,8 +299,7 @@ pub fn saved_networks() -> Result>, NetworkError> { let len = v.len(); if len > 1 { let ssid = v[1].trim().to_string(); - let response = Network { ssid }; - ssids.push(response) + ssids.push(ssid) } } @@ -484,7 +463,7 @@ pub fn traffic(iface: &str) -> Result, NetworkError> { /// network interface. If the command executes successfully, an `Ok` `Result` /// type is returned. In the event of an error, a `NetworkError` is returned /// in the `Result`. -pub fn start_iface_service(iface: String) -> Result<(), NetworkError> { +pub fn start_iface_service(iface: &str) -> Result<(), NetworkError> { let iface_service = format!("wpa_supplicant@{}.service", &iface); // start the interface service @@ -493,7 +472,10 @@ pub fn start_iface_service(iface: String) -> Result<(), NetworkError> { .arg("start") .arg(iface_service) .output() - .map_err(|source| NetworkError::StartInterface { source, iface })?; + .map_err(|source| NetworkError::StartInterface { + source, + iface: iface.to_string(), + })?; Ok(()) } @@ -510,15 +492,15 @@ pub fn start_iface_service(iface: String) -> Result<(), NetworkError> { /// `` is the provided interface parameter), an `Ok` `Result` type /// is returned. In the event of an error, a `NetworkError` is returned in the /// `Result`. -pub fn add(wlan_iface: String, wifi: &WiFi) -> Result<(), NetworkError> { +pub fn add(wlan_iface: &str, ssid: &str, pass: &str) -> Result<(), NetworkError> { // generate configuration based on provided ssid & password let output = Command::new("wpa_passphrase") - .arg(&wifi.ssid) - .arg(&wifi.pass) + .arg(&ssid) + .arg(&pass) .stdout(Stdio::piped()) .output() .map_err(|source| NetworkError::GenWpaPassphrase { - ssid: wifi.ssid.to_string(), + ssid: ssid.to_string(), source, })?; @@ -545,7 +527,7 @@ pub fn add(wlan_iface: String, wifi: &WiFi) -> Result<(), NetworkError> { } else { let err_msg = String::from_utf8_lossy(&output.stdout); Err(NetworkError::GenWpaPassphraseWarning { - ssid: wifi.ssid.to_string(), + ssid: ssid.to_string(), err_msg: err_msg.to_string(), }) } @@ -558,7 +540,7 @@ pub fn add(wlan_iface: String, wifi: &WiFi) -> Result<(), NetworkError> { /// are checked. If the service is active but the interface is down (ie. not /// currently connected to an access point), then the access point is activated /// by calling the `activate_ap()` function. -pub fn check_iface(wlan_iface: String, ap_iface: String) -> Result<(), NetworkError> { +pub fn check_iface(wlan_iface: &str, ap_iface: &str) -> Result<(), NetworkError> { let wpa_service = format!("wpa_supplicant@{}.service", &wlan_iface); // returns 0 if the service is currently active @@ -569,7 +551,7 @@ pub fn check_iface(wlan_iface: String, ap_iface: String) -> Result<(), NetworkEr .map_err(NetworkError::WlanState)?; // returns the current state of the wlan interface - let iface_state = state(&wlan_iface)?; + let iface_state = state(wlan_iface)?; // returns down if the interface is not currently connected to an ap let wlan_state = match iface_state {