1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
use crate::core::UndefinedStruct;
use crate::{BoardTypeData, SMBiosStruct, SMBiosType};
use serde::{ser::SerializeSeq, ser::SerializeStruct, Serialize, Serializer};
use std::fmt;
use std::ops::Deref;

/// # System Enclosure or Chassis (Type 3)
///
/// The information in this structure (see Table 16) defines attributes of the system’s mechanical
/// enclosure(s). For example, if a system included a separate enclosure for its peripheral devices, two
/// structures would be returned: one for the main system enclosure and the second for the peripheral device
/// enclosure. The additions to this structure in version 2.1 of this specification support the population of the
/// CIM_Chassis class.
///
/// Compliant with:
/// DMTF SMBIOS Reference Specification 3.4.0 (DSP0134)
/// Document Date: 2020-07-17
pub struct SMBiosSystemChassisInformation<'a> {
    parts: &'a UndefinedStruct,
}

impl<'a> SMBiosStruct<'a> for SMBiosSystemChassisInformation<'a> {
    const STRUCT_TYPE: u8 = 3u8;

    fn new(parts: &'a UndefinedStruct) -> Self {
        Self { parts }
    }

    fn parts(&self) -> &'a UndefinedStruct {
        self.parts
    }
}

impl<'a> SMBiosSystemChassisInformation<'a> {
    const CONTAINED_ELEMENTS_OFFSET: usize = 0x15usize;

    /// Manufacturer
    pub fn manufacturer(&self) -> Option<String> {
        self.parts.get_field_string(0x04)
    }

    /// Chassis type
    ///
    /// Bit 7 Chassis lock is present if 1.
    /// Otherwise, either a lock is not present or it is
    /// unknown if the enclosure has a lock.
    /// Bits 6:0 Enumeration value.
    pub fn chassis_type(&self) -> Option<ChassisTypeData> {
        self.parts
            .get_field_byte(0x05)
            .map(|raw| ChassisTypeData::from(raw))
    }

    /// Version
    pub fn version(&self) -> Option<String> {
        self.parts.get_field_string(0x06)
    }

    /// Serial number
    pub fn serial_number(&self) -> Option<String> {
        self.parts.get_field_string(0x07)
    }

    /// Asset tag number
    pub fn asset_tag_number(&self) -> Option<String> {
        self.parts.get_field_string(0x08)
    }

    /// Boot-up State
    ///
    /// State of the enclosure when it was last booted.
    pub fn bootup_state(&self) -> Option<ChassisStateData> {
        self.parts
            .get_field_byte(0x09)
            .map(|raw| ChassisStateData::from(raw))
    }

    /// Power supply state
    ///
    /// State of the enclosure’s power supply (or
    /// supplies) when last booted
    pub fn power_supply_state(&self) -> Option<ChassisStateData> {
        self.parts
            .get_field_byte(0x0A)
            .map(|raw| ChassisStateData::from(raw))
    }

    /// Thermal state
    ///
    /// Thermal state of the enclosure when last
    /// booted.
    pub fn thermal_state(&self) -> Option<ChassisStateData> {
        self.parts
            .get_field_byte(0x0B)
            .map(|raw| ChassisStateData::from(raw))
    }

    /// Security status
    ///
    /// Physical security status of the enclosure when
    /// last booted.
    pub fn security_status(&self) -> Option<ChassisSecurityStatusData> {
        self.parts
            .get_field_byte(0x0C)
            .map(|raw| ChassisSecurityStatusData::from(raw))
    }

    /// OEM-defined
    ///
    /// OEM- or BIOS vendor-specific information
    pub fn oem_defined(&self) -> Option<u32> {
        self.parts.get_field_dword(0x0D)
    }

    /// Height
    ///
    /// Height of the enclosure, in 'U's
    ///
    /// A U is a standard unit of measure for the
    /// height of a rack or rack-mountable component
    /// and is equal to 1.75 inches or 4.445 cm.
    pub fn height(&self) -> Option<ChassisHeight> {
        self.parts
            .get_field_byte(0x11)
            .map(|raw| ChassisHeight::from(raw))
    }

    /// Number of power cords
    ///
    /// Number of power cords associated with the
    /// enclosure or chassis
    pub fn number_of_power_cords(&self) -> Option<PowerCords> {
        self.parts
            .get_field_byte(0x12)
            .map(|raw| PowerCords::from(raw))
    }

    /// Contained element count (n)
    ///
    /// Number of Contained Element records that
    /// follow, in the range 0 to 255
    /// Each Contained Element group comprises m
    /// bytes, as specified by the Contained Element
    /// Record Length field that follows. If no
    /// Contained Elements are included, this field is
    /// set to 0.
    pub fn contained_element_count(&self) -> Option<u8> {
        self.parts.get_field_byte(0x13)
    }

    /// Contained element record length (m)
    ///
    /// Byte length of each Contained Element record
    /// that follows, in the range 0 to 255
    /// If no Contained Elements are included, this
    /// field is set to 0. For version 2.3.2 and later of
    /// this specification, this field is set to at least 03h
    /// when Contained Elements are specified.
    pub fn contained_element_record_length(&self) -> Option<u8> {
        self.parts.get_field_byte(0x14)
    }

    fn contained_elements_size(&self) -> Option<usize> {
        self.contained_element_record_length().and_then(|m| {
            self.contained_element_count()
                .and_then(|n| Some(m as usize * n as usize))
        })
    }

    /// Contained Elements
    pub fn contained_elements(&self) -> Option<ContainedElements<'_>> {
        ContainedElements::new(self)
    }

    /// SKU number
    ///
    /// Chassis or enclosure SKU number
    pub fn sku_number(&self) -> Option<String> {
        self.contained_elements_size().and_then(|size| {
            self.parts
                .get_field_string(Self::CONTAINED_ELEMENTS_OFFSET + size)
        })
    }
}

impl fmt::Debug for SMBiosSystemChassisInformation<'_> {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct(std::any::type_name::<SMBiosSystemChassisInformation<'_>>())
            .field("header", &self.parts.header)
            .field("manufacturer", &self.manufacturer())
            .field("chassis_type", &self.chassis_type())
            .field("version", &self.version())
            .field("serial_number", &self.serial_number())
            .field("asset_tag_number", &self.asset_tag_number())
            .field("bootup_state", &self.bootup_state())
            .field("power_supply_state", &self.power_supply_state())
            .field("thermal_state", &self.thermal_state())
            .field("security_status", &self.security_status())
            .field("oem_defined", &self.oem_defined())
            .field("height", &self.height())
            .field("number_of_power_cords", &self.number_of_power_cords())
            .field("contained_element_count", &self.contained_element_count())
            .field(
                "contained_element_record_length",
                &self.contained_element_record_length(),
            )
            .field("contained_elements", &self.contained_elements())
            .field("sku_number", &self.sku_number())
            .finish()
    }
}

impl Serialize for SMBiosSystemChassisInformation<'_> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("SMBiosSystemChassisInformation", 17)?;
        state.serialize_field("header", &self.parts.header)?;
        state.serialize_field("manufacturer", &self.manufacturer())?;
        state.serialize_field("chassis_type", &self.chassis_type())?;
        state.serialize_field("version", &self.version())?;
        state.serialize_field("serial_number", &self.serial_number())?;
        state.serialize_field("asset_tag_number", &self.asset_tag_number())?;
        state.serialize_field("bootup_state", &self.bootup_state())?;
        state.serialize_field("power_supply_state", &self.power_supply_state())?;
        state.serialize_field("thermal_state", &self.thermal_state())?;
        state.serialize_field("security_status", &self.security_status())?;
        state.serialize_field("oem_defined", &self.oem_defined())?;
        state.serialize_field("height", &self.height())?;
        state.serialize_field("number_of_power_cords", &self.number_of_power_cords())?;
        state.serialize_field("contained_element_count", &self.contained_element_count())?;
        state.serialize_field(
            "contained_element_record_length",
            &self.contained_element_record_length(),
        )?;
        state.serialize_field("contained_elements", &self.contained_elements())?;
        state.serialize_field("sku_number", &self.sku_number())?;
        state.end()
    }
}

/// # Chassis Height
#[derive(Serialize, Debug)]
pub enum ChassisHeight {
    /// A chassis enclosure height is not specified.
    Unspecified,
    /// Height of the enclosure, in 'U's
    ///
    /// A U is a standard unit of measure for the height of a rack
    /// or rack-mountable component and is equal to 1.75 inches or
    /// 4.445 cm.
    U(u8),
}

impl From<u8> for ChassisHeight {
    fn from(raw: u8) -> Self {
        match raw {
            0 => ChassisHeight::Unspecified,
            _ => ChassisHeight::U(raw),
        }
    }
}

/// # Number of Power Cords
#[derive(Serialize, Debug)]
pub enum PowerCords {
    /// The number of power cords is not specified.
    Unspecified,
    /// The number of power cords
    Count(u8),
}

impl From<u8> for PowerCords {
    fn from(raw: u8) -> Self {
        match raw {
            0 => PowerCords::Unspecified,
            _ => PowerCords::Count(raw),
        }
    }
}

/// # Chassis Type Data
pub struct ChassisTypeData {
    /// Raw value
    ///
    /// _raw_ is most useful when _value_ is None.
    /// This is most likely to occur when the standard was updated but
    /// this library code has not been updated to match the current
    /// standard.
    pub raw: u8,
    /// The contained [ChassisType] value
    pub value: ChassisType,
}

impl fmt::Debug for ChassisTypeData {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct(std::any::type_name::<ChassisTypeData>())
            .field("raw", &self.raw)
            .field("value", &self.value)
            .finish()
    }
}

impl Serialize for ChassisTypeData {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("ChassisTypeData", 2)?;
        state.serialize_field("raw", &self.raw)?;
        state.serialize_field("value", &self.value)?;
        state.end()
    }
}

impl fmt::Display for ChassisTypeData {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.value {
            ChassisType::None => write!(f, "{}", &self.raw),
            _ => write!(f, "{:?}", &self.value),
        }
    }
}

impl Deref for ChassisTypeData {
    type Target = ChassisType;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

/// # Chassis Type
#[derive(Serialize, Debug, PartialEq, Eq)]
pub enum ChassisType {
    /// Other
    Other,
    /// Unknown
    Unknown,
    /// Desktop
    Desktop,
    /// Low Profile Desktop
    LowProfileDesktop,
    /// Pizza Box
    PizzaBox,
    /// Mini Tower
    MiniTower,
    /// Tower
    Tower,
    /// Portable
    Portable,
    /// Laptop
    Laptop,
    /// Notebook
    Notebook,
    /// Hand Held
    HandHeld,
    /// Docking Station
    DockingStation,
    /// All in One
    AllInOne,
    /// Sub Notebook
    SubNotebook,
    /// Space-saving
    SpaceSaving,
    /// Lunch Box
    LunchBox,
    /// Main Server Chassis
    MainServerChassis,
    /// Expansion Chassis
    ExpansionChassis,
    /// SubChassis
    SubChassis,
    /// Bus Expansion Chassis
    BusExpansionChassis,
    /// Peripheral Chassis
    PeripheralChassis,
    /// RAID Chassis
    RaidChassis,
    /// Rack Mount Chassis
    RackMountChassis,
    /// Sealed-case PC
    SealedCasePC,
    /// Multi-system chassis
    MultiSystemChassis,
    /// Compact PCI
    CompactPci,
    /// Advanced TCA
    AdvancedTca,
    /// Blade
    Blade,
    /// Blade Encloser
    BladeEnclosure,
    /// Tablet
    Tablet,
    /// Convertible
    Convertible,
    /// Detachable
    Detachable,
    /// IoT Gateway
    IoTGateway,
    /// Embedded PC
    EmbeddedPC,
    /// Mini PC
    MiniPC,
    /// Stick PC
    StickPC,
    /// A value unknown to this standard, check the raw value
    None,
}

impl From<u8> for ChassisTypeData {
    fn from(raw: u8) -> Self {
        ChassisTypeData {
            value: match raw {
                0x01 => ChassisType::Other,
                0x02 => ChassisType::Unknown,
                0x03 => ChassisType::Desktop,
                0x04 => ChassisType::LowProfileDesktop,
                0x05 => ChassisType::PizzaBox,
                0x06 => ChassisType::MiniTower,
                0x07 => ChassisType::Tower,
                0x08 => ChassisType::Portable,
                0x09 => ChassisType::Laptop,
                0x0A => ChassisType::Notebook,
                0x0B => ChassisType::HandHeld,
                0x0C => ChassisType::DockingStation,
                0x0D => ChassisType::AllInOne,
                0x0E => ChassisType::SubNotebook,
                0x0F => ChassisType::SpaceSaving,
                0x10 => ChassisType::LunchBox,
                0x11 => ChassisType::MainServerChassis,
                0x12 => ChassisType::ExpansionChassis,
                0x13 => ChassisType::SubChassis,
                0x14 => ChassisType::BusExpansionChassis,
                0x15 => ChassisType::PeripheralChassis,
                0x16 => ChassisType::RaidChassis,
                0x17 => ChassisType::RackMountChassis,
                0x18 => ChassisType::SealedCasePC,
                0x19 => ChassisType::MultiSystemChassis,
                0x1A => ChassisType::CompactPci,
                0x1B => ChassisType::AdvancedTca,
                0x1C => ChassisType::Blade,
                0x1D => ChassisType::BladeEnclosure,
                0x1E => ChassisType::Tablet,
                0x1F => ChassisType::Convertible,
                0x20 => ChassisType::Detachable,
                0x21 => ChassisType::IoTGateway,
                0x22 => ChassisType::EmbeddedPC,
                0x23 => ChassisType::MiniPC,
                0x24 => ChassisType::StickPC,
                _ => ChassisType::None,
            },
            raw,
        }
    }
}

/// # Chassis State Data
pub struct ChassisStateData {
    /// Raw value
    ///
    /// _raw_ is most useful when _value_ is None.
    /// This is most likely to occur when the standard was updated but
    /// this library code has not been updated to match the current
    /// standard.
    raw: u8,
    /// The contained [ChassisState] value
    value: ChassisState,
}

impl fmt::Debug for ChassisStateData {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct(std::any::type_name::<ChassisStateData>())
            .field("raw", &self.raw)
            .field("value", &self.value)
            .finish()
    }
}

impl Serialize for ChassisStateData {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("ChassisStateData", 2)?;
        state.serialize_field("raw", &self.raw)?;
        state.serialize_field("value", &self.value)?;
        state.end()
    }
}

impl Deref for ChassisStateData {
    type Target = ChassisState;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

/// # Chassis Statue
#[derive(Serialize, Debug, PartialEq, Eq)]
pub enum ChassisState {
    /// Other
    Other,
    /// Unknown
    Unknown,
    /// Safe
    Safe,
    /// Warning
    Warning,
    /// Critical
    Critical,
    /// Non-recoverable
    NonRecoverable,
    /// A value unknown to this standard, check the raw value
    None,
}

impl From<u8> for ChassisStateData {
    fn from(raw: u8) -> Self {
        ChassisStateData {
            value: match raw {
                0x01 => ChassisState::Other,
                0x02 => ChassisState::Unknown,
                0x03 => ChassisState::Safe,
                0x04 => ChassisState::Warning,
                0x05 => ChassisState::Critical,
                0x06 => ChassisState::NonRecoverable,
                _ => ChassisState::None,
            },
            raw,
        }
    }
}

/// # Chassis Security Status Data
pub struct ChassisSecurityStatusData {
    /// Raw value
    ///
    /// _raw_ is most useful when _value_ is None.
    /// This is most likely to occur when the standard was updated but
    /// this library code has not been updated to match the current
    /// standard.
    raw: u8,
    /// The contained [ChassisSecurityStatus] value
    value: ChassisSecurityStatus,
}

impl fmt::Debug for ChassisSecurityStatusData {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct(std::any::type_name::<ChassisSecurityStatusData>())
            .field("raw", &self.raw)
            .field("value", &self.value)
            .finish()
    }
}

impl Serialize for ChassisSecurityStatusData {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("ChassisSecurityStatusData", 2)?;
        state.serialize_field("raw", &self.raw)?;
        state.serialize_field("value", &self.value)?;
        state.end()
    }
}

impl Deref for ChassisSecurityStatusData {
    type Target = ChassisSecurityStatus;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

/// # Chassis Security Status
#[derive(Serialize, Debug, PartialEq, Eq)]
pub enum ChassisSecurityStatus {
    /// Other
    Other,
    /// Unknown
    Unknown,
    /// None
    StatusNone,
    /// External interface locked out
    ExternalInterfaceLockedOut,
    /// External interface enabled
    ExternalInterfaceEnabled,
    /// A value unknown to this standard, check the raw value
    None,
}

impl From<u8> for ChassisSecurityStatusData {
    fn from(raw: u8) -> Self {
        ChassisSecurityStatusData {
            value: match raw {
                0x01 => ChassisSecurityStatus::Other,
                0x02 => ChassisSecurityStatus::Unknown,
                0x03 => ChassisSecurityStatus::StatusNone,
                0x04 => ChassisSecurityStatus::ExternalInterfaceLockedOut,
                0x05 => ChassisSecurityStatus::ExternalInterfaceEnabled,
                _ => ChassisSecurityStatus::None,
            },
            raw,
        }
    }
}

/// # Contained Elements
pub struct ContainedElements<'a> {
    raw: &'a [u8],
    record_count: usize,
    record_length: usize,
}

impl<'a> ContainedElements<'a> {
    fn new(chassis_information: &'a SMBiosSystemChassisInformation<'a>) -> Option<Self> {
        chassis_information
            .contained_element_record_length()
            .and_then(|record_length| {
                chassis_information
                    .contained_element_count()
                    .and_then(|record_count| {
                        chassis_information
                            .parts()
                            .get_field_data(
                                SMBiosSystemChassisInformation::CONTAINED_ELEMENTS_OFFSET,
                                SMBiosSystemChassisInformation::CONTAINED_ELEMENTS_OFFSET
                                    + (record_length as usize * record_count as usize),
                            )
                            .and_then(|raw| {
                                Some(Self {
                                    raw,
                                    record_count: record_count as usize,
                                    record_length: record_length as usize,
                                })
                            })
                    })
            })
    }
}

impl<'a> fmt::Debug for ContainedElements<'a> {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct(std::any::type_name::<ContainedElements<'_>>())
            .field("records", &self.into_iter())
            .finish()
    }
}

impl<'a> Serialize for ContainedElements<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let elements: Vec<ChassisElement<'_>> = self.into_iter().collect();
        let mut seq = serializer.serialize_seq(Some(elements.len()))?;
        for e in elements {
            seq.serialize_element(&e)?;
        }
        seq.end()
    }
}

/// # Contained Chassis Element
pub struct ChassisElement<'a> {
    /// Raw byte slice for this chassis element
    pub raw: &'a [u8],
}

impl<'a> ChassisElement<'a> {
    const MINIMUM_RAW_SIZE: usize = 3usize;
    const ELEMENT_TYPE_OFFSET: usize = 0usize;
    const ELEMENT_MINIMUM_OFFSET: usize = 1usize;
    const ELEMENT_MAXIMUM_OFFSET: usize = 2usize;

    fn new(raw: &'a [u8]) -> Option<Self> {
        if raw.len() < Self::MINIMUM_RAW_SIZE {
            None
        } else {
            Some(Self { raw })
        }
    }

    /// Contained Element Type
    pub fn element_type(&self) -> ElementType {
        ElementType::from(self.raw[Self::ELEMENT_TYPE_OFFSET])
    }

    /// Contained Element Minimum
    pub fn element_minimum(&self) -> ElementMinimum {
        ElementMinimum::from(self.raw[Self::ELEMENT_MINIMUM_OFFSET])
    }

    /// Contained Element Maximum
    pub fn element_maximum(&self) -> ElementMaximum {
        ElementMaximum::from(self.raw[Self::ELEMENT_MAXIMUM_OFFSET])
    }
}

impl fmt::Debug for ChassisElement<'_> {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct(std::any::type_name::<ChassisElement<'_>>())
            .field("raw", &self.raw)
            .field("element_type", &self.element_type())
            .field("element_minimum", &self.element_minimum())
            .field("element_maximum", &self.element_maximum())
            .finish()
    }
}

impl Serialize for ChassisElement<'_> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("ChassisElement", 4)?;
        state.serialize_field("raw", &self.raw)?;
        state.serialize_field("element_type", &self.element_type())?;
        state.serialize_field("element_minimum", &self.element_minimum())?;
        state.serialize_field("element_maximum", &self.element_maximum())?;
        state.end()
    }
}

/// # Contained Element Type
#[derive(Serialize, Debug)]
pub enum ElementType {
    /// SMBIOS Baseboard Type enumeration
    BaseboardType(BoardTypeData),
    /// SMBIOS structure type enumeration
    SMBiosType(SMBiosType),
}

impl From<u8> for ElementType {
    fn from(raw: u8) -> Self {
        if raw & 0b1000_0000 == 0b1000_0000 {
            ElementType::SMBiosType(SMBiosType(raw & 0b0111_1111))
        } else {
            ElementType::BaseboardType(BoardTypeData::from(raw))
        }
    }
}

/// # Contained Element Minimum
///
/// Specifies the minimum number of the 'element_type' that can be
/// installed in the chassis for the chassis to properly operate,
/// in the range 0 to 254.
#[derive(Serialize, Debug)]
pub enum ElementMinimum {
    /// Specifies the minimum number of the 'element_type' that can be
    /// installed in the chassis for the chassis to properly operate,
    ///  in the range 0 to 254.
    Count(u8),
    /// The value 255 (0FFh) is reserved for future definition by this specification.
    Reserved,
}

impl From<u8> for ElementMinimum {
    fn from(raw: u8) -> Self {
        match raw {
            0xFF => ElementMinimum::Reserved,
            _ => ElementMinimum::Count(raw),
        }
    }
}

/// # Contained Element Maximum
///
/// Specifies the minimum number of the 'element_type' that can be
/// installed in the chassis in the range 0 to 254.
#[derive(Serialize, Debug)]
pub enum ElementMaximum {
    /// Specifies the maximum number of the 'element_type' that can be
    /// installed in the chassis for the chassis to properly operate,
    ///  in the range 1 to 255.
    Count(u8),
    /// The value 0 is reserved for future definition by this specification.
    Reserved,
}

impl From<u8> for ElementMaximum {
    fn from(raw: u8) -> Self {
        match raw {
            0x00 => ElementMaximum::Reserved,
            _ => ElementMaximum::Count(raw),
        }
    }
}

/// # Iterates over the [ChassisElement] entries within [ContainedElements]
pub struct ContainedElementsIterator<'a> {
    contained_elements: &'a ContainedElements<'a>,
    current_index: usize,
    current_entry: usize,
}

impl<'a> ContainedElementsIterator<'a> {
    fn reset(&mut self) {
        self.current_index = 0;
        self.current_entry = 0;
    }
}

impl<'a> IntoIterator for &'a ContainedElements<'a> {
    type Item = ChassisElement<'a>;
    type IntoIter = ContainedElementsIterator<'a>;

    fn into_iter(self) -> Self::IntoIter {
        ContainedElementsIterator {
            contained_elements: self,
            current_index: 0,
            current_entry: 0,
        }
    }
}

impl<'a> Iterator for ContainedElementsIterator<'a> {
    type Item = ChassisElement<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current_entry == self.contained_elements.record_count {
            self.reset();
            return None;
        }

        let next_index = self.current_index + self.contained_elements.record_length;
        match ChassisElement::new(&self.contained_elements.raw[self.current_index..next_index]) {
            Some(chassis_element) => {
                self.current_index = next_index;
                self.current_entry += 1;
                Some(chassis_element)
            }
            None => {
                self.reset();
                None
            }
        }
    }
}

impl<'a> fmt::Debug for ContainedElementsIterator<'a> {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_list()
            .entries(self.contained_elements.into_iter())
            .finish()
    }
}

impl<'a> Serialize for ContainedElementsIterator<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let elements: Vec<ChassisElement<'_>> = self.contained_elements.into_iter().collect();
        let mut seq = serializer.serialize_seq(Some(elements.len()))?;
        for e in elements {
            seq.serialize_element(&e)?;
        }
        seq.end()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::structs::types::{
        BoardType, ChassisHeight, ChassisSecurityStatus, ChassisState, ChassisType, ElementType,
        PowerCords, SMBiosSystemChassisInformation,
    };

    #[test]
    fn unit_test() {
        let struct_type3 = vec![
            0x03, 0x1C, 0x03, 0x00, 0x01, 0x03, 0x02, 0x03, 0x04, 0x03, 0x03, 0x03, 0x03, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x85, 0x00, 0x02, 0x05, 0x00, 0x02, 0x05,
            b'L', b'E', b'N', b'O', b'V', b'O', 0x00, b'N', b'o', b'n', b'e', 0x00, b'M', b'J',
            b'0', b'6', b'U', b'R', b'D', b'Z', 0x00, b'4', b'0', b'8', b'9', b'9', b'8', b'5',
            0x00, b'D', b'e', b'f', b'a', b'u', b'l', b't', b' ', b's', b't', b'r', b'i', b'n',
            b'g', 0x00, 0x00,
        ];

        let parts = UndefinedStruct::new(&struct_type3);
        let test_struct = SMBiosSystemChassisInformation::new(&parts);

        assert_eq!(test_struct.manufacturer(), Some("LENOVO".to_string()));
        assert_eq!(*test_struct.chassis_type().unwrap(), ChassisType::Desktop);
        assert_eq!(test_struct.version(), Some("None".to_string()));
        assert_eq!(test_struct.serial_number(), Some("MJ06URDZ".to_string()));
        assert_eq!(test_struct.asset_tag_number(), Some("4089985".to_string()));
        assert_eq!(*test_struct.bootup_state().unwrap(), ChassisState::Safe);
        assert_eq!(
            *test_struct.power_supply_state().unwrap(),
            ChassisState::Safe
        );
        assert_eq!(*test_struct.thermal_state().unwrap(), ChassisState::Safe);
        assert_eq!(
            *test_struct.security_status().unwrap(),
            ChassisSecurityStatus::StatusNone
        );
        assert_eq!(test_struct.oem_defined(), Some(0));
        match test_struct.height().unwrap() {
            ChassisHeight::U(_) => panic!("expected no height specified"),
            ChassisHeight::Unspecified => (),
        }
        match test_struct.number_of_power_cords().unwrap() {
            PowerCords::Count(count) => assert_eq!(count, 1),
            PowerCords::Unspecified => panic!("expected a count"),
        }
        assert_eq!(test_struct.contained_element_count(), Some(2));
        assert_eq!(test_struct.contained_element_record_length(), Some(3));
        let contained_elements = test_struct.contained_elements().unwrap();
        let mut iterator = contained_elements.into_iter();
        let first = iterator.next().unwrap();
        match first.element_type() {
            ElementType::SMBiosType(bios_type) => {
                assert_eq!(*bios_type, 5)
            }
            _ => panic!("expected SMBIOS type"),
        }
        let second = iterator.next().unwrap();
        match second.element_type() {
            ElementType::BaseboardType(baseboard_type) => {
                assert_eq!(*baseboard_type, BoardType::SystemManagementModule)
            }
            _ => panic!("expected baseboard type"),
        }
        assert_eq!(test_struct.sku_number(), Some("Default string".to_string()));
    }
}