diff --git a/rust/agama-dbus-server/src/network/dbus/interfaces.rs b/rust/agama-dbus-server/src/network/dbus/interfaces.rs index 9270281150..bb28d1202b 100644 --- a/rust/agama-dbus-server/src/network/dbus/interfaces.rs +++ b/rust/agama-dbus-server/src/network/dbus/interfaces.rs @@ -242,13 +242,13 @@ impl Connection { /// Custom mac-address #[dbus_interface(property)] pub async fn mac_address(&self) -> String { - self.get_connection().await.mac_address_string() + self.get_connection().await.base().mac_address_string() } #[dbus_interface(property)] pub async fn set_mac_address(&mut self, mac_address: &str) -> zbus::fdo::Result<()> { let mut connection = self.get_connection().await; - connection.set_mac_address(mac_address)?; + connection.base_mut().set_mac_address(mac_address)?; self.update_connection(connection).await } } diff --git a/rust/agama-dbus-server/src/network/model.rs b/rust/agama-dbus-server/src/network/model.rs index c8c721bab3..b83616a451 100644 --- a/rust/agama-dbus-server/src/network/model.rs +++ b/rust/agama-dbus-server/src/network/model.rs @@ -307,22 +307,6 @@ impl Connection { matches!(self, Connection::Loopback(_)) } - pub fn mac_address_string(&self) -> String { - match &self.base().mac_address { - Some(mac) => mac.to_string(), - None => "".to_string(), - } - } - - pub fn set_mac_address(&mut self, mac_address: &str) -> Result<(), InvalidMacAddress> { - self.base_mut().mac_address = if mac_address.is_empty() { - None - } else { - Some(MacAddress::from_str(mac_address)?) - }; - Ok(()) - } - pub fn is_ethernet(&self) -> bool { matches!(self, Connection::Loopback(_)) || matches!(self, Connection::Ethernet(_)) } @@ -339,6 +323,24 @@ pub struct BaseConnection { pub match_config: MatchConfig, } +impl BaseConnection { + pub fn mac_address_string(&self) -> String { + match &self.mac_address { + Some(mac) => mac.to_string(), + None => "".to_string(), + } + } + + pub fn set_mac_address(&mut self, mac_address: &str) -> Result<(), InvalidMacAddress> { + self.mac_address = if mac_address.is_empty() { + None + } else { + Some(MacAddress::from_str(mac_address)?) + }; + Ok(()) + } +} + impl PartialEq for BaseConnection { fn eq(&self, other: &Self) -> bool { self.id == other.id && self.uuid == other.uuid && self.ip_config == other.ip_config diff --git a/rust/agama-dbus-server/src/network/nm/dbus.rs b/rust/agama-dbus-server/src/network/nm/dbus.rs index 8cc7b385aa..7d756f7ba6 100644 --- a/rust/agama-dbus-server/src/network/nm/dbus.rs +++ b/rust/agama-dbus-server/src/network/nm/dbus.rs @@ -35,7 +35,7 @@ pub fn connection_to_dbus(conn: &Connection) -> NestedHash { if conn.is_ethernet() { let ethernet_config = HashMap::from([( "assigned-mac-address", - Value::new(conn.mac_address_string()), + Value::new(conn.base().mac_address_string()), )]); result.insert(ETHERNET_KEY, ethernet_config); } else if let Connection::Wireless(wireless) = conn { @@ -226,10 +226,7 @@ fn wireless_config_to_dbus(conn: &WirelessConnection) -> NestedHash { ("ssid", Value::new(config.ssid.to_vec())), ( "assigned-mac-address", - Value::new(match &conn.base.mac_address { - Some(mac) => mac.to_string(), - None => "".to_string(), - }), + Value::new(conn.base.mac_address_string()), ), ]); @@ -626,7 +623,7 @@ mod test { let match_config = connection.match_config(); assert_eq!(match_config.kernel, vec!["pci-0000:00:19.0"]); - assert_eq!(connection.mac_address_string(), "12:34:56:78:9A:BC"); + assert_eq!(connection.base().mac_address_string(), "12:34:56:78:9A:BC"); assert_eq!( ip_config.addresses, @@ -870,7 +867,7 @@ mod test { let mut updated = Connection::Ethernet(EthernetConnection::default()); updated.set_interface(""); - assert!(updated.set_mac_address("").is_ok()); + assert!(updated.base_mut().set_mac_address("").is_ok()); let updated = connection_to_dbus(&updated); let merged = merge_dbus_connections(&original, &updated);