use crate::mac_address::MacAddress;
use crate::schema::port::Port as PortSchema;
use crate::Error;
use crate::NvBmc;
use crate::Resource;
use crate::ResourceSchema;
use nv_redfish_core::Bmc;
use nv_redfish_core::NavProperty;
use std::marker::PhantomData;
use std::sync::Arc;
#[cfg(feature = "oem-lenovo")]
use crate::oem::lenovo::port::LenovoPort;
pub struct Port<B: Bmc> {
data: Arc<PortSchema>,
_marker: PhantomData<B>,
}
impl<B: Bmc> Port<B> {
pub(crate) async fn new(
bmc: &NvBmc<B>,
nav: &NavProperty<PortSchema>,
) -> Result<Self, Error<B>> {
nav.get(bmc.as_ref())
.await
.map_err(Error::Bmc)
.map(|data| Self {
data,
_marker: PhantomData,
})
}
#[must_use]
pub fn raw(&self) -> Arc<PortSchema> {
self.data.clone()
}
#[must_use]
pub fn associated_mac_addresses(&self) -> Vec<MacAddress<'_>> {
self.data
.ethernet
.as_ref()
.and_then(Option::as_ref)
.and_then(|ethernet| ethernet.associated_mac_addresses.as_ref())
.and_then(Option::as_ref)
.map(|addresses| {
addresses
.iter()
.map(String::as_str)
.map(MacAddress::new)
.collect()
})
.unwrap_or_default()
}
#[cfg(feature = "oem-lenovo")]
pub fn oem_lenovo(&self) -> Result<Option<LenovoPort>, Error<B>> {
LenovoPort::new(&self.data).map_err(Error::Json)
}
}
impl<B: Bmc> Resource for Port<B> {
fn resource_ref(&self) -> &ResourceSchema {
&self.data.as_ref().base
}
}