use crate::computer_system::BootOptionReference;
use crate::schema::boot_option::BootOption as BootOptionSchema;
use crate::schema::boot_option_collection::BootOptionCollection as BootOptionCollectionSchema;
use crate::Error;
use crate::NvBmc;
use crate::Resource;
use crate::ResourceSchema;
use nv_redfish_core::Bmc;
use nv_redfish_core::NavProperty;
use std::convert::identity;
use std::marker::PhantomData;
use std::sync::Arc;
use tagged_types::TaggedType;
pub struct BootOptionCollection<B: Bmc> {
bmc: NvBmc<B>,
collection: Arc<BootOptionCollectionSchema>,
}
impl<B: Bmc> BootOptionCollection<B> {
pub(crate) async fn new(
bmc: &NvBmc<B>,
nav: &NavProperty<BootOptionCollectionSchema>,
) -> Result<Self, Error<B>> {
let collection = bmc.expand_property(nav).await?;
Ok(Self {
bmc: bmc.clone(),
collection,
})
}
pub async fn members(&self) -> Result<Vec<BootOption<B>>, Error<B>> {
let mut members = Vec::new();
for m in &self.collection.members {
members.push(BootOption::new(&self.bmc, m).await?);
}
Ok(members)
}
}
pub type UefiDevicePath<T> = TaggedType<T, UefiDevicePathTag>;
#[doc(hidden)]
#[derive(tagged_types::Tag)]
#[implement(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[transparent(Debug, Display, FromStr, Serialize, Deserialize)]
#[capability(inner_access, cloned)]
pub enum UefiDevicePathTag {}
pub type DisplayName<T> = TaggedType<T, DisplayNameTag>;
#[doc(hidden)]
#[derive(tagged_types::Tag)]
#[implement(Clone, Copy)]
#[transparent(Debug, Display, Serialize, Deserialize)]
#[capability(inner_access, cloned)]
pub enum DisplayNameTag {}
pub struct BootOption<B: Bmc> {
data: Arc<BootOptionSchema>,
_marker: PhantomData<B>,
}
impl<B: Bmc> BootOption<B> {
pub(crate) async fn new(
bmc: &NvBmc<B>,
nav: &NavProperty<BootOptionSchema>,
) -> 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<BootOptionSchema> {
self.data.clone()
}
#[must_use]
pub fn boot_reference(&self) -> BootOptionReference<&str> {
self.data.boot_option_reference.as_deref().map_or_else(
|| BootOptionReference::new(self.id().inner()),
BootOptionReference::new,
)
}
#[must_use]
pub fn enabled(&self) -> Option<bool> {
self.data.boot_option_enabled.and_then(identity)
}
#[must_use]
pub fn display_name(&self) -> Option<DisplayName<&str>> {
self.data
.display_name
.as_ref()
.and_then(Option::as_ref)
.map(String::as_str)
.map(DisplayName::new)
}
#[must_use]
pub fn uefi_device_path(&self) -> Option<UefiDevicePath<&str>> {
self.data
.uefi_device_path
.as_ref()
.and_then(Option::as_ref)
.map(String::as_str)
.map(UefiDevicePath::new)
}
}
impl<B: Bmc> Resource for BootOption<B> {
fn resource_ref(&self) -> &ResourceSchema {
&self.data.as_ref().base
}
}