Work on more Rust-side bindings

This commit is contained in:
Lucy 2022-01-11 16:02:29 -05:00
parent 91a7fc6da1
commit da5c6ed172
No known key found for this signature in database
GPG key ID: EBC517FAD666BBF1
7 changed files with 116 additions and 2 deletions

View file

@ -6,3 +6,5 @@ license = "MPL-2.0"
[dependencies]
zbus = "2.0.0"
[features]

View file

@ -0,0 +1,4 @@
// SPDX-License-Identifier: MPL-2.0
pub mod ip4;
pub mod ip6;

View file

@ -0,0 +1,30 @@
// SPDX-License-Identifier: MPL-2.0
use crate::interface::config::ip4::Ipv4ConfigProxy;
use std::{net::Ipv4Addr, ops::Deref};
use zbus::Result;
pub struct Ipv4Config<'a>(Ipv4ConfigProxy<'a>);
impl<'a> Ipv4Config<'a> {
pub async fn addresses(&self) -> Result<Vec<Vec<Ipv4Addr>>> {
let addresses = self.0.addresses().await?;
Ok(addresses
.into_iter()
.map(|addresses| addresses.into_iter().map(Ipv4Addr::from).collect())
.collect())
}
}
impl<'a> Deref for Ipv4Config<'a> {
type Target = Ipv4ConfigProxy<'a>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a> From<Ipv4ConfigProxy<'a>> for Ipv4Config<'a> {
fn from(config: Ipv4ConfigProxy<'a>) -> Self {
Ipv4Config(config)
}
}

View file

@ -0,0 +1,35 @@
// SPDX-License-Identifier: MPL-2.0
use crate::interface::config::ip6::Ipv6ConfigProxy;
use std::{net::Ipv6Addr, ops::Deref};
use zbus::Result;
pub struct Ipv6Config<'a>(Ipv6ConfigProxy<'a>);
impl<'a> Ipv6Config<'a> {
pub async fn addresses(&self) -> Result<Vec<Ipv6Addr>> {
let addresses = self.0.addresses().await?;
Ok(addresses
.into_iter()
.map(|(address, _, _)| {
let address_bytes: [u8; 16] = address
.try_into()
.expect("NetworkManager gave invalid IPv6 addresss");
Ipv6Addr::from(address_bytes)
})
.collect())
}
}
impl<'a> Deref for Ipv6Config<'a> {
type Target = Ipv6ConfigProxy<'a>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a> From<Ipv6ConfigProxy<'a>> for Ipv6Config<'a> {
fn from(config: Ipv6ConfigProxy<'a>) -> Self {
Ipv6Config(config)
}
}

View file

@ -4,11 +4,40 @@ pub mod bluetooth;
pub mod wired;
pub mod wireless;
use crate::interface::device::DeviceProxy;
use std::ops::Deref;
use crate::{
config::{ip4::Ipv4Config, ip6::Ipv6Config},
interface::{
config::{ip4::Ipv4ConfigProxy, ip6::Ipv6ConfigProxy},
device::DeviceProxy,
},
};
use std::{net::Ipv4Addr, ops::Deref};
use zbus::Result;
pub struct Device<'a>(DeviceProxy<'a>);
impl<'a> Device<'a> {
pub async fn ip4_address(&self) -> Result<Ipv4Addr> {
self.0.ip4_address().await.map(Ipv4Addr::from)
}
pub async fn ip4_config(&self) -> Result<Ipv4Config<'a>> {
let config = Ipv4ConfigProxy::builder(self.0.connection())
.path(self.0.ip4_config().await?)?
.build()
.await?;
Ok(Ipv4Config::from(config))
}
pub async fn ip6_config(&self) -> Result<Ipv6Config<'a>> {
let config = Ipv6ConfigProxy::builder(self.0.connection())
.path(self.0.ip6_config().await?)?
.build()
.await?;
Ok(Ipv6Config::from(config))
}
}
impl<'a> Deref for Device<'a> {
type Target = DeviceProxy<'a>;

View file

@ -1,5 +1,6 @@
// SPDX-License-Identifier: MPL-2.0
pub mod config;
pub mod device;
pub mod interface;
pub mod nm;

View file

@ -25,4 +25,17 @@ impl<'a> NetworkManager<'a> {
}
Ok(out)
}
pub async fn all_devices(&self) -> Result<Vec<Device<'a>>> {
let devices = self.0.get_all_devices().await?;
let mut out = Vec::with_capacity(devices.len());
for device in devices {
let device = DeviceProxy::builder(self.0.connection())
.path(device)?
.build()
.await?;
out.push(device.into());
}
Ok(out)
}
}