replace String with str and remove unnecessary structs
This commit is contained in:
parent
4b0b2626a4
commit
58f2ddde05
|
@ -32,15 +32,6 @@ use serde::{Deserialize, Serialize};
|
||||||
use crate::error::NetworkError;
|
use crate::error::NetworkError;
|
||||||
use crate::utils;
|
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.
|
/// Access point data retrieved via scan.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[cfg_attr(feature = "miniserde_support", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "miniserde_support", derive(Serialize, Deserialize))]
|
||||||
|
@ -114,17 +105,6 @@ pub struct Traffic {
|
||||||
pub transmitted: u64,
|
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 */
|
/* GET - Methods for retrieving data */
|
||||||
|
|
||||||
/// Retrieve list of available wireless access points for a given network
|
/// Retrieve list of available wireless access points for a given network
|
||||||
|
@ -310,7 +290,7 @@ pub fn rssi_percent(iface: &str) -> Result<Option<String>, NetworkError> {
|
||||||
/// of all saved networks. If no network credentials are found, a `None` type
|
/// 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
|
/// is returned in the `Result`. In the event of an error, a `NetworkError` is
|
||||||
/// returned in the `Result`.
|
/// returned in the `Result`.
|
||||||
pub fn saved_networks() -> Result<Option<Vec<Network>>, NetworkError> {
|
pub fn saved_networks() -> Result<Option<Vec<String>>, NetworkError> {
|
||||||
let mut wpa = wpactrl::WpaCtrl::builder().open()?;
|
let mut wpa = wpactrl::WpaCtrl::builder().open()?;
|
||||||
let networks = wpa.request("LIST_NETWORKS")?;
|
let networks = wpa.request("LIST_NETWORKS")?;
|
||||||
let mut ssids = Vec::new();
|
let mut ssids = Vec::new();
|
||||||
|
@ -319,8 +299,7 @@ pub fn saved_networks() -> Result<Option<Vec<Network>>, NetworkError> {
|
||||||
let len = v.len();
|
let len = v.len();
|
||||||
if len > 1 {
|
if len > 1 {
|
||||||
let ssid = v[1].trim().to_string();
|
let ssid = v[1].trim().to_string();
|
||||||
let response = Network { ssid };
|
ssids.push(ssid)
|
||||||
ssids.push(response)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -484,7 +463,7 @@ pub fn traffic(iface: &str) -> Result<Option<Traffic>, NetworkError> {
|
||||||
/// network interface. If the command executes successfully, an `Ok` `Result`
|
/// network interface. If the command executes successfully, an `Ok` `Result`
|
||||||
/// type is returned. In the event of an error, a `NetworkError` is returned
|
/// type is returned. In the event of an error, a `NetworkError` is returned
|
||||||
/// in the `Result`.
|
/// 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);
|
let iface_service = format!("wpa_supplicant@{}.service", &iface);
|
||||||
|
|
||||||
// start the interface service
|
// start the interface service
|
||||||
|
@ -493,7 +472,10 @@ pub fn start_iface_service(iface: String) -> Result<(), NetworkError> {
|
||||||
.arg("start")
|
.arg("start")
|
||||||
.arg(iface_service)
|
.arg(iface_service)
|
||||||
.output()
|
.output()
|
||||||
.map_err(|source| NetworkError::StartInterface { source, iface })?;
|
.map_err(|source| NetworkError::StartInterface {
|
||||||
|
source,
|
||||||
|
iface: iface.to_string(),
|
||||||
|
})?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -510,15 +492,15 @@ pub fn start_iface_service(iface: String) -> Result<(), NetworkError> {
|
||||||
/// `<wlan_iface>` is the provided interface parameter), an `Ok` `Result` type
|
/// `<wlan_iface>` is the provided interface parameter), an `Ok` `Result` type
|
||||||
/// is returned. In the event of an error, a `NetworkError` is returned in the
|
/// is returned. In the event of an error, a `NetworkError` is returned in the
|
||||||
/// `Result`.
|
/// `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
|
// generate configuration based on provided ssid & password
|
||||||
let output = Command::new("wpa_passphrase")
|
let output = Command::new("wpa_passphrase")
|
||||||
.arg(&wifi.ssid)
|
.arg(&ssid)
|
||||||
.arg(&wifi.pass)
|
.arg(&pass)
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.output()
|
.output()
|
||||||
.map_err(|source| NetworkError::GenWpaPassphrase {
|
.map_err(|source| NetworkError::GenWpaPassphrase {
|
||||||
ssid: wifi.ssid.to_string(),
|
ssid: ssid.to_string(),
|
||||||
source,
|
source,
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
@ -545,7 +527,7 @@ pub fn add(wlan_iface: String, wifi: &WiFi) -> Result<(), NetworkError> {
|
||||||
} else {
|
} else {
|
||||||
let err_msg = String::from_utf8_lossy(&output.stdout);
|
let err_msg = String::from_utf8_lossy(&output.stdout);
|
||||||
Err(NetworkError::GenWpaPassphraseWarning {
|
Err(NetworkError::GenWpaPassphraseWarning {
|
||||||
ssid: wifi.ssid.to_string(),
|
ssid: ssid.to_string(),
|
||||||
err_msg: err_msg.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
|
/// 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
|
/// currently connected to an access point), then the access point is activated
|
||||||
/// by calling the `activate_ap()` function.
|
/// 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);
|
let wpa_service = format!("wpa_supplicant@{}.service", &wlan_iface);
|
||||||
|
|
||||||
// returns 0 if the service is currently active
|
// 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)?;
|
.map_err(NetworkError::WlanState)?;
|
||||||
|
|
||||||
// returns the current state of the wlan interface
|
// 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
|
// returns down if the interface is not currently connected to an ap
|
||||||
let wlan_state = match iface_state {
|
let wlan_state = match iface_state {
|
||||||
|
|
Loading…
Reference in New Issue