feat: Moved Network to its own file

This commit is contained in:
s-prechtl 2023-06-23 16:06:30 +02:00
parent 0dfd17cc94
commit e30ecd06e6
3 changed files with 104 additions and 25 deletions

1
src/lib.rs Normal file
View file

@ -0,0 +1 @@
pub mod network;

View file

@ -1,19 +1,6 @@
use std::{process::Command, vec}; use wifi_connector::network::Network;
use anyhow::*; use anyhow::*;
use std::{process::Command, vec};
struct Network {
in_use: bool,
ssid: String,
signal: u8,
}
impl Network {
fn from_nmcli_stdout(line: String) {
for element in line.split(' ') {
println!("{}", element);
}
}
}
fn get_available_wifis() -> Result<String> { fn get_available_wifis() -> Result<String> {
let output = Command::new("nmcli") let output = Command::new("nmcli")
@ -30,19 +17,37 @@ fn get_descriptor_positions(header_line: &String) -> Vec<u8> {
let mut positions: Vec<u8> = vec![]; let mut positions: Vec<u8> = vec![];
positions.push(0); positions.push(0);
positions.push(header_line.find(" SSID").expect("SSID not found in header string") as u8 +1); positions.push(
positions.push(header_line.find("SIGNAL").expect("SIGNAL not found in header string") as u8); header_line
.find(" SSID")
.expect("SSID not found in header string") as u8
+ 1,
);
positions.push(
header_line
.find("SIGNAL")
.expect("SIGNAL not found in header string") as u8,
);
return positions; return positions;
} }
fn main() { fn main() {
let all_wifis: String = get_available_wifis().expect("Wifi fetching exploded"); let nmcli_output: String = get_available_wifis().expect("Wifi fetching exploded");
let positions = get_descriptor_positions(&all_wifis); let positions = get_descriptor_positions(&nmcli_output);
for position in (positions) {
println!("{}", position); let mut all_wifi_lines: Vec<String> = nmcli_output.split('\n').map(|s| String::from(s)).collect();
let mut all_networks: Vec<Network> = vec![];
all_wifi_lines.remove(0);
for line in all_wifi_lines {
if line.is_empty() {
continue;
}
all_networks.push(Network::from_nmcli_stdout(line.to_owned(), &positions));
}
for network in all_networks {
dbg!("{}", network);
} }
// for line in all_wifis.split("\n") {
// Network::from_nmcli_stdout(line.to_owned());
// }
} }

73
src/network.rs Normal file
View file

@ -0,0 +1,73 @@
#[derive(Debug)]
pub struct Network {
in_use: bool,
ssid: String,
signal: u8,
}
impl Network {
pub fn from_nmcli_stdout(line: String, header_positions: &Vec<u8>) -> Self {
let mut network = Self {
in_use: false,
ssid: "".to_string(),
signal: 0,
};
let ssid_pos = header_positions
.get(1)
.expect("header_positions has no index for SSID")
.to_owned() as usize;
let signal_pos = header_positions
.get(2)
.expect("header_positions has no index for SSID")
.to_owned() as usize;
if line
.chars()
.nth(
header_positions
.get(0)
.expect("header_positions has no index 0")
.to_owned() as usize,
)
.expect("Given line has no index 0")
== '*'
{
network.in_use = true;
}
let mut current_spaces = 0;
for char in line.chars().skip(ssid_pos) {
if char == ' ' {
current_spaces += 1;
if current_spaces == 2 {
break;
}
} else {
if current_spaces == 1 {
network.ssid.push(' ');
}
current_spaces = 0;
network.ssid.push(char);
}
}
let mut signal_as_string: String = line
.chars()
.nth(signal_pos)
.expect("Line has no Signal pos")
.to_string();
signal_as_string.push(
line.chars()
.nth(signal_pos + 1)
.expect("Line has no Signal pos"),
);
network.signal = signal_as_string
.parse::<u8>()
.expect("Could not parse signal");
return network;
}
}