dg_network_manager 1.0.0

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

pub type RouteInfo = (Vec<u8>, u32, Vec<u8>, u32);

#[proxy(
    interface = "org.freedesktop.NetworkManager.IP6Config",
    default_service = "org.freedesktop.NetworkManager"
)]
pub trait Ipv6 {
    #[zbus(property)]
    fn addresses(&self) -> ZResult<AddressArray>;

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

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

    #[zbus(property)]
    fn routes(&self) -> ZResult<RouteArray>;

    #[zbus(property)]
    fn route_data(&self) -> ZResult<RouteData>;

    #[zbus(property)]
    fn nameservers(&self) -> ZResult<Vec<Vec<u8>>>;

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

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

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

    #[zbus(property)]
    fn dns_priority(&self) -> ZResult<i32>;
}

pub struct Ipv6Client {
    service_path: String,
    proxy: Ipv6Proxy<'static>,
}

impl Ipv6Client {
    pub async fn new(connection: &Connection, service_path: String) -> Result<Self, Error> {
        let proxy = Ipv6Proxy::builder(connection)
            .path(service_path.clone())
            .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()))?;

        proxy
            .addresses()
            .await
            .map_err(|e| Error::new(ErrorKind::NotFound, e.to_string()))?;

        Ok(Self {
            service_path,
            proxy,
        })
    }

    pub fn service_path(&self) -> &str {
        &self.service_path
    }

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

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

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

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

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

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

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

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

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

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