use crate::bmc_quirks::BmcQuirks;
use crate::protocol_features::ExpandQueryFeatures;
use crate::ProtocolFeatures;
use nv_redfish_core::Bmc;
use std::sync::Arc;
#[cfg(feature = "nv-bmc-expand")]
use crate::Error;
#[cfg(feature = "nv-bmc-expand")]
use nv_redfish_core::query::ExpandQuery;
#[cfg(feature = "nv-bmc-expand")]
use nv_redfish_core::Expandable;
#[cfg(feature = "nv-bmc-expand")]
use nv_redfish_core::NavProperty;
pub struct NvBmc<B: Bmc> {
bmc: Arc<B>,
protocol_features: Arc<ProtocolFeatures>,
pub(crate) quirks: Arc<BmcQuirks>,
}
impl<B: Bmc> NvBmc<B> {
pub(crate) fn new(bmc: Arc<B>, protocol_features: ProtocolFeatures, quirks: BmcQuirks) -> Self {
Self {
bmc,
protocol_features: protocol_features.into(),
quirks: quirks.into(),
}
}
pub(crate) fn replace_bmc(self, bmc: Arc<B>) -> Self {
Self {
bmc,
protocol_features: self.protocol_features,
quirks: self.quirks,
}
}
pub(crate) fn restrict_expand(self) -> Self {
Self {
bmc: self.bmc,
protocol_features: ProtocolFeatures {
expand: ExpandQueryFeatures {
expand_all: false,
no_links: false,
},
}
.into(),
quirks: self.quirks,
}
}
#[allow(dead_code)] pub fn as_ref(&self) -> &B {
self.bmc.as_ref()
}
#[cfg(feature = "nv-bmc-expand")]
pub async fn expand_property<T>(&self, nav: &NavProperty<T>) -> Result<Arc<T>, Error<B>>
where
T: Expandable,
{
let optimal_query = if self.protocol_features.expand.no_links {
Some(ExpandQuery::no_links())
} else if self.protocol_features.expand.expand_all {
Some(ExpandQuery::all())
} else {
None
};
if let Some(optimal_query) = optimal_query {
nav.expand(self.bmc.as_ref(), optimal_query)
.await
.map_err(Error::Bmc)?
.get(self.bmc.as_ref())
.await
.map_err(Error::Bmc)
} else {
nav.get(self.bmc.as_ref()).await.map_err(Error::Bmc)
}
}
}
impl<B: Bmc> Clone for NvBmc<B> {
fn clone(&self) -> Self {
Self {
bmc: self.bmc.clone(),
protocol_features: self.protocol_features.clone(),
quirks: self.quirks.clone(),
}
}
}