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
use super::MalformedStructureError;
use core::convert::TryInto;
use core::fmt;

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum ErrorGranularity {
    Other,
    Unknown,
    DeviceLevel,
    MemoryLevel,
    Undefined(u8),
}

impl From<u8> for ErrorGranularity {
    fn from(_type: u8) -> ErrorGranularity {
        match _type {
            1 => ErrorGranularity::Other,
            2 => ErrorGranularity::Unknown,
            3 => ErrorGranularity::DeviceLevel,
            4 => ErrorGranularity::MemoryLevel,
            t => ErrorGranularity::Undefined(t),
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum ErrorOperation {
    Other,
    Unknown,
    Read,
    Write,
    PartialWrite,
    Undefined(u8),
}

impl From<u8> for ErrorOperation {
    fn from(_type: u8) -> ErrorOperation {
        match _type {
            1 => ErrorOperation::Other,
            2 => ErrorOperation::Unknown,
            3 => ErrorOperation::Read,
            4 => ErrorOperation::Write,
            5 => ErrorOperation::PartialWrite,
            t => ErrorOperation::Undefined(t),
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum ErrorType {
    Other,
    Unknown,
    Ok,
    BadRead,
    Parity,
    SingleBit,
    DoubleBit,
    MultiBit,
    Nibble,
    Checksum,
    Crc,
    CorrectedSingleBit,
    Corrected,
    Uncorrectable,
    Undefined(u8),
}

impl From<u8> for ErrorType {
    fn from(_type: u8) -> ErrorType {
        match _type {
            1 => ErrorType::Other,
            2 => ErrorType::Unknown,
            3 => ErrorType::Ok,
            4 => ErrorType::BadRead,
            5 => ErrorType::Parity,
            6 => ErrorType::SingleBit,
            7 => ErrorType::DoubleBit,
            8 => ErrorType::MultiBit,
            9 => ErrorType::Nibble,
            10 => ErrorType::Checksum,
            11 => ErrorType::Crc,
            12 => ErrorType::CorrectedSingleBit,
            13 => ErrorType::Corrected,
            14 => ErrorType::Uncorrectable,
            t => ErrorType::Undefined(t),
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum FormFactor {
    Other,
    Unknown,
    Simm,
    Sip,
    Chip,
    Dip,
    Zip,
    ProprietaryCard,
    Dimm,
    Tsop,
    RowOfChips,
    Rimm,
    SoDimm,
    Srimm,
    FbDimm,
    Undefined(u8),
}

impl Default for FormFactor {
    fn default() -> Self {
        FormFactor::Unknown
    }
}

impl From<u8> for FormFactor {
    fn from(_type: u8) -> FormFactor {
        match _type {
            0 => FormFactor::Other,
            1 => FormFactor::Unknown,
            2 => FormFactor::Simm,
            3 => FormFactor::Sip,
            4 => FormFactor::Chip,
            5 => FormFactor::Dip,
            6 => FormFactor::Zip,
            7 => FormFactor::ProprietaryCard,
            8 => FormFactor::Dimm,
            9 => FormFactor::Dimm,
            10 => FormFactor::Tsop,
            11 => FormFactor::RowOfChips,
            12 => FormFactor::Rimm,
            13 => FormFactor::SoDimm,
            14 => FormFactor::Srimm,
            15 => FormFactor::FbDimm,
            t => FormFactor::Undefined(t),
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum MemoryTechnology {
    Other,
    Unknown,
    Dram,
    NvDimmN,
    NvDimmF,
    NvDimmP,
    IntelOptane,
    Undefined(u8),
}

impl Default for MemoryTechnology {
    fn default() -> Self {
        MemoryTechnology::Unknown
    }
}

impl From<u8> for MemoryTechnology {
    fn from(_type: u8) -> MemoryTechnology {
        match _type {
            1 => MemoryTechnology::Other,
            2 => MemoryTechnology::Unknown,
            3 => MemoryTechnology::Dram,
            4 => MemoryTechnology::NvDimmN,
            5 => MemoryTechnology::NvDimmF,
            6 => MemoryTechnology::NvDimmP,
            7 => MemoryTechnology::IntelOptane,
            t => MemoryTechnology::Undefined(t),
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum Type {
    Other,
    Unknown,
    Dram,
    Edram,
    Vram,
    Sram,
    Ram,
    Rom,
    Flash,
    Eeprom,
    Feprom,
    Eprom,
    Cdram,
    ThreeDram,
    Sdram,
    Sgram,
    Rdram,
    Ddr,
    Ddr2,
    Ddr2FbDimm,
    Reserved,
    Ddr3,
    Fbd2,
    Ddr4,
    LpDdr,
    LpDdr2,
    LpDdr3,
    Undefined(u8),
}

impl Default for Type {
    fn default() -> Self {
        Type::Unknown
    }
}

impl From<u8> for Type {
    fn from(_type: u8) -> Type {
        match _type {
            1 => Type::Other,
            2 => Type::Unknown,
            3 => Type::Dram,
            4 => Type::Edram,
            5 => Type::Vram,
            6 => Type::Sram,
            7 => Type::Ram,
            8 => Type::Rom,
            9 => Type::Flash,
            10 => Type::Eeprom,
            11 => Type::Feprom,
            12 => Type::Eprom,
            13 => Type::Cdram,
            14 => Type::ThreeDram,
            15 => Type::Sdram,
            16 => Type::Sgram,
            17 => Type::Rdram,
            18 => Type::Ddr,
            19 => Type::Ddr2,
            20 => Type::Ddr2FbDimm,
            21 => Type::Reserved,
            22 => Type::Reserved,
            23 => Type::Reserved,
            24 => Type::Ddr3,
            25 => Type::Fbd2,
            26 => Type::Ddr4,
            27 => Type::LpDdr,
            28 => Type::LpDdr2,
            29 => Type::LpDdr3,
            t => Type::Undefined(t),
        }
    }
}

bitflags! {
    /// The memory device details
    pub struct Detail: u16 {
        const RESERVED =      0b0000000000000000;
        const OTHER =         0b0000000000000010;
        const UNKNOWN =       0b0000000000000100;
        const FAST_PAGED =    0b0000000000001000;
        const STATIC_COLUMN = 0b0000000000010000;
        const PSEUDO_STATIC = 0b0000000000100000;
        const RAMBUS =        0b0000000001000000;
        const SYNCHRONOUS =   0b0000000010000000;
        const CMOS =          0b0000000100000000;
        const EDO =           0b0000001000000000;
        const WINDOW_DRAM =   0b0000010000000000;
        const CACHE_DRAM =    0b0000100000000000;
        const NON_VOLATILE =  0b0001000000000000;
        const REGISTERED =    0b0010000000000000;
        const UNREGISTERED =  0b0100000000000000;
        const LRDIMM =        0b1000000000000000;
    }
}

impl Default for Detail {
    fn default() -> Self {
        Detail::UNKNOWN
    }
}

bitflags! {
    pub struct OperatingModes: u16 {
        const RESERVED =                    0b0000000000000000;
        const OTHER =                       0b0000000000000010;
        const UNKNOWN =                     0b0000000000000100;
        const VOLATILE =                    0b0000000000001000;
        const BYTE_ACCESSIBLE_PERSISTENT =  0b0000000000010000;
        const BLOCK_ACCESSIBLE_PERSISTENT = 0b0000000000100000;
    }
}

#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct MemoryDevice<'buffer> {
    pub handle: u16,
    pub physical_memory_handle: u16,
    pub memory_error_handle: Option<u16>,
    /// Total width, in bits, of this memory device, including any check
    /// or error-correction bits. If there are no error-correction bits,
    /// this value should be equal to Data Width
    pub total_width: Option<u16>,
    /// Data width, in bits, of this memory device. A Data Width of 0 and a
    /// Total Width of 8 indicates that the device is being used solely to
    /// provide 8 error-correction bits
    pub data_width: Option<u16>,
    /// Size of the memory device. If the size is 32GB-1MB or greater, the
    /// field value is 7FFFh and the actual size is stored in the extended_size
    /// field.
    pub size: Option<u16>,
    pub form_factor: FormFactor,
    /// Identifies when the Memory Device is one of a set of Memory Devices
    /// that must be populated with all devices of the same type and size,
    /// and the set to which this device belongs
    pub device_set: Option<u8>,
    /// Identifies the physically-labeled socket or board position
    /// where the memory device is located
    pub device_locator: &'buffer str,
    /// Identifies the physically labeled bank where the memory device is located
    pub bank_locator: &'buffer str,
    pub memory_type: Type,
    pub type_detail: Detail,
    /// Identifies the maximum capable speed of the device, in megatransfers
    /// per second (MT/s)
    pub speed: Option<u16>,
    pub manufacturer: &'buffer str,
    pub serial: &'buffer str,
    pub asset_tag: &'buffer str,
    pub part_number: &'buffer str,
    pub attributes: u8,
    /// Extended size of the memory device (complements the Size field)
    pub extended_size: u32,
    /// Identifies the configured speed of the memory device, in
    /// megatransfers per second (MT/s)
    pub configured_memory_speed: Option<u16>,
    /// Minimum operating voltage for this device, in millivolts
    pub minimum_voltage: Option<u16>,
    /// Maximum operating voltage for this device, in millivolts
    pub maximum_voltage: Option<u16>,
    /// Configured voltage for this device, in millivolts
    pub configured_voltage: Option<u16>,
    /// Memory technology type for this memory device
    pub memory_technology: Option<MemoryTechnology>,
    /// The operating modes supported by this memory device
    pub operating_mode_capability: Option<OperatingModes>,
    pub firmware_version: Option<&'buffer str>,
    /// The two-byte module manufacturer ID found in the SPD of this memory
    /// device; LSB first.
    pub module_manufacturer: Option<u16>,
    /// The two-byte module product ID found in the SPD of this memory device;
    /// LSB first
    pub module_product_id: Option<u16>,
    /// The two-byte memory subsystem controller manufacturer ID found in the
    /// SPD of this memory device; LSB first
    pub memory_subsystem_controller_manufacturer_id: Option<u16>,
    /// The two-byte memory subsystem controller product ID found in the SPD
    /// of this memory device; LSB first
    pub memory_subsystem_controller_product_id: Option<u16>,
    /// Size of the Non-volatile portion of the memory device in Bytes, if any
    pub non_volatile_size: Option<u64>,
    /// Size of the Volatile portion of the memory device in Bytes, if any
    pub volatile_size: Option<u64>,
    /// Size of the Cache portion of the memory device in Bytes, if any.
    pub cache_size: Option<u64>,
    /// Size of the Logical memory device in Bytes
    pub logical_size: Option<u64>,
    /// Identifies the maximum capable speed of the device, in megatransfers per second
    pub extended_speed: Option<u32>,
    /// Identifies the configured speed of the memory device, in megatransfers per second
    pub extended_configured_memory_speed: Option<u32>,
}

impl<'buffer> MemoryDevice<'buffer> {
    pub(crate) fn try_from(
        structure: super::RawStructure<'buffer>,
    ) -> Result<MemoryDevice<'buffer>, MalformedStructureError> {
        let mut memory = MemoryDevice::default();
        let mut mem_pointer = 0;
        if structure.version > (2, 1).into() {
            memory.handle = structure.handle;
            memory.physical_memory_handle = get_word(&mut mem_pointer, &structure.data)?;
            memory.memory_error_handle =
                get_optional_word(&mut mem_pointer, &structure.data, 0xFFFE)?;
            memory.total_width = get_optional_word(&mut mem_pointer, &structure.data, 0xFFFF)?;
            memory.data_width = get_optional_word(&mut mem_pointer, &structure.data, 0xFFFF)?;
            memory.size = get_optional_word(&mut mem_pointer, &structure.data, 0xFFFF)?;

            memory.form_factor = FormFactor::from(structure.data[mem_pointer]);
            // Advance the pointer
            mem_pointer += 1;

            let device_set = structure.data[mem_pointer];
            match device_set {
                0 | 255 => memory.device_set = None,
                _ => memory.device_set = Some(device_set),
            };
            // Advance the pointer
            mem_pointer += 1;

            memory.device_locator = find_string(&structure, &mut mem_pointer)?;
            memory.bank_locator = find_string(&structure, &mut mem_pointer)?;

            memory.memory_type = Type::from(structure.data[mem_pointer]);
            // Advance the pointer
            mem_pointer += 1;

            memory.type_detail =
                Detail::from_bits_truncate(get_word(&mut mem_pointer, &structure.data)?);
        }
        if structure.version > (2, 3).into() {
            memory.speed = get_optional_word(&mut mem_pointer, &structure.data, 0)?;
            memory.manufacturer = find_string(&structure, &mut mem_pointer)?;
            memory.serial = find_string(&structure, &mut mem_pointer)?;
            memory.asset_tag = find_string(&structure, &mut mem_pointer)?;
            memory.part_number = find_string(&structure, &mut mem_pointer)?;
        }
        if structure.version > (2, 6).into() {
            memory.attributes = structure.data[mem_pointer];
            mem_pointer += 1;
        }
        if structure.version > (2, 7).into() {
            memory.extended_size = get_dword(&mut mem_pointer, &structure.data)?;
            memory.configured_memory_speed =
                get_optional_word(&mut mem_pointer, &structure.data, 0)?;
        }
        if structure.version > (2, 8).into() {
            memory.minimum_voltage = get_optional_word(&mut mem_pointer, &structure.data, 0)?;
            memory.maximum_voltage = get_optional_word(&mut mem_pointer, &structure.data, 0)?;
            memory.configured_voltage = get_optional_word(&mut mem_pointer, &structure.data, 0)?;
        }
        if structure.version > (3, 2).into() {
            memory.memory_technology = Some(MemoryTechnology::from(structure.data[mem_pointer]));
            mem_pointer += 1;
            memory.operating_mode_capability = Some(OperatingModes::from_bits_truncate(get_word(
                &mut mem_pointer,
                &structure.data,
            )?));
            memory.firmware_version = Some(find_string(&structure, &mut mem_pointer)?);
            memory.module_manufacturer = Some(get_word(&mut mem_pointer, &structure.data)?);
            memory.module_product_id = Some(get_word(&mut mem_pointer, &structure.data)?);
            memory.memory_subsystem_controller_manufacturer_id =
                Some(get_word(&mut mem_pointer, &structure.data)?);
            memory.memory_subsystem_controller_product_id =
                Some(get_word(&mut mem_pointer, &structure.data)?);
            memory.non_volatile_size =
                get_optional_qword(&mut mem_pointer, &structure.data, 0xFFFFFFFFFFFFFFFF)?;
            memory.volatile_size =
                get_optional_qword(&mut mem_pointer, &structure.data, 0xFFFFFFFFFFFFFFFF)?;
            memory.cache_size =
                get_optional_qword(&mut mem_pointer, &structure.data, 0xFFFFFFFFFFFFFFFF)?;
            memory.logical_size =
                get_optional_qword(&mut mem_pointer, &structure.data, 0xFFFFFFFFFFFFFFFF)?;
        }
        if structure.version > (3, 3).into() {
            memory.extended_speed = Some(get_dword(&mut mem_pointer, &structure.data)?);
            memory.extended_configured_memory_speed =
                Some(get_dword(&mut mem_pointer, &structure.data)?);
        }

        Ok(memory)
    }
}

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum MemoryArrayLocation {
    Other,
    Unknown,
    SystemBoardOrMotherboard,
    IsaAddOnCard,
    EisaAddOnCard,
    PciAddOnCard,
    McaAddOnCard,
    PcmciaAddOnCard,
    ProprietaryAddOnCard,
    NuBus,
    Pc98c20AddOnCard,
    Pc98c24AddOnCard,
    Pc98eAddOnCard,
    Pc98LocalBusAddOnCard,
    CxlAddOnCard,
    Undefined(u8),
}

impl Default for MemoryArrayLocation {
    fn default() -> Self {
        Self::Unknown
    }
}

impl From<u8> for MemoryArrayLocation {
    fn from(_type: u8) -> Self {
        match _type {
            1 => Self::Other,
            2 => Self::Unknown,
            3 => Self::SystemBoardOrMotherboard,
            4 => Self::IsaAddOnCard,
            5 => Self::EisaAddOnCard,
            6 => Self::PciAddOnCard,
            7 => Self::McaAddOnCard,
            8 => Self::PcmciaAddOnCard,
            9 => Self::ProprietaryAddOnCard,
            10 => Self::NuBus,
            11 => Self::Pc98c20AddOnCard,
            12 => Self::Pc98c24AddOnCard,
            13 => Self::Pc98eAddOnCard,
            14 => Self::Pc98LocalBusAddOnCard,
            15 => Self::CxlAddOnCard,
            t => Self::Undefined(t),
        }
    }
}

impl fmt::Display for MemoryArrayLocation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Other                     => write!(f, "Other"),
            Self::Unknown                   => write!(f, "Unknown"),
            Self::SystemBoardOrMotherboard  => write!(f, "System board or motherboard"),
            Self::IsaAddOnCard              => write!(f, "ISA add-on card"),
            Self::EisaAddOnCard             => write!(f, "EISA add-on card"),
            Self::PciAddOnCard              => write!(f, "PCI add-on card"),
            Self::McaAddOnCard              => write!(f, "MCA add-on card"),
            Self::PcmciaAddOnCard           => write!(f, "PCMCIA add-on card"),
            Self::ProprietaryAddOnCard      => write!(f, "Proprietary add-on card"),
            Self::NuBus                     => write!(f, "NuBus"),
            Self::Pc98c20AddOnCard          => write!(f, "PC-98/C20 add-on card"),
            Self::Pc98c24AddOnCard          => write!(f, "PC-98/C24 add-on card"),
            Self::Pc98eAddOnCard            => write!(f, "PC-98/E add-on card"),
            Self::Pc98LocalBusAddOnCard     => write!(f, "PC-98/Local bus add-on card"),
            Self::CxlAddOnCard              => write!(f, "CXL add-on card"),
            Self::Undefined(t)              => write!(f, "Undefined: {}", t),
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum MemoryArrayUse {
    Other,
    Unknown,
    SystemMemory,
    VideoMemory,
    FlashMemory,
    NonVolatileRam,
    CacheMemory,
    Undefined(u8),
}

impl Default for MemoryArrayUse {
    fn default() -> Self {
        Self::Unknown
    }
}

impl From<u8> for MemoryArrayUse {
    fn from(_type: u8) -> Self {
        match _type {
            1 => Self::Other,
            2 => Self::Unknown,
            3 => Self::SystemMemory,
            4 => Self::VideoMemory,
            5 => Self::FlashMemory,
            6 => Self::NonVolatileRam,
            7 => Self::CacheMemory,
            t => Self::Undefined(t),
        }
    }
}

impl fmt::Display for MemoryArrayUse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Other          => write!(f, "Other"),
            Self::Unknown        => write!(f, "Unknown"),
            Self::SystemMemory   => write!(f, "System memory"),
            Self::VideoMemory    => write!(f, "Video memory"),
            Self::FlashMemory    => write!(f, "Flash memory"),
            Self::NonVolatileRam => write!(f, "Non-volatile RAM"),
            Self::CacheMemory    => write!(f, "Cache memory"),
            Self::Undefined(t)   => write!(f, "Undefined: {}", t),
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum MemoryArrayErrorCorrectionTypes {
    Other,
    Unknown,
    None,
    Parity,
    SingleBitEcc,
    MultiBitEcc,
    CRC,
    Undefined(u8),
}

impl Default for MemoryArrayErrorCorrectionTypes {
    fn default() -> Self {
        Self::Unknown
    }
}

impl From<u8> for MemoryArrayErrorCorrectionTypes {
    fn from(_type: u8) -> Self {
        match _type {
            1 => Self::Other,
            2 => Self::Unknown,
            3 => Self::None,
            4 => Self::Parity,
            5 => Self::SingleBitEcc,
            6 => Self::MultiBitEcc,
            7 => Self::CRC,
            t => Self::Undefined(t),
        }
    }
}

impl fmt::Display for MemoryArrayErrorCorrectionTypes {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Other         => write!(f, "Other"),
            Self::Unknown       => write!(f, "Unknown"),
            Self::None          => write!(f, "None"),
            Self::Parity        => write!(f, "Parity"),
            Self::SingleBitEcc  => write!(f, "Single-bit ECC"),
            Self::MultiBitEcc   => write!(f, "Multi-bit ECC"),
            Self::CRC           => write!(f, "CRC"),
            Self::Undefined(t)  => write!(f, "Undefined: {}", t),
        }
    }
}

#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct PhysicalMemoryArray {
    pub handle: u16,
    /// Physical location of the Memory Array, whether on the system board or an add-in board
    pub location: MemoryArrayLocation,
    /// Function for which the array is used
    pub r#use: MemoryArrayUse,
    /// Primary hardware error correction or detection method supported by this memory array
    pub memory_error_correction: MemoryArrayErrorCorrectionTypes,
    /// Maximum memory capacity, in kilobytes, for this array
    /// If the capacity is not represented in this field, then
    /// this field contains 8000 0000h and the Extended
    /// Maximum Capacity field should be used. Values 2
    /// TB (8000 0000h) or greater must be represented
    /// in the Extended Maximum Capacity field.
    pub maximum_capacity: Option<u32>,
    /// Handle, or instance number, associated with any
    /// error that was previously detected for the array
    pub memory_error_information_handle: Option<u16>,
    /// Number of slots or sockets available for Memory Devices in this array
    /// This value represents the number of Memory Device structures that compose this Memory
    /// Array. Each Memory Device has a reference to the “owning” Memory Array.
    pub number_of_memory_devices: u16,
    /// Maximum memory capacity, in bytes, for this array.
    /// This field is only valid when the Maximum Capacity field contains 8000 0000h.
    /// When Maximum Capacity contains a value that is not 8000 0000h, Extended Maximum Capacity must contain zeros.
    pub extended_maximum_capacity: Option<u64>,
}

impl PhysicalMemoryArray {
    pub(crate) fn try_from(
        structure: super::RawStructure,
    ) -> Result<Self, MalformedStructureError> {
        let mut pma = PhysicalMemoryArray::default();
        let mut mem_pointer = 0;
        if structure.version > (2, 1).into() {
            pma.handle = structure.handle;
            pma.location = MemoryArrayLocation::from(structure.data[mem_pointer]);
            mem_pointer += 1;
            pma.r#use = MemoryArrayUse::from(structure.data[mem_pointer]);
            mem_pointer += 1;
            pma.memory_error_correction = MemoryArrayErrorCorrectionTypes::from(structure.data[mem_pointer]);
            mem_pointer += 1;
            pma.maximum_capacity = get_optional_dword(&mut mem_pointer, &structure.data, 0x80000000)?;
            pma.memory_error_information_handle = get_optional_word(&mut mem_pointer, &structure.data, 0xFFFE)?;
            pma.number_of_memory_devices = get_word(&mut mem_pointer, &structure.data)?;
        }
        if structure.version > (2, 7).into() {
            pma.extended_maximum_capacity =
                if pma.maximum_capacity.is_none() {
                    get_optional_qword(&mut mem_pointer, &structure.data, 0)?
                } else {
                    None
                };
        }
        Ok(pma)
    }
}

fn find_string<'buffer>(
    structure: &super::RawStructure<'buffer>,
    pointer: &mut usize,
) -> Result<&'buffer str, MalformedStructureError> {
    let s = structure.find_string(structure.data[*pointer])?;
    *pointer += 1;
    Ok(s)
}

fn get_optional_qword(
    pointer: &mut usize,
    data: &[u8],
    none_val: u64,
) -> Result<Option<u64>, MalformedStructureError> {
    let word = get_qword(pointer, data)?;
    if word == none_val {
        Ok(None)
    } else {
        Ok(Some(word))
    }
}

fn get_optional_dword(
    pointer: &mut usize,
    data: &[u8],
    none_val: u32,
) -> Result<Option<u32>, MalformedStructureError> {
    let word = get_dword(pointer, data)?;
    if word == none_val {
        Ok(None)
    } else {
        Ok(Some(word))
    }
}

fn get_optional_word(
    pointer: &mut usize,
    data: &[u8],
    none_val: u16,
) -> Result<Option<u16>, MalformedStructureError> {
    let word = get_word(pointer, data)?;
    if word == none_val {
        Ok(None)
    } else {
        Ok(Some(word))
    }
}

fn get_word(pointer: &mut usize, data: &[u8]) -> Result<u16, MalformedStructureError> {
    let word = u16::from_le_bytes(
        data[*pointer..(*pointer + 2)]
            .try_into()
            .map_err(|e| MalformedStructureError::InvalidSlice(e))?,
    );
    *pointer += 2;
    Ok(word)
}

fn get_dword(pointer: &mut usize, data: &[u8]) -> Result<u32, MalformedStructureError> {
    let dword = u32::from_le_bytes(
        data[*pointer..(*pointer + 4)]
            .try_into()
            .map_err(|e| MalformedStructureError::InvalidSlice(e))?,
    );
    *pointer += 4;
    Ok(dword)
}

fn get_qword(pointer: &mut usize, data: &[u8]) -> Result<u64, MalformedStructureError> {
    let qword = u64::from_le_bytes(
        data[*pointer..(*pointer + 8)]
            .try_into()
            .map_err(|e| MalformedStructureError::InvalidSlice(e))?,
    );
    *pointer += 8;
    Ok(qword)
}