From 308a502ddd3727ddd1a53bcc065e5274da73a336 Mon Sep 17 00:00:00 2001 From: Jorik Cronenberg Date: Thu, 25 Jan 2024 11:17:48 +0100 Subject: [PATCH] Use combinators for WEP in wireless_config_from_dbus --- rust/agama-dbus-server/src/network/error.rs | 2 + rust/agama-dbus-server/src/network/model.rs | 13 +++++++ rust/agama-dbus-server/src/network/nm/dbus.rs | 37 ++++++------------- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/rust/agama-dbus-server/src/network/error.rs b/rust/agama-dbus-server/src/network/error.rs index a308860545..04819a971b 100644 --- a/rust/agama-dbus-server/src/network/error.rs +++ b/rust/agama-dbus-server/src/network/error.rs @@ -31,6 +31,8 @@ pub enum NetworkStateError { UnexpectedConfiguration, #[error("Invalid WEP authentication algorithm: '{0}'")] InvalidWEPAuthAlg(String), + #[error("Invalid WEP key type: '{0}'")] + InvalidWEPKeyType(u32), } impl From for zbus::fdo::Error { diff --git a/rust/agama-dbus-server/src/network/model.rs b/rust/agama-dbus-server/src/network/model.rs index 29e958e261..af7654a35e 100644 --- a/rust/agama-dbus-server/src/network/model.rs +++ b/rust/agama-dbus-server/src/network/model.rs @@ -845,6 +845,19 @@ pub enum WEPKeyType { Passphrase = 2, } +impl TryFrom for WEPKeyType { + type Error = NetworkStateError; + + fn try_from(value: u32) -> Result { + match value { + 0 => Ok(WEPKeyType::Unknown), + 1 => Ok(WEPKeyType::Key), + 2 => Ok(WEPKeyType::Passphrase), + _ => Err(NetworkStateError::InvalidWEPKeyType(value)), + } + } +} + #[derive(Debug, Default, PartialEq, Clone)] pub enum WEPAuthAlg { #[default] diff --git a/rust/agama-dbus-server/src/network/nm/dbus.rs b/rust/agama-dbus-server/src/network/nm/dbus.rs index 542242f245..096ef41b91 100644 --- a/rust/agama-dbus-server/src/network/nm/dbus.rs +++ b/rust/agama-dbus-server/src/network/nm/dbus.rs @@ -610,31 +610,18 @@ fn wireless_config_from_dbus(conn: &OwnedNestedHash) -> Option { let key_mgmt: &str = security.get("key-mgmt")?.downcast_ref()?; wireless_config.security = NmKeyManagement(key_mgmt.to_string()).try_into().ok()?; - let wep_key_type = if let Some(wep_key_type) = security.get("wep-key-type") { - let wep_key_type: u32 = *wep_key_type.downcast_ref()?; - match wep_key_type { - // 0 shouldn't appear because it is treated as empty but just in case - 0 => WEPKeyType::Unknown, - 1 => WEPKeyType::Key, - 2 => WEPKeyType::Passphrase, - _ => { - log::error!("\"wep-key-type\" from NetworkManager not valid"); - WEPKeyType::default() - } - } - } else { - WEPKeyType::default() - }; - let auth_alg = if let Some(auth_alg) = security.get("auth-alg") { - WEPAuthAlg::try_from(auth_alg.downcast_ref()?).ok()? - } else { - WEPAuthAlg::default() - }; - let wep_key_index: u32 = if let Some(wep_key_index) = security.get("wep-tx-keyidx") { - *wep_key_index.downcast_ref()? - } else { - 0 - }; + let wep_key_type = security + .get("wep-key-type") + .and_then(|alg| WEPKeyType::try_from(*alg.downcast_ref::()?).ok()) + .unwrap_or_default(); + let auth_alg = security + .get("auth-alg") + .and_then(|alg| WEPAuthAlg::try_from(alg.downcast_ref()?).ok()) + .unwrap_or_default(); + let wep_key_index = security + .get("wep-tx-keyidx") + .and_then(|idx| idx.downcast_ref::().cloned()) + .unwrap_or_default(); wireless_config.wep_security = Some(WEPSecurity { wep_key_type, auth_alg,