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

@ -127,9 +127,8 @@ impl CosmicNetworkApplet {
self.update_togglers(&new_state);
// check for failed conns that can be reset
for new_s in &mut new_state.active_conns {
let state = match new_s {
ActiveConnectionInfo::WiFi { state, .. } => state,
_ => continue,
let ActiveConnectionInfo::WiFi { state, .. } = new_s else {
continue;
};
if matches!(state, ActiveConnectionState::Activated) {
@ -180,7 +179,7 @@ impl CosmicNetworkApplet {
_ => icon_name,
},
)
.to_string()
.to_string();
}
fn update_togglers(&mut self, state: &NetworkManagerState) {
@ -194,7 +193,7 @@ impl CosmicNetworkApplet {
chain::Toggler::off(WIFI.clone(), 1.)
};
timeline.set_chain(chain);
};
}
if state.airplane_mode != self.nm_state.airplane_mode {
changed = true;
@ -204,7 +203,7 @@ impl CosmicNetworkApplet {
chain::Toggler::off(AIRPLANE_MODE.clone(), 1.)
};
timeline.set_chain(chain);
};
}
if changed {
timeline.start();
}
@ -344,21 +343,15 @@ impl cosmic::Application for CosmicNetworkApplet {
let conn_match = self
.new_connection
.as_ref()
.map(|c| c.ssid() == ssid && c.hw_address() == *hw_address)
.unwrap_or_default();
.is_some_and(|c| c.ssid() == ssid && c.hw_address() == *hw_address);
if conn_match && success {
if let Some(s) = state
if let Some(ActiveConnectionInfo::WiFi { state, .. }) = state
.active_conns
.iter_mut()
.find(|ap| &ap.name() == ssid && ap.hw_address() == *hw_address)
{
match s {
ActiveConnectionInfo::WiFi { state, .. } => {
*state = ActiveConnectionState::Activated;
}
_ => {}
};
*state = ActiveConnectionState::Activated;
}
self.failed_known_ssids.remove(ssid);
self.new_connection = None;
@ -401,7 +394,7 @@ impl cosmic::Application for CosmicNetworkApplet {
} else if self
.new_connection
.as_ref()
.map(|c| c.ssid()).is_some_and(|ssid| {
.map(NewConnectionState::ssid).is_some_and(|ssid| {
state.active_conns.iter().any(|c|
matches!(c, ActiveConnectionInfo::WiFi { name, state: ActiveConnectionState::Activated, .. } if ssid == name)
)
@ -423,9 +416,7 @@ impl cosmic::Application for CosmicNetworkApplet {
}
},
Message::SelectWirelessAccessPoint(access_point) => {
let tx = if let Some(tx) = self.nm_sender.as_ref() {
tx
} else {
let Some(tx) = self.nm_sender.as_ref() else {
return Task::none();
};
@ -467,9 +458,7 @@ impl cosmic::Application for CosmicNetworkApplet {
}
Message::SubmitPassword => {
// save password
let tx = if let Some(tx) = self.nm_sender.as_ref() {
tx
} else {
let Some(tx) = self.nm_sender.as_ref() else {
return Task::none();
};
@ -485,12 +474,12 @@ impl cosmic::Application for CosmicNetworkApplet {
let _ = tx.unbounded_send(NetworkManagerRequest::Authenticate {
ssid: access_point.ssid.clone(),
identity: is_enterprise.then(|| identity.clone()),
password: password,
password,
hw_address: access_point.hw_address,
});
self.new_connection
.replace(NewConnectionState::Waiting(access_point));
};
}
}
Message::ActivateKnownWifi(ssid, hw_address) => {
let mut network_type = NetworkType::Open;
@ -616,7 +605,7 @@ impl cosmic::Application for CosmicNetworkApplet {
Task::none()
}
fn view(&self) -> Element<Message> {
fn view(&self) -> Element<'_, Message> {
self.core
.applet
.icon_button(&self.icon_name)
@ -624,7 +613,7 @@ impl cosmic::Application for CosmicNetworkApplet {
.into()
}
fn view_window(&self, _id: window::Id) -> Element<Message> {
fn view_window(&self, _id: window::Id) -> Element<'_, Message> {
let Spacing {
space_xxs, space_s, ..
} = theme::active().cosmic().spacing;
@ -744,7 +733,7 @@ impl cosmic::Application for CosmicNetworkApplet {
.into(),
),
_ => {}
};
}
if self.failed_known_ssids.contains(name) {
btn_content.push(
cosmic::widget::button::icon(
@ -753,7 +742,7 @@ impl cosmic::Application for CosmicNetworkApplet {
.icon_size(16)
.on_press(Message::ResetFailedKnownSsid(name.clone(), *hw_address))
.into(),
)
);
}
known_wifi.push(Element::from(
@ -768,7 +757,7 @@ impl cosmic::Application for CosmicNetworkApplet {
.align_x(Alignment::Center),
));
}
};
}
}
let mut content = if let Some(hw_device_to_show) = self.hw_device_to_show {
@ -899,7 +888,7 @@ impl cosmic::Application for CosmicNetworkApplet {
.align_y(Alignment::Center)
.spacing(8),
)
.on_press(Message::OpenHwDevice(Some(hw_device.clone()))),
.on_press(Message::OpenHwDevice(Some(hw_device))),
));
}
@ -955,7 +944,7 @@ impl cosmic::Application for CosmicNetworkApplet {
known.hw_address,
))
.into(),
)
);
}
let mut btn = menu_button(

View file

@ -42,6 +42,6 @@ pub fn localize() {
let requested_languages = i18n_embed::DesktopLanguageRequester::requested_languages();
if let Err(error) = localizer.select(&requested_languages) {
eprintln!("Error while loading language for App List {}", error);
eprintln!("Error while loading language for App List {error}");
}
}

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 {