smbioslib/structs/
defined_struct.rs

1//! [DefinedStruct] and [DefinedStructTable] perform downcast operations
2//! via into() and into_iter() trait functions for [UndefinedStruct].
3
4use serde::Serialize;
5use std::iter::FromIterator;
6
7use crate::core::UndefinedStruct;
8
9use super::*;
10
11/// # SMBIOS Standard Defined Structure
12///
13/// Represents one of the SMBIOS defined structures or, in the case
14/// of an OEM defined structure, as a generically defined Unknown variant
15#[derive(Serialize, Debug)]
16pub enum DefinedStruct<'a> {
17    /// BIOS Information (Type 0)
18    Information(SMBiosInformation<'a>),
19    /// System Information (Type 1)
20    SystemInformation(SMBiosSystemInformation<'a>),
21    /// Baseboard (or Module) Information (Type 2)
22    BaseBoardInformation(SMBiosBaseboardInformation<'a>),
23    /// System Enclosure or Chassis (Type 3)
24    SystemChassisInformation(SMBiosSystemChassisInformation<'a>),
25    /// Processor Information (Type 4)
26    ProcessorInformation(SMBiosProcessorInformation<'a>),
27    /// Memory Controller Information (Type 5, Obsolete)
28    MemoryControllerInformation(SMBiosMemoryControllerInformation<'a>),
29    /// Memory Module Information (Type 6, Obsolete)
30    MemoryModuleInformation(SMBiosMemoryModuleInformation<'a>),
31    /// Cache Informaiton (Type 7)
32    CacheInformation(SMBiosCacheInformation<'a>),
33    /// Port Connector Information (Type 8)
34    PortConnectorInformation(SMBiosPortConnectorInformation<'a>),
35    /// System Slot Information (Type 9)
36    SystemSlot(SMBiosSystemSlot<'a>),
37    /// On Board Devices Information (Type 10, Obsolete)
38    OnBoardDeviceInformation(SMBiosOnBoardDeviceInformation<'a>),
39    /// OEM Strings (Type 11)
40    OemStrings(SMBiosOemStrings<'a>),
41    /// System Configuration Options (Type 12)
42    SystemConfigurationOptions(SMBiosSystemConfigurationOptions<'a>),
43    /// BIOS Language Information (Type 13)
44    LanguageInformation(SMBiosBiosLanguageInformation<'a>),
45    /// Group Associations (Type 14)
46    GroupAssociations(SMBiosGroupAssociations<'a>),
47    /// System Event Log (Type 15)
48    EventLog(SMBiosSystemEventLog<'a>),
49    /// Physical Memory Array (Type 16)
50    PhysicalMemoryArray(SMBiosPhysicalMemoryArray<'a>),
51    /// Memory Device (Type 17)
52    MemoryDevice(SMBiosMemoryDevice<'a>),
53    /// 32-Bit Memory Error Information (Type 18)
54    MemoryErrorInformation32Bit(SMBiosMemoryErrorInformation32<'a>),
55    /// Memory Array Mapped Address (Type 19)
56    MemoryArrayMappedAddress(SMBiosMemoryArrayMappedAddress<'a>),
57    /// Memory Device Mapped Address (Type 20)
58    MemoryDeviceMappedAddress(SMBiosMemoryDeviceMappedAddress<'a>),
59    /// Built-in Pointing Device (Type 21)
60    BuiltInPointingDevice(SMBiosBuiltInPointingDevice<'a>),
61    /// Portable Battery (Type 22)
62    PortableBattery(SMBiosPortableBattery<'a>),
63    /// System Reset (Type 23)
64    SystemReset(SMBiosSystemReset<'a>),
65    /// Hardware Security (Type 24)
66    HardwareSecurity(SMBiosHardwareSecurity<'a>),
67    /// System Power Controls (Type 25)
68    SystemPowerControls(SMBiosSystemPowerControls<'a>),
69    /// Voltage Probe (Type 26)
70    VoltageProbe(SMBiosVoltageProbe<'a>),
71    /// Cooling Device (Type 27)
72    CoolingDevice(SMBiosCoolingDevice<'a>),
73    /// Temperature Probe (Type 28)
74    TemperatureProbe(SMBiosTemperatureProbe<'a>),
75    /// Electrical Current Probe (Type 29)
76    ElectricalCurrentProbe(SMBiosElectricalCurrentProbe<'a>),
77    /// Out-of-Band Remote Access (Type 30)
78    OutOfBandRemoteAccess(SMBiosOutOfBandRemoteAccess<'a>),
79    /// Boot Integrity Services (BIS) (Type 31)
80    BisEntryPoint(SMBiosBisEntryPoint<'a>),
81    /// System Boot Information (Type 32)
82    SystemBootInformation(SMBiosSystemBootInformation<'a>),
83    /// 64-Bit Memory Error Information (Type 33)
84    MemoryErrorInformation64Bit(SMBiosMemoryErrorInformation64<'a>),
85    /// Management Device (Type 34)
86    ManagementDevice(SMBiosManagementDevice<'a>),
87    /// Management Device Component (Type 35)
88    ManagementDeviceComponent(SMBiosManagementDeviceComponent<'a>),
89    /// Management Device Threshold Data (Type 36)
90    ManagementDeviceThresholdData(SMBiosManagementDeviceThresholdData<'a>),
91    /// Memory Channel (Type 37)
92    MemoryChannel(SMBiosMemoryChannel<'a>),
93    /// IPMI Device Information (Type 38)
94    IpmiDeviceInformation(SMBiosIpmiDeviceInformation<'a>),
95    /// Power Supply (Type 39)
96    SystemPowerSupply(SMBiosSystemPowerSupply<'a>),
97    /// Additional Information (Type 40)
98    AdditionalInformation(SMBiosAdditionalInformation<'a>),
99    /// Onboard Devices Extended Information (Type 41)
100    OnboardDevicesExtendedInformation(SMBiosOnboardDevicesExtendedInformation<'a>),
101    /// Management Controller Host Interface (Type 42)
102    ManagementControllerHostInterface(SMBiosManagementControllerHostInterface<'a>),
103    /// TPM Device (Type 43)
104    TpmDevice(SMBiosTpmDevice<'a>),
105    /// Processor Additional Information (Type 44)
106    ProcessorAdditionalInformation(SMBiosProcessorAdditionalInformation<'a>),
107    /// Firmware Inventory Information (Type 45)
108    FirmwareInventoryInformation(SMBiosFirmwareInventoryInformation<'a>),
109    /// String Property (Type 46)
110    StringProperty(SMBiosStringProperty<'a>),
111    /// Inactive (Type 126)
112    Inactive(SMBiosInactive<'a>),
113    /// End-of-Table (Type 127)
114    EndOfTable(SMBiosEndOfTable<'a>),
115    /// OEM-Defined or Unknown Structure
116    ///
117    /// - A structure with a type value not yet defined, such as by a DMTF specification
118    /// that supercedes the types known by this library
119    /// - An OEM type with a value > 127.
120    Undefined(SMBiosUnknown<'a>),
121}
122
123impl<'a> From<&'a UndefinedStruct> for DefinedStruct<'a> {
124    fn from(undefined_struct: &'a UndefinedStruct) -> Self {
125        match undefined_struct.header.struct_type() {
126            SMBiosInformation::STRUCT_TYPE => {
127                DefinedStruct::Information(SMBiosInformation::new(undefined_struct))
128            }
129            SMBiosSystemInformation::STRUCT_TYPE => {
130                DefinedStruct::SystemInformation(SMBiosSystemInformation::new(undefined_struct))
131            }
132            SMBiosBaseboardInformation::STRUCT_TYPE => DefinedStruct::BaseBoardInformation(
133                SMBiosBaseboardInformation::new(undefined_struct),
134            ),
135            SMBiosSystemChassisInformation::STRUCT_TYPE => DefinedStruct::SystemChassisInformation(
136                SMBiosSystemChassisInformation::new(undefined_struct),
137            ),
138            SMBiosProcessorInformation::STRUCT_TYPE => DefinedStruct::ProcessorInformation(
139                SMBiosProcessorInformation::new(undefined_struct),
140            ),
141            SMBiosMemoryControllerInformation::STRUCT_TYPE => {
142                DefinedStruct::MemoryControllerInformation(SMBiosMemoryControllerInformation::new(
143                    undefined_struct,
144                ))
145            }
146            SMBiosMemoryModuleInformation::STRUCT_TYPE => DefinedStruct::MemoryModuleInformation(
147                SMBiosMemoryModuleInformation::new(undefined_struct),
148            ),
149            SMBiosCacheInformation::STRUCT_TYPE => {
150                DefinedStruct::CacheInformation(SMBiosCacheInformation::new(undefined_struct))
151            }
152            SMBiosPortConnectorInformation::STRUCT_TYPE => DefinedStruct::PortConnectorInformation(
153                SMBiosPortConnectorInformation::new(undefined_struct),
154            ),
155            SMBiosSystemSlot::STRUCT_TYPE => {
156                DefinedStruct::SystemSlot(SMBiosSystemSlot::new(undefined_struct))
157            }
158            SMBiosOnBoardDeviceInformation::STRUCT_TYPE => DefinedStruct::OnBoardDeviceInformation(
159                SMBiosOnBoardDeviceInformation::new(undefined_struct),
160            ),
161            SMBiosOemStrings::STRUCT_TYPE => {
162                DefinedStruct::OemStrings(SMBiosOemStrings::new(undefined_struct))
163            }
164            SMBiosSystemConfigurationOptions::STRUCT_TYPE => {
165                DefinedStruct::SystemConfigurationOptions(SMBiosSystemConfigurationOptions::new(
166                    undefined_struct,
167                ))
168            }
169            SMBiosBiosLanguageInformation::STRUCT_TYPE => DefinedStruct::LanguageInformation(
170                SMBiosBiosLanguageInformation::new(undefined_struct),
171            ),
172            SMBiosGroupAssociations::STRUCT_TYPE => {
173                DefinedStruct::GroupAssociations(SMBiosGroupAssociations::new(undefined_struct))
174            }
175            SMBiosSystemEventLog::STRUCT_TYPE => {
176                DefinedStruct::EventLog(SMBiosSystemEventLog::new(undefined_struct))
177            }
178            SMBiosPhysicalMemoryArray::STRUCT_TYPE => {
179                DefinedStruct::PhysicalMemoryArray(SMBiosPhysicalMemoryArray::new(undefined_struct))
180            }
181            SMBiosMemoryDevice::STRUCT_TYPE => {
182                DefinedStruct::MemoryDevice(SMBiosMemoryDevice::new(undefined_struct))
183            }
184            SMBiosMemoryErrorInformation32::STRUCT_TYPE => {
185                DefinedStruct::MemoryErrorInformation32Bit(SMBiosMemoryErrorInformation32::new(
186                    undefined_struct,
187                ))
188            }
189            SMBiosMemoryArrayMappedAddress::STRUCT_TYPE => DefinedStruct::MemoryArrayMappedAddress(
190                SMBiosMemoryArrayMappedAddress::new(undefined_struct),
191            ),
192            SMBiosMemoryDeviceMappedAddress::STRUCT_TYPE => {
193                DefinedStruct::MemoryDeviceMappedAddress(SMBiosMemoryDeviceMappedAddress::new(
194                    undefined_struct,
195                ))
196            }
197            SMBiosBuiltInPointingDevice::STRUCT_TYPE => DefinedStruct::BuiltInPointingDevice(
198                SMBiosBuiltInPointingDevice::new(undefined_struct),
199            ),
200            SMBiosPortableBattery::STRUCT_TYPE => {
201                DefinedStruct::PortableBattery(SMBiosPortableBattery::new(undefined_struct))
202            }
203            SMBiosSystemReset::STRUCT_TYPE => {
204                DefinedStruct::SystemReset(SMBiosSystemReset::new(undefined_struct))
205            }
206            SMBiosHardwareSecurity::STRUCT_TYPE => {
207                DefinedStruct::HardwareSecurity(SMBiosHardwareSecurity::new(undefined_struct))
208            }
209            SMBiosSystemPowerControls::STRUCT_TYPE => {
210                DefinedStruct::SystemPowerControls(SMBiosSystemPowerControls::new(undefined_struct))
211            }
212            SMBiosVoltageProbe::STRUCT_TYPE => {
213                DefinedStruct::VoltageProbe(SMBiosVoltageProbe::new(undefined_struct))
214            }
215            SMBiosCoolingDevice::STRUCT_TYPE => {
216                DefinedStruct::CoolingDevice(SMBiosCoolingDevice::new(undefined_struct))
217            }
218            SMBiosTemperatureProbe::STRUCT_TYPE => {
219                DefinedStruct::TemperatureProbe(SMBiosTemperatureProbe::new(undefined_struct))
220            }
221            SMBiosElectricalCurrentProbe::STRUCT_TYPE => DefinedStruct::ElectricalCurrentProbe(
222                SMBiosElectricalCurrentProbe::new(undefined_struct),
223            ),
224            SMBiosOutOfBandRemoteAccess::STRUCT_TYPE => DefinedStruct::OutOfBandRemoteAccess(
225                SMBiosOutOfBandRemoteAccess::new(undefined_struct),
226            ),
227            SMBiosBisEntryPoint::STRUCT_TYPE => {
228                DefinedStruct::BisEntryPoint(SMBiosBisEntryPoint::new(undefined_struct))
229            }
230            SMBiosSystemBootInformation::STRUCT_TYPE => DefinedStruct::SystemBootInformation(
231                SMBiosSystemBootInformation::new(undefined_struct),
232            ),
233            SMBiosMemoryErrorInformation64::STRUCT_TYPE => {
234                DefinedStruct::MemoryErrorInformation64Bit(SMBiosMemoryErrorInformation64::new(
235                    undefined_struct,
236                ))
237            }
238            SMBiosManagementDevice::STRUCT_TYPE => {
239                DefinedStruct::ManagementDevice(SMBiosManagementDevice::new(undefined_struct))
240            }
241            SMBiosManagementDeviceComponent::STRUCT_TYPE => {
242                DefinedStruct::ManagementDeviceComponent(SMBiosManagementDeviceComponent::new(
243                    undefined_struct,
244                ))
245            }
246            SMBiosManagementDeviceThresholdData::STRUCT_TYPE => {
247                DefinedStruct::ManagementDeviceThresholdData(
248                    SMBiosManagementDeviceThresholdData::new(undefined_struct),
249                )
250            }
251            SMBiosMemoryChannel::STRUCT_TYPE => {
252                DefinedStruct::MemoryChannel(SMBiosMemoryChannel::new(undefined_struct))
253            }
254            SMBiosIpmiDeviceInformation::STRUCT_TYPE => DefinedStruct::IpmiDeviceInformation(
255                SMBiosIpmiDeviceInformation::new(undefined_struct),
256            ),
257            SMBiosSystemPowerSupply::STRUCT_TYPE => {
258                DefinedStruct::SystemPowerSupply(SMBiosSystemPowerSupply::new(undefined_struct))
259            }
260            SMBiosAdditionalInformation::STRUCT_TYPE => DefinedStruct::AdditionalInformation(
261                SMBiosAdditionalInformation::new(undefined_struct),
262            ),
263            SMBiosOnboardDevicesExtendedInformation::STRUCT_TYPE => {
264                DefinedStruct::OnboardDevicesExtendedInformation(
265                    SMBiosOnboardDevicesExtendedInformation::new(undefined_struct),
266                )
267            }
268            SMBiosManagementControllerHostInterface::STRUCT_TYPE => {
269                DefinedStruct::ManagementControllerHostInterface(
270                    SMBiosManagementControllerHostInterface::new(undefined_struct),
271                )
272            }
273            SMBiosTpmDevice::STRUCT_TYPE => {
274                DefinedStruct::TpmDevice(SMBiosTpmDevice::new(undefined_struct))
275            }
276            SMBiosProcessorAdditionalInformation::STRUCT_TYPE => {
277                DefinedStruct::ProcessorAdditionalInformation(
278                    SMBiosProcessorAdditionalInformation::new(undefined_struct),
279                )
280            }
281            SMBiosFirmwareInventoryInformation::STRUCT_TYPE => {
282                DefinedStruct::FirmwareInventoryInformation(
283                    SMBiosFirmwareInventoryInformation::new(undefined_struct),
284                )
285            }
286            SMBiosStringProperty::STRUCT_TYPE => {
287                DefinedStruct::StringProperty(SMBiosStringProperty::new(undefined_struct))
288            }
289            SMBiosInactive::STRUCT_TYPE => {
290                DefinedStruct::Inactive(SMBiosInactive::new(undefined_struct))
291            }
292            SMBiosEndOfTable::STRUCT_TYPE => {
293                DefinedStruct::EndOfTable(SMBiosEndOfTable::new(undefined_struct))
294            }
295            _ => DefinedStruct::Undefined(SMBiosUnknown::new(undefined_struct)),
296        }
297    }
298}
299
300/// # Defined Struct Table
301///
302/// Contains a list of [DefinedStruct] items.
303#[derive(Serialize, Debug)]
304pub struct DefinedStructTable<'a>(Vec<DefinedStruct<'a>>);
305
306impl<'a> DefinedStructTable<'a> {
307    fn new() -> DefinedStructTable<'a> {
308        DefinedStructTable(Vec::new())
309    }
310
311    fn add(&mut self, elem: DefinedStruct<'a>) {
312        self.0.push(elem);
313    }
314}
315
316impl<'a> IntoIterator for DefinedStructTable<'a> {
317    type Item = DefinedStruct<'a>;
318    type IntoIter = std::vec::IntoIter<Self::Item>;
319
320    fn into_iter(self) -> Self::IntoIter {
321        self.0.into_iter()
322    }
323}
324
325impl<'a> FromIterator<&'a UndefinedStruct> for DefinedStructTable<'a> {
326    fn from_iter<I: IntoIterator<Item = &'a UndefinedStruct>>(iter: I) -> Self {
327        let mut defined_struct_table = DefinedStructTable::new();
328
329        for undefined_struct in iter {
330            defined_struct_table.add(undefined_struct.into());
331        }
332
333        defined_struct_table
334    }
335}