chore: apply recommendations from clippy

This commit is contained in:
Cheong Lau 2025-10-04 10:51:18 +10:00 committed by GitHub
parent cec55dafd7
commit 8e0f1c4a09
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
56 changed files with 720 additions and 824 deletions

View file

@ -52,7 +52,7 @@ async fn start_listening(
let mut active_conns_changed = network_manager.receive_active_connections_changed().await;
active_conns_changed.next().await;
while let (Some(_change), _) = tokio::join!(
while let (Some(_change), ()) = tokio::join!(
active_conns_changed.next(),
tokio::time::sleep(tokio::time::Duration::from_secs(1))
) {

View file

@ -24,7 +24,7 @@ pub async fn handle_wireless_device(
if let Some(t) = scan_changed.next().await {
if let Ok(-1) = t.get().await {
eprintln!("scan errored");
return Ok(Default::default());
return Ok(Vec::new());
}
}
let access_points = device.get_access_points().await?;
@ -33,8 +33,7 @@ pub async fn handle_wireless_device(
.await
.and_then(|dev| dev.cached_state())
.unwrap_or_default()
.map(|s| s.into())
.unwrap_or_else(|| DeviceState::Unknown);
.map_or(DeviceState::Unknown, |s| s.into());
// Sort by strength and remove duplicates
let mut aps = HashMap::<String, AccessPoint>::new();
for ap in access_points {
@ -45,7 +44,7 @@ pub async fn handle_wireless_device(
if access_point.strength > strength {
continue;
}
};
}
let proxy: &AccessPointProxy = &ap;
let Ok(flags) = ap.rsn_flags().await else {
continue;
@ -100,6 +99,7 @@ pub struct AccessPoint {
// TODO do we want to support eap methods other than peap in the applet?
// Then we'd need a dropdown for the eap method,
// and tls requires a cert instead of a password
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, Copy)]
pub enum NetworkType {
Open,

View file

@ -42,7 +42,7 @@ pub async fn active_connections(
Some(SpecificDevice::Wired(wired_device)) => {
info.push(ActiveConnectionInfo::Wired {
name: connection.id().await?,
hw_address: HwAddress::from_string(&wired_device.hw_address().await?)
hw_address: HwAddress::from_str(&wired_device.hw_address().await?)
.unwrap_or_default(),
speed: wired_device.speed().await?,
ip_addresses: addresses.clone(),
@ -53,10 +53,8 @@ pub async fn active_connections(
info.push(ActiveConnectionInfo::WiFi {
name: String::from_utf8_lossy(&access_point.ssid().await?).into_owned(),
ip_addresses: addresses.clone(),
hw_address: HwAddress::from_string(
&wireless_device.hw_address().await?,
)
.unwrap_or_default(),
hw_address: HwAddress::from_str(&wireless_device.hw_address().await?)
.unwrap_or_default(),
state,
strength: access_point.strength().await.unwrap_or_default(),
});

View file

@ -51,7 +51,7 @@ async fn start_listening(
let mut devices_changed = network_manager.receive_devices_changed().await;
let secs = if has_popup { 4 } else { 60 };
while let (Some(_change), _) = tokio::join!(
while let (Some(_change), ()) = tokio::join!(
devices_changed.next(),
tokio::time::sleep(tokio::time::Duration::from_secs(secs))
) {

View file

@ -1,3 +1,5 @@
use std::fmt::Write;
#[derive(Copy, Clone, PartialEq, Eq, Default, Debug, PartialOrd, Ord)]
pub struct HwAddress {
address: u64,
@ -5,7 +7,7 @@ pub struct HwAddress {
impl HwAddress {
pub fn from_str(arg: &str) -> Option<Self> {
let columnless_vec = arg.split(":").collect::<Vec<&str>>();
let columnless_vec = arg.split(':').collect::<Box<[_]>>();
if columnless_vec.len() * 3 - 1 != arg.len() {
return None;
}
@ -16,24 +18,19 @@ impl HwAddress {
}
u64::from_str_radix(columnless_vec.join("").as_str(), 16)
.ok()
.and_then(|address| Some(HwAddress { address }))
}
pub fn from_string(arg: &String) -> Option<Self> {
HwAddress::from_str(arg.as_str())
}
pub fn to_string(&self) -> String {
// return if self.address > 100000000000000 {
// "Intel Corp".to_string()
// } else {
// "TP-Link".to_string()
// };
format!("{:#x}", self.address)
.trim_start_matches("0x")
.chars()
.collect::<Vec<_>>()
.chunks(2)
.map(|chunk| chunk.iter().cloned().collect::<String>())
.collect::<Vec<String>>()
.join(":")
.map(|address| HwAddress { address })
}
}
impl std::fmt::Display for HwAddress {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (index, c) in format!("{:x}", self.address).char_indices() {
if index != 0 && index % 2 == 0 {
f.write_char(':')?;
}
f.write_char(c)?;
}
Ok(())
}
}

View file

@ -74,9 +74,8 @@ async fn start_listening(
) -> State {
match state {
State::Ready => {
let conn = match Connection::system().await {
Ok(c) => c,
Err(_) => return State::Finished,
let Ok(conn) = Connection::system().await else {
return State::Finished;
};
let (tx, rx) = unbounded();
@ -96,9 +95,8 @@ async fn start_listening(
}
}
State::Waiting(conn, mut rx) => {
let network_manager = match NetworkManager::new(&conn).await {
Ok(n) => n,
Err(_) => return State::Finished,
let Ok(network_manager) = NetworkManager::new(&conn).await else {
return State::Finished;
};
match rx.next().await {
@ -114,7 +112,7 @@ async fn start_listening(
}
let mut is_there_device = false;
for device in c.devices().await.unwrap_or_default() {
if HwAddress::from_string(device.hw_address().await.as_ref().unwrap())
if HwAddress::from_str(device.hw_address().await.as_ref().unwrap())
== Some(hw_address)
{
is_there_device = true;
@ -288,7 +286,7 @@ async fn start_listening(
_ => {
return State::Finished;
}
};
}
State::Waiting(conn, rx)
}
@ -494,7 +492,7 @@ impl NetworkManagerState {
ap.network_type,
ap.working,
ap.state
)
);
}
self_.active_conns = active_conns;
self_.known_access_points = known_access_points;
@ -510,7 +508,7 @@ impl NetworkManagerState {
self.wireless_access_points = Vec::new();
}
async fn connect_wifi<'a>(
async fn connect_wifi(
&self,
conn: &Connection,
ssid: &str,
@ -587,7 +585,7 @@ impl NetworkManagerState {
.hw_address()
.await
.ok()
.and_then(|device_address| HwAddress::from_string(&device_address))
.and_then(|device_address| HwAddress::from_str(&device_address))
.unwrap_or_default();
if device_hw_address != hw_address {
continue;
@ -631,8 +629,8 @@ impl NetworkManagerState {
let (_, active_conn) = nm
.add_and_activate_connection(conn_settings, device.inner().path(), &ap.path)
.await?;
let dummy = ActiveConnectionProxy::new(&conn, active_conn).await?;
let active = ActiveConnectionProxy::builder(&conn)
let dummy = ActiveConnectionProxy::new(conn, active_conn).await?;
let active = ActiveConnectionProxy::builder(conn)
.destination(dummy.inner().destination().to_owned())
.unwrap()
.interface(dummy.inner().interface().to_owned())
@ -645,7 +643,7 @@ impl NetworkManagerState {
ActiveConnection::from(active)
};
let mut changes = active_conn.receive_state_changed().await;
_ = tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
() = tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
let mut count = 5;
loop {
let state = active_conn.state().await;
@ -654,15 +652,14 @@ impl NetworkManagerState {
} else if let Ok(enums::ActiveConnectionState::Deactivated) = state {
anyhow::bail!("Failed to activate connection");
}
match tokio::time::timeout(Duration::from_secs(20), changes.next()).await {
Ok(Some(s)) => {
let state = s.get().await.unwrap_or_default().into();
if matches!(state, enums::ActiveConnectionState::Activated) {
return Ok(());
}
if let Ok(Some(s)) =
tokio::time::timeout(Duration::from_secs(20), changes.next()).await
{
let state = s.get().await.unwrap_or_default().into();
if matches!(state, enums::ActiveConnectionState::Activated) {
return Ok(());
}
_ => {}
};
}
count -= 1;
if count <= 0 {