1use crate::hardware_id::HardwareIdRef;
20use crate::hardware_id::Manufacturer as HardwareIdManufacturer;
21use crate::hardware_id::Model as HardwareIdModel;
22use crate::hardware_id::PartNumber as HardwareIdPartNumber;
23use crate::hardware_id::SerialNumber as HardwareIdSerialNumber;
24use crate::schema::redfish::assembly::Assembly as AssemblySchema;
25use crate::schema::redfish::assembly::AssemblyData as AssemblyDataSchema;
26use crate::Error;
27use crate::NvBmc;
28use crate::Resource;
29use crate::ResourceSchema;
30use nv_redfish_core::Bmc;
31use nv_redfish_core::NavProperty;
32use std::marker::PhantomData;
33use std::sync::Arc;
34
35#[doc(hidden)]
36pub enum AssemblyTag {}
37
38pub type Manufacturer<T> = HardwareIdManufacturer<T, AssemblyTag>;
40
41pub type Model<T> = HardwareIdModel<T, AssemblyTag>;
43
44pub type PartNumber<T> = HardwareIdPartNumber<T, AssemblyTag>;
46
47pub type SerialNumber<T> = HardwareIdSerialNumber<T, AssemblyTag>;
49
50pub struct Assembly<B: Bmc> {
54 bmc: NvBmc<B>,
55 data: Arc<AssemblySchema>,
56}
57
58impl<B: Bmc> Assembly<B> {
59 pub(crate) async fn new(
61 bmc: &NvBmc<B>,
62 nav: &NavProperty<AssemblySchema>,
63 ) -> Result<Self, Error<B>> {
64 bmc.expand_property(nav).await.map(|data| Self {
68 bmc: bmc.clone(),
69 data,
70 })
71 }
72
73 #[must_use]
75 pub fn raw(&self) -> Arc<AssemblySchema> {
76 self.data.clone()
77 }
78
79 pub async fn assemblies(&self) -> Result<Vec<AssemblyData<B>>, Error<B>> {
86 let mut result = Vec::new();
87 if let Some(assemblies) = &self.data.assemblies {
88 for m in assemblies {
89 result.push(AssemblyData::new(&self.bmc, m).await?);
90 }
91 }
92 Ok(result)
93 }
94}
95
96impl<B: Bmc> Resource for Assembly<B> {
97 fn resource_ref(&self) -> &ResourceSchema {
98 &self.data.as_ref().base
99 }
100}
101
102pub struct AssemblyData<B: Bmc> {
104 data: Arc<AssemblyDataSchema>,
105 _marker: PhantomData<B>,
106}
107
108impl<B: Bmc> AssemblyData<B> {
109 pub(crate) async fn new(
111 bmc: &NvBmc<B>,
112 nav: &NavProperty<AssemblyDataSchema>,
113 ) -> Result<Self, Error<B>> {
114 nav.get(bmc.as_ref())
115 .await
116 .map_err(crate::Error::Bmc)
117 .map(|data| Self {
118 data,
119 _marker: PhantomData,
120 })
121 }
122
123 #[must_use]
125 pub fn raw(&self) -> Arc<AssemblyDataSchema> {
126 self.data.clone()
127 }
128
129 #[must_use]
131 pub fn hardware_id(&self) -> HardwareIdRef<'_, AssemblyTag> {
132 HardwareIdRef {
133 manufacturer: self
134 .data
135 .producer
136 .as_ref()
137 .and_then(Option::as_deref)
138 .map(Manufacturer::new),
139 model: self
140 .data
141 .model
142 .as_ref()
143 .and_then(Option::as_deref)
144 .map(Model::new),
145 part_number: self
146 .data
147 .part_number
148 .as_ref()
149 .and_then(Option::as_deref)
150 .map(PartNumber::new),
151 serial_number: self
152 .data
153 .serial_number
154 .as_ref()
155 .and_then(Option::as_deref)
156 .map(SerialNumber::new),
157 }
158 }
159}