Skip to content

Commit

Permalink
Use combinators for WEP in wireless_config_from_dbus
Browse files Browse the repository at this point in the history
  • Loading branch information
jcronenberg committed Jan 25, 2024
1 parent 410267b commit 308a502
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
2 changes: 2 additions & 0 deletions rust/agama-dbus-server/src/network/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NetworkStateError> for zbus::fdo::Error {
Expand Down
13 changes: 13 additions & 0 deletions rust/agama-dbus-server/src/network/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,19 @@ pub enum WEPKeyType {
Passphrase = 2,
}

impl TryFrom<u32> for WEPKeyType {
type Error = NetworkStateError;

fn try_from(value: u32) -> Result<Self, Self::Error> {
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]
Expand Down
37 changes: 12 additions & 25 deletions rust/agama-dbus-server/src/network/nm/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,31 +610,18 @@ fn wireless_config_from_dbus(conn: &OwnedNestedHash) -> Option<WirelessConfig> {
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::<u32>()?).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::<u32>().cloned())
.unwrap_or_default();
wireless_config.wep_security = Some(WEPSecurity {
wep_key_type,
auth_alg,
Expand Down

0 comments on commit 308a502

Please sign in to comment.