use crate::schema::bios::Bios as BiosSchema;
use crate::Error;
use crate::NvBmc;
use nv_redfish_core::Bmc;
use nv_redfish_core::EdmPrimitiveType;
use nv_redfish_core::NavProperty;
use std::marker::PhantomData;
use std::sync::Arc;
pub struct Bios<B: Bmc> {
data: Arc<BiosSchema>,
_marker: PhantomData<B>,
}
impl<B: Bmc> Bios<B> {
pub(crate) async fn new(
bmc: &NvBmc<B>,
nav: &NavProperty<BiosSchema>,
) -> Result<Self, Error<B>> {
nav.get(bmc.as_ref())
.await
.map_err(crate::Error::Bmc)
.map(|data| Self {
data,
_marker: PhantomData,
})
}
#[must_use]
pub fn raw(&self) -> Arc<BiosSchema> {
self.data.clone()
}
#[must_use]
pub fn attribute<'a>(&'a self, name: &str) -> Option<BiosAttributeRef<'a>> {
self.data
.attributes
.as_ref()
.and_then(|attributes| attributes.dynamic_properties.get(name))
.map(|v| BiosAttributeRef::new(v.as_ref()))
}
}
pub struct BiosAttributeRef<'a> {
value: Option<&'a EdmPrimitiveType>,
}
impl<'a> BiosAttributeRef<'a> {
const fn new(value: Option<&'a EdmPrimitiveType>) -> Self {
Self { value }
}
#[must_use]
pub const fn is_null(&self) -> bool {
self.value.is_none()
}
#[must_use]
pub const fn str_value(&self) -> Option<&str> {
match self.value {
Some(EdmPrimitiveType::String(v)) => Some(v.as_str()),
_ => None,
}
}
#[must_use]
pub const fn bool_value(&self) -> Option<bool> {
match self.value {
Some(EdmPrimitiveType::Bool(v)) => Some(*v),
_ => None,
}
}
#[must_use]
pub const fn integer_value(&self) -> Option<i64> {
match self.value {
Some(EdmPrimitiveType::Integer(v)) => Some(*v),
_ => None,
}
}
#[must_use]
pub const fn decimal_value(&self) -> Option<f64> {
match self.value {
Some(EdmPrimitiveType::Decimal(v)) => Some(*v),
_ => None,
}
}
}