dg_network_manager 1.0.0

Network Manager DBUS API
Documentation
use std::io::{Error, ErrorKind};
use zbus::proxy::CacheProperties;
use zbus::{Connection, Result as ZResult, proxy};

#[proxy(
    interface = "org.freedesktop.NetworkManager.VPN.Connection",
    default_service = "org.freedesktop.NetworkManager"
)]
pub trait VPNConnection {
    #[zbus(property)]
    fn vpn_state(&self) -> ZResult<u32>;

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

pub struct VPNConnectionClient {
    service_path: String,
    proxy: VPNConnectionProxy<'static>,
}

impl VPNConnectionClient {
    pub async fn new(connection: &Connection, service_path: String) -> Result<Self, Error> {
        let proxy = VPNConnectionProxy::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()))?;

        // Verify we can access the VPN connection
        proxy
            .vpn_state()
            .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 vpn_state(&self) -> Result<u32, Error> {
        self.proxy
            .vpn_state()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

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