From db9a040b9fe46a3a100abd730cde2ecb2b4a36ab Mon Sep 17 00:00:00 2001 From: Jorik Cronenberg Date: Tue, 5 Dec 2023 12:21:52 +0100 Subject: [PATCH] Move dbus mac-address processing to separate function --- rust/agama-dbus-server/src/network/nm/dbus.rs | 40 ++++++++----------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/rust/agama-dbus-server/src/network/nm/dbus.rs b/rust/agama-dbus-server/src/network/nm/dbus.rs index 5459ec8fe7..b941a7964e 100644 --- a/rust/agama-dbus-server/src/network/nm/dbus.rs +++ b/rust/agama-dbus-server/src/network/nm/dbus.rs @@ -309,31 +309,9 @@ fn base_connection_from_dbus(conn: &OwnedNestedHash) -> Option { } if let Some(ethernet_config) = conn.get(ETHERNET_KEY) { - if let Some(mac_address) = ethernet_config.get("assigned-mac-address") { - base_connection.mac_address = - match MacAddress::from_str(mac_address.downcast_ref::()?) { - Ok(mac) => mac, - Err(e) => { - log::warn!("Couldn't parse MAC: {}", e); - MacAddress::Unset - } - }; - } else { - base_connection.mac_address = MacAddress::Unset; - } + base_connection.mac_address = mac_address_from_dbus(ethernet_config)?; } else if let Some(wireless_config) = conn.get(WIRELESS_KEY) { - if let Some(mac_address) = wireless_config.get("assigned-mac-address") { - base_connection.mac_address = - match MacAddress::from_str(mac_address.downcast_ref::()?) { - Ok(mac) => mac, - Err(e) => { - log::warn!("Couldn't parse MAC: {}", e); - MacAddress::Unset - } - }; - } else { - base_connection.mac_address = MacAddress::Unset; - } + base_connection.mac_address = mac_address_from_dbus(wireless_config)?; } base_connection.ip_config = ip_config_from_dbus(&conn)?; @@ -341,6 +319,20 @@ fn base_connection_from_dbus(conn: &OwnedNestedHash) -> Option { Some(base_connection) } +fn mac_address_from_dbus(config: &HashMap) -> Option { + if let Some(mac_address) = config.get("assigned-mac-address") { + match MacAddress::from_str(mac_address.downcast_ref::()?) { + Ok(mac) => Some(mac), + Err(e) => { + log::warn!("Couldn't parse MAC: {}", e); + None + } + } + } else { + Some(MacAddress::Unset) + } +} + fn match_config_from_dbus( match_config: &HashMap, ) -> Option {