dg_network_manager 1.0.0

Network Manager DBUS API
Documentation
use crate::dbus_api::DnsConfiguration;
use std::collections::HashMap;
use std::io::{Error, ErrorKind};
use zbus::proxy::CacheProperties;
use zbus::zvariant::OwnedValue;
use zbus::{Connection, Result as ZResult, proxy};

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

    #[zbus(property)]
    fn rc_manager(&self) -> ZResult<String>;

    #[zbus(property)]
    fn configuration(&self) -> ZResult<Vec<HashMap<String, OwnedValue>>>;
}

pub struct DnsManagerClient {
    proxy: DnsManagerProxy<'static>,
}

impl DnsManagerClient {
    pub async fn new(connection: &Connection) -> Result<Self, Error> {
        let proxy = DnsManagerProxy::builder(connection)
            .cache_properties(CacheProperties::Yes)
            .build()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?;

        Ok(Self { proxy })
    }

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

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

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