use std::collections::HashMap;
use std::convert::TryFrom;
use log::error;
use super::NmError;
pub(crate) fn own_value_to_bytes_array(
value: zvariant::OwnedValue,
) -> Result<Vec<u8>, NmError> {
Ok(zvariant::Array::try_from(value)?
.iter()
.filter_map(|val| {
if let zvariant::Value::U8(i) = val {
return Some(i);
}
None
})
.copied()
.collect())
}
pub(crate) fn u8_array_to_mac_string(data: Vec<u8>) -> String {
data.iter()
.map(|byte| format!("{byte:02X}"))
.collect::<Vec<_>>()
.join(":")
}
pub(crate) fn mac_str_to_u8_array(mac: &str) -> Vec<u8> {
let result = mac
.split(':')
.map(|byte| u8::from_str_radix(byte, 16))
.collect::<Result<Vec<_>, _>>();
match result {
Ok(arr) => arr,
Err(e) => {
error!(
"Failed to convert to MAC address to bytes {:?}: {}",
mac, e
);
Vec::new()
}
}
}
pub(crate) trait ToDbusValue {
fn to_value(&self) -> Result<HashMap<&str, zvariant::Value>, NmError>;
}