dg_network_manager 1.0.0

Network Manager DBUS API
Documentation
use super::DeviceClient;
use std::io::{Error, ErrorKind};
use std::sync::Arc;
use zbus::proxy::CacheProperties;
use zbus::zvariant::OwnedObjectPath;
use zbus::{Connection, Result as ZResult, proxy};

#[proxy(
    interface = "org.freedesktop.NetworkManager.Device.Bond",
    default_service = "org.freedesktop.NetworkManager"
)]
pub trait Bond {
    // Properties
    #[zbus(property)]
    fn hw_address(&self) -> ZResult<String>;

    #[zbus(property)]
    fn carrier(&self) -> ZResult<bool>;

    #[zbus(property)]
    fn slaves(&self) -> ZResult<Vec<OwnedObjectPath>>;
}

#[derive(Debug, Clone)]
pub struct BondClient {
    device: DeviceClient,
    proxy: BondProxy<'static>,
}

impl BondClient {
    pub async fn new(connection: Arc<Connection>, service_path: String) -> Result<Self, Error> {
        let device = DeviceClient::new(connection.clone(), service_path.clone()).await?;

        let proxy = BondProxy::builder(&connection)
            .path(service_path)
            .map_err(|e| Error::new(ErrorKind::InvalidInput, e.to_string()))?
            .cache_properties(CacheProperties::Yes)
            .build()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?;

        // Verify we can access bond-specific properties
        proxy
            .hw_address()
            .await
            .map_err(|e| Error::new(ErrorKind::NotFound, e.to_string()))?;

        Ok(Self { device, proxy })
    }

    // Forward common device methods to the base device client
    pub fn service_path(&self) -> &str {
        self.device.service_path()
    }

    pub async fn interface(&self) -> Result<String, Error> {
        self.device.interface().await
    }

    pub async fn disconnect(&self) -> Result<(), Error> {
        self.device.disconnect().await
    }

    // Bond-specific methods
    pub async fn hw_address(&self) -> Result<String, Error> {
        self.proxy
            .hw_address()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn carrier(&self) -> Result<bool, Error> {
        self.proxy
            .carrier()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn slaves(&self) -> Result<Vec<OwnedObjectPath>, Error> {
        self.proxy
            .slaves()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    // A helper method that could convert slave paths to Device objects if needed
    pub async fn get_slave_devices(
        &self,
        connection: Arc<Connection>,
    ) -> Result<Vec<super::Device>, Error> {
        let slave_paths = self.slaves().await?;

        let mut devices = Vec::new();
        for path in slave_paths {
            match super::Device::new(connection.clone(), path.to_string()).await {
                Ok(device) => devices.push(device),
                Err(_) => continue,
            }
        }

        Ok(devices)
    }
}