dbus-settings-bindings/networkmanager/src/config/ip4.rs

63 lines
1.4 KiB
Rust
Raw Normal View History

2022-01-11 16:02:29 -05:00
// SPDX-License-Identifier: MPL-2.0
use crate::interface::config::ip4::Ipv4ConfigProxy;
use std::{net::Ipv4Addr, ops::Deref, str::FromStr};
2022-01-11 16:02:29 -05:00
use zbus::Result;
use zvariant::DeserializeDict;
2022-01-11 16:02:29 -05:00
2022-01-21 11:12:35 -05:00
#[derive(Debug)]
2022-01-11 16:02:29 -05:00
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()
2022-02-08 14:10:28 -05:00
.map(|addresses| {
addresses
.into_iter()
.map(|addr| addr.swap_bytes())
.map(Ipv4Addr::from)
.collect()
})
2022-01-11 16:02:29 -05:00
.collect())
}
pub async fn address_data(&self) -> Result<Vec<AddressData>> {
Ok(self
.0
.address_data()
.await?
.into_iter()
.filter_map(|mut map| {
let address = {
let address_str = map.remove("address")?;
let address_str = address_str.downcast_ref::<zvariant::Str>()?;
Ipv4Addr::from_str(address_str).ok()?
};
let prefix = u64::try_from(map.remove("prefix")?).ok()? as usize;
Some(AddressData { address, prefix })
})
.collect())
}
2022-01-11 16:02:29 -05:00
}
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)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct AddressData {
pub address: Ipv4Addr,
pub prefix: usize,
}