tdms 0.3.0

A LabView TDMS file parser written in Rust
Documentation
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
use crate::data_type::{TDMSValue, TdmsDataType};
use crate::{to_i32, to_u32, to_u64};
use crate::{
    Big, General, InvalidDAQmxDataIndex, InvalidSegment, Little, StringConversionError, TdmsError,
};
use indexmap::{indexmap, IndexMap};
use std::io::{Read, Seek};

/// These are bitmasks for the Table of Contents byte.
const K_TOC_META_DATA: u32 = 1 << 1;
// this flag represents a segment who's channel list/order has been changed from the previous segments
// and therefore a new order for processing the raw data must be followed
const K_TOC_NEW_OBJ_LIST: u32 = 1 << 2;
const K_TOC_RAW_DATA: u32 = 1 << 3;
const K_TOC_INTERLEAVED_DATA: u32 = 1 << 5;
const K_TOC_BIG_ENDIAN: u32 = 1 << 6;
const K_TOC_DAQMX_RAW_DATA: u32 = 1 << 7;

/// Ease of use enum for determining how to read numerical values.
#[derive(Clone, Copy, Debug)]
pub enum Endianness {
    Little,
    Big,
}

#[derive(Debug, Clone)]
/// `Segment` represents an entire TDMS File Segment and potentially its raw data.
pub struct Segment {
    pub lead_in: LeadIn,
    pub metadata: Option<Metadata>,
    pub start_pos: u64,
    pub end_pos: u64,
    pub groups: IndexMap<GroupPath, Option<IndexMap<ChannelPath, Channel>>>,
    pub chunk_size: u64,
}

/// GroupPath is a simple alias to allow our function signatures to be more telling
pub type GroupPath = String;
/// ChannelPath is a simple alias to allow our function signatures to be more telling
pub type ChannelPath = String;

#[derive(Clone, Debug)]
pub struct Channel {
    pub full_path: String,
    pub group_path: String,
    pub path: String,
    pub data_type: TdmsDataType,
    pub raw_data_index: Option<RawDataIndex>,
    pub daqmx_data_index: Option<DAQmxDataIndex>,
    pub properties: Vec<MetadataProperty>,
    pub chunk_positions: Vec<ChannelPositions>,
    pub string_offset_pos: Option<ChannelPositions>,
    pub interleaved_offset: u64,
}

#[derive(Clone, Debug, Copy)]
pub struct ChannelPositions(pub u64, pub u64);

impl Segment {
    /// `new` expects a reader who's cursor position is at the start of a new TDMS segment.
    /// You will see an InvalidSegment error return if the reader position isn't correct as the first
    /// byte read will not be the correct tag for a segment. The segment will hold on to the original
    /// reader in order to be able to return data not read into memory
    pub fn new<R: Read + Seek>(
        r: &mut R,
        previous_segment: Option<&Segment>,
    ) -> Result<Self, TdmsError> {
        let segment_start_pos = r.stream_position()?;
        let mut lead_in = [0; 28];

        r.read(&mut lead_in[..])?;

        let lead_in = LeadIn::from_bytes(&lead_in)?;

        // calculate the end position by taking the start and adding the offset plus lead in bytes
        let segment_end_pos = lead_in.next_segment_offset + 28 + segment_start_pos;

        let endianness = if lead_in.table_of_contents & K_TOC_BIG_ENDIAN != 0 {
            Big
        } else {
            Little
        };

        let mut metadata: Option<Metadata> = None;
        if lead_in.table_of_contents & K_TOC_META_DATA != 0 {
            metadata = Some(Metadata::from_reader(endianness, r)?);
        }

        // if we have have metadata, load up group and channel list for the segment - I debated
        // somehow building this list dynamically as we read the file but honestly the performance
        // hit according to benches was minimal and this makes a cleaner set of function boundaries
        // and lets us get away from passing in mutable state all over the place
        let mut groups: IndexMap<GroupPath, Option<IndexMap<ChannelPath, Channel>>> =
            IndexMap::<GroupPath, Option<IndexMap<ChannelPath, Channel>>>::new();

        // this variable will tell us where we're at in the raw data of the channel, allowing us to
        // set start and end thresholds of the channel's data itself
        let mut data_pos: u64 = segment_start_pos + lead_in.raw_data_offset;
        let mut interleaved_total_size: u64 = 0;
        let mut chunk_size: u64 = 0;

        match &mut metadata {
            Some(metadata) => {
                for obj in &mut metadata.objects {
                    let path = obj.object_path.clone();
                    let paths: Vec<&str> = path.split("/").collect();

                    if obj.object_path == "/" || paths.len() < 3 {
                        continue;
                    }
                    let mut data_type: TdmsDataType = TdmsDataType::Void;

                    if previous_segment.is_some()
                        && obj.raw_data_index.is_none()
                        && obj.daqmx_data_index.is_none()
                    {
                        match previous_segment
                            .unwrap()
                            .get_channel(rem_quotes(paths[1]), rem_quotes(paths[2]))
                        {
                            None => {}
                            Some(c) => {
                                obj.raw_data_index = match &c.raw_data_index {
                                    None => None,
                                    Some(r) => Some(r.clone()),
                                };

                                obj.daqmx_data_index = match &c.daqmx_data_index {
                                    None => None,
                                    Some(d) => Some(d.clone()),
                                }
                            }
                        }
                    }

                    match &obj.raw_data_index {
                        None => {}
                        Some(index) => data_type = index.data_type,
                    }

                    match &obj.daqmx_data_index {
                        None => {}
                        Some(index) => data_type = index.data_type,
                    }

                    // add to the total interleaved size so we can calculate the offset later if needed
                    interleaved_total_size += TdmsDataType::get_size(data_type) as u64;

                    if paths.len() >= 2 && paths[1] != "" {
                        if !groups.contains_key(rem_quotes(paths[1])) {
                            let _ = groups.insert(rem_quotes(paths[1]).to_string(), None);
                        }
                    }

                    if paths.len() >= 3 && paths[2] != "" {
                        let map = groups.get_mut(rem_quotes(paths[1]));
                        let mut start_pos = data_pos.clone();
                        let mut end_pos = 0;

                        let mut string_offset_pos: Option<ChannelPositions> = None;
                        let raw_data_index = match &obj.raw_data_index {
                            Some(index) => {
                                let type_size = TdmsDataType::get_size(index.data_type);

                                // if not interleaved, the end threshold comes at chunk end
                                if lead_in.table_of_contents & K_TOC_INTERLEAVED_DATA == 0 {
                                    if index.data_type == TdmsDataType::String
                                        && index.number_of_bytes.is_some()
                                    {
                                        start_pos = start_pos + index.number_of_values * 4;
                                        string_offset_pos = Some(ChannelPositions(data_pos, data_pos + index.number_of_values * 4));

                                        data_pos = data_pos + index.number_of_bytes.unwrap();
                                        end_pos = data_pos.clone();
                                        chunk_size += index.number_of_bytes.unwrap();
                                    } else {
                                        data_pos = data_pos
                                            + (type_size as u64
                                                * index.array_dimension as u64
                                                * index.number_of_values);

                                        end_pos = data_pos.clone();
                                        chunk_size += type_size as u64
                                            * index.array_dimension as u64
                                            * index.number_of_values
                                    }
                                }

                                Some(index.clone())
                            }
                            None => None,
                        };

                        let daqmx_data_index = match &obj.daqmx_data_index {
                            Some(index) => Some(index.clone()),
                            None => None,
                        };

                        let channel = Channel {
                            full_path: obj.object_path.clone(),
                            group_path: rem_quotes(paths[1]).to_string(),
                            path: rem_quotes(paths[2]).to_string(),
                            data_type,
                            raw_data_index,
                            daqmx_data_index,
                            properties: obj.properties.clone(),
                            chunk_positions: vec![ChannelPositions(start_pos, end_pos)],
                            // this will be calculated later as we need all the channels information
                            // prior to calculating this offset
                            interleaved_offset: 0,
                            string_offset_pos
                        };

                        match map {
                            Some(map) => {
                                match map {
                                    Some(map) => {
                                        map.insert(rem_quotes(paths[2]).to_string(), channel);
                                    }
                                    None => {
                                        groups.insert(
                                    rem_quotes(paths[1]).to_string(),
                                    Some(indexmap! {rem_quotes(paths[2]).to_string() => channel}),
                                );
                                    }
                                }
                            }
                            None => (),
                        }
                    }
                }
            }
            _ => (),
        }

        if lead_in.table_of_contents & K_TOC_INTERLEAVED_DATA != 0 {
            for (_, channels) in groups.iter_mut() {
                match channels {
                    None => continue,
                    Some(channels) => {
                        for (_, channel) in channels.iter_mut() {
                            let size = TdmsDataType::get_size(channel.data_type);

                            // offset tells the iterator how many bytes to move to the next value
                            channel.interleaved_offset = interleaved_total_size - size as u64;
                            // update the end_pos_threshold  now that we have an idea of setup
                            match channel.chunk_positions.get_mut(0) {
                                None => (),
                                Some(positions) => {
                                    positions.1 = chunk_size - channel.interleaved_offset
                                        + interleaved_total_size
                                }
                            }

                            interleaved_total_size += size as u64;
                        }
                    }
                }
            }
        }

        // now we need to iterate yet again in order to build the start/end positions of each chunk
        for (_, channels) in groups.iter_mut() {
            match channels {
                None => continue,
                Some(channels) => {
                    for (_, channel) in channels.iter_mut() {
                        if channel.data_type == TdmsDataType::DAQmxRawData {
                            continue;
                        }

                        let mut i = 0;
                        loop {
                            let ChannelPositions(prev_start, prev_end) =
                                match channel.chunk_positions.get(i) {
                                    None => {
                                        return Err(General(String::from(
                                            "unable to fetch previous chunk positions",
                                        )))
                                    }
                                    Some(p) => p,
                                };

                            let new_start = prev_start + chunk_size;
                            let mut new_end = prev_end + chunk_size;

                            if new_start > segment_end_pos {
                                break;
                            }

                            if new_end > segment_end_pos {
                                new_end = segment_end_pos;
                            }

                            channel
                                .chunk_positions
                                .push(ChannelPositions(new_start, new_end));
                            i += 1;
                        }
                    }
                }
            }
        }

        //

        return Ok(Segment {
            lead_in,
            metadata,
            start_pos: segment_start_pos,
            // lead in plus offset
            end_pos: segment_end_pos,
            groups,
            chunk_size,
        });
    }

    /// this function is not accurate unless the lead in portion of the segment has been read
    pub fn endianess(&self) -> Endianness {
        return if self.lead_in.table_of_contents & K_TOC_BIG_ENDIAN != 0 {
            Big
        } else {
            Little
        };
    }

    /// this function is not accurate unless the lead in portion of the segment has been read
    pub fn has_interleaved_data(&self) -> bool {
        return self.lead_in.table_of_contents & K_TOC_INTERLEAVED_DATA != 0;
    }

    /// this function is not accurate unless the lead in portion of the segment has been read
    pub fn has_daqmx_raw_data(&self) -> bool {
        return self.lead_in.table_of_contents & K_TOC_DAQMX_RAW_DATA != 0;
    }

    /// this function is not accurate unless the lead in portion of the segment has been read
    pub fn has_raw_data(&self) -> bool {
        return self.lead_in.table_of_contents & K_TOC_RAW_DATA != 0;
    }

    /// this function is not accurate unless the lead in portion of the segment has been read
    pub fn has_new_obj_list(&self) -> bool {
        return self.lead_in.table_of_contents & K_TOC_NEW_OBJ_LIST != 0;
    }

    pub fn get_channel_mut(&mut self, group_path: &str, path: &str) -> Option<&mut Channel> {
        let group = match self.groups.get_mut(group_path) {
            None => return None,
            Some(g) => g,
        };

        let channels = match group {
            None => return None,
            Some(c) => c,
        };

        return channels.get_mut(path);
    }

    pub fn get_channel(&self, group_path: &str, path: &str) -> Option<&Channel> {
        let group = match self.groups.get(group_path) {
            None => return None,
            Some(g) => g,
        };

        let channels = match group {
            None => return None,
            Some(c) => c,
        };

        return channels.get(path);
    }
}

#[derive(Debug, Clone)]
/// `LeadIn` represents the 28 bytes representing the lead in to a TDMS Segment.
pub struct LeadIn {
    pub tag: [u8; 4],
    pub table_of_contents: u32,
    pub version_number: u32,
    pub next_segment_offset: u64,
    pub raw_data_offset: u64,
}

impl LeadIn {
    /// `from_bytes` accepts a 28 byte array which represents the lead-in to a segment. This is hardcoded
    /// as there are no dynamic lengths in this portion of a segment
    pub fn from_bytes(lead_in: &[u8; 28]) -> Result<Self, TdmsError> {
        let mut tag: [u8; 4] = [0; 4];
        tag.clone_from_slice(&lead_in[0..4]);

        if hex::encode(tag) != String::from("5444536d") {
            return Err(InvalidSegment());
        }

        let mut toc: [u8; 4] = [0; 4];
        toc.clone_from_slice(&lead_in[4..8]);

        // the Table of Contents is always in little endian format regardless if the rest of the segment
        // is in big endian
        let table_of_contents = u32::from_le_bytes(toc);

        let mut version: [u8; 4] = [0; 4];
        version.clone_from_slice(&lead_in[8..12]);

        let version_number = if table_of_contents & K_TOC_BIG_ENDIAN != 0 {
            u32::from_be_bytes(version)
        } else {
            u32::from_le_bytes(version)
        };

        let mut offset: [u8; 8] = [0; 8];
        offset.clone_from_slice(&lead_in[12..20]);

        let next_segment_offset = if table_of_contents & K_TOC_BIG_ENDIAN != 0 {
            u64::from_be_bytes(offset)
        } else {
            u64::from_le_bytes(offset)
        };

        let mut raw_offset: [u8; 8] = [0; 8];
        raw_offset.clone_from_slice(&lead_in[20..28]);

        let raw_data_offset = if table_of_contents & K_TOC_BIG_ENDIAN != 0 {
            u64::from_be_bytes(raw_offset) + 28
        } else {
            u64::from_le_bytes(raw_offset) + 28
        };

        return Ok(LeadIn {
            tag,
            table_of_contents,
            version_number,
            next_segment_offset,
            raw_data_offset,
        });
    }
}

#[derive(Debug, Clone)]
/// `Metadata` represents the collection of metadata objects for a segment in the order in which they
/// were read
pub struct Metadata {
    pub number_of_objects: u32,
    pub objects: Vec<MetadataObject>,
}

#[derive(Debug, Clone)]
/// `MetadataObject` represents information that is not raw data associated with the segment. May
/// contain DAQmx raw data index, a standard index, or nothing at all.
pub struct MetadataObject {
    pub object_path: String,
    pub raw_data_index: Option<RawDataIndex>,
    pub daqmx_data_index: Option<DAQmxDataIndex>,
    pub properties: Vec<MetadataProperty>,
}

impl Metadata {
    /// from_reader accepts an open reader and attempts to read metadata from the currently selected
    /// segment. Note that you must have read the segment's lead in information completely before
    /// attempting to use this function
    pub fn from_reader<R: Read + Seek>(
        endianness: Endianness,
        r: &mut R,
    ) -> Result<Self, TdmsError> {
        let mut buf: [u8; 4] = [0; 4];
        r.read(&mut buf)?;

        let number_of_objects = match endianness {
            Little => u32::from_le_bytes(buf),
            Big => u32::from_be_bytes(buf),
        };

        let mut objects: Vec<MetadataObject> = vec![];

        for _ in 0..number_of_objects {
            let mut buf: [u8; 4] = [0; 4];
            r.read(&mut buf)?;

            let length: u32 = to_u32!(buf, endianness);

            // must be a vec due to variable length
            let length = match usize::try_from(length) {
                Ok(l) => l,
                Err(_) => {
                    return Err(General(String::from(
                        "error converting strength length to system size",
                    )))
                }
            };

            let mut path = vec![0; length];
            r.read_exact(&mut path)?;

            // all strings are UTF8 encoded in TDMS files, most prefixed by the length attribute
            // like above
            let object_path = match String::from_utf8(path) {
                Ok(n) => n,
                Err(_) => {
                    return Err(StringConversionError(String::from(
                        "unable to convert object path",
                    )))
                }
            };

            let mut buf: [u8; 4] = [0; 4];
            r.read_exact(&mut buf)?;
            let mut raw_data_index: Option<RawDataIndex> = None;
            let mut daqmx_data_index: Option<DAQmxDataIndex> = None;

            let first_byte: u32 = to_u32!(buf, endianness);

            // indicates format changing scaler
            if first_byte == 0x69120000 || first_byte == 0x00001269 {
                let index = DAQmxDataIndex::from_reader(endianness, r, true)?;
                daqmx_data_index = Some(index);
                // indicates digital line scaler
            } else if first_byte == 0x69130000
                || first_byte == 0x0000126A
                || first_byte == 0x00001369
            {
                let index = DAQmxDataIndex::from_reader(endianness, r, false)?;
                daqmx_data_index = Some(index);
            } else {
                if first_byte != 0xFFFFFFFF && first_byte != 0x0000000 {
                    raw_data_index = Some(RawDataIndex::from_reader(endianness, r)?)
                }
            }

            r.read_exact(&mut buf)?;
            let num_of_properties: u32 = to_u32!(buf, endianness);

            // now we iterate through all the properties for the object
            let mut properties: Vec<MetadataProperty> = vec![];
            for _ in 0..num_of_properties {
                match MetadataProperty::from_reader(endianness, r) {
                    Ok(p) => properties.push(p),
                    Err(e) => return Err(e),
                };
            }

            objects.push(MetadataObject {
                object_path,
                raw_data_index,
                daqmx_data_index,
                properties,
            });
        }

        return Ok(Metadata {
            number_of_objects,
            objects,
        });
    }
}

#[derive(Debug, Clone)]
pub struct RawDataIndex {
    pub data_type: TdmsDataType,
    pub array_dimension: u32, // should only ever be 1
    pub number_of_values: u64,
    pub number_of_bytes: Option<u64>, // only valid if data type is TDMS String
}

impl RawDataIndex {
    pub fn from_reader<R: Read + Seek>(
        endianness: Endianness,
        r: &mut R,
    ) -> Result<Self, TdmsError> {
        let mut buf: [u8; 4] = [0; 4];

        // now we check the data type
        r.read_exact(&mut buf)?;
        let data_type = to_i32!(buf, endianness);

        let data_type = TdmsDataType::try_from(data_type)?;

        r.read_exact(&mut buf)?;
        let array_dimension: u32 = to_u32!(buf, endianness);

        let mut buf: [u8; 8] = [0; 8];
        r.read_exact(&mut buf)?;
        let number_of_values = to_u64!(buf, endianness);

        let number_of_bytes: Option<u64> = match data_type {
            TdmsDataType::String => {
                r.read_exact(&mut buf)?;
                let num = to_u64!(buf, endianness);

                Some(num)
            }
            _ => None,
        };

        return Ok(RawDataIndex {
            data_type,
            array_dimension,
            number_of_values,
            number_of_bytes,
        });
    }
}

#[derive(Debug, Clone)]
pub struct DAQmxDataIndex {
    pub data_type: TdmsDataType,
    pub array_dimension: u32, // should only ever be 1
    pub number_of_values: u64,
    pub format_changing_size: Option<u32>,
    pub format_changing_vec: Option<Vec<FormatChangingScaler>>,
    pub buffer_vec_size: u32,
    pub buffers: Vec<u32>,
}

impl DAQmxDataIndex {
    pub fn from_reader<R: Read + Seek>(
        endianness: Endianness,
        r: &mut R,
        is_format_changing: bool,
    ) -> Result<Self, TdmsError> {
        let mut buf: [u8; 4] = [0; 4];
        r.read_exact(&mut buf)?;

        let data_type = to_u32!(buf, endianness);

        if data_type != 0xFFFFFFFF {
            return Err(InvalidDAQmxDataIndex());
        }

        r.read_exact(&mut buf)?;
        let array_dimension = to_u32!(buf, endianness);

        let mut buf: [u8; 8] = [0; 8];
        r.read_exact(&mut buf)?;
        let number_of_values = to_u64!(buf, endianness);

        let mut buf: [u8; 4] = [0; 4];

        let format_changing_size: Option<u32> = None;
        let format_changing_vec: Option<Vec<FormatChangingScaler>> = None;
        if is_format_changing {
            r.read_exact(&mut buf)?;
            let changing_vec_size = to_u32!(buf, endianness);

            let mut vec: Vec<FormatChangingScaler> = vec![];
            for _ in 0..changing_vec_size {
                vec.push(FormatChangingScaler::from_reader(endianness, r)?)
            }
        }

        r.read_exact(&mut buf)?;
        let buffer_vec_size = to_u32!(buf, endianness);

        let mut buffers: Vec<u32> = vec![];

        for _ in 0..buffer_vec_size {
            r.read_exact(&mut buf)?;
            let elements = to_u32!(buf, endianness);

            buffers.push(elements);
        }

        return Ok(DAQmxDataIndex {
            data_type: TdmsDataType::DAQmxRawData,
            array_dimension,
            number_of_values,
            format_changing_size,
            format_changing_vec,
            buffer_vec_size,
            buffers,
        });
    }
}

#[derive(Debug, Clone)]
pub struct FormatChangingScaler {
    pub data_type: TdmsDataType,
    pub raw_buffer_index: u32,
    pub raw_byte_offset: u32,
    pub sample_format_bitmap: u32,
    pub scale_id: u32,
}

impl FormatChangingScaler {
    pub fn from_reader<R: Read + Seek>(
        endianness: Endianness,
        r: &mut R,
    ) -> Result<Self, TdmsError> {
        let mut buf: [u8; 4] = [0; 4];
        r.read_exact(&mut buf)?;

        let data_type = to_i32!(buf, endianness);

        let data_type = TdmsDataType::try_from(data_type)?;

        r.read_exact(&mut buf)?;
        let raw_buffer_index = to_u32!(buf, endianness);

        r.read_exact(&mut buf)?;
        let raw_byte_offset = to_u32!(buf, endianness);

        r.read_exact(&mut buf)?;
        let sample_format_bitmap = to_u32!(buf, endianness);

        r.read_exact(&mut buf)?;
        let scale_id = to_u32!(buf, endianness);

        return Ok(FormatChangingScaler {
            data_type,
            raw_buffer_index,
            raw_byte_offset,
            sample_format_bitmap,
            scale_id,
        });
    }
}

#[derive(Debug, Clone)]
/// `MetadataProperty` is a key/value pair associated with a `MetadataObject`
pub struct MetadataProperty {
    pub name: String,
    pub data_type: TdmsDataType,
    pub value: TDMSValue,
}

impl MetadataProperty {
    /// from_reader accepts an open reader and attempts to read metadata properties from the currently
    /// selected segment and metadata object. Note that you must have read the metadata object's lead
    /// in information prior to using this function
    pub fn from_reader<R: Read + Seek>(
        endianness: Endianness,
        r: &mut R,
    ) -> Result<Self, TdmsError> {
        let mut buf: [u8; 4] = [0; 4];
        r.read_exact(&mut buf)?;

        let length: u32 = to_u32!(buf, endianness);

        // must be a vec due to variable length
        let length = match usize::try_from(length) {
            Ok(l) => l,
            Err(_) => {
                return Err(General(String::from(
                    "error converting strength length to system size",
                )))
            }
        };

        let mut name = vec![0; length];
        r.read_exact(&mut name)?;

        // all strings are UTF8 encoded in TDMS files, most prefixed by the length attribute
        // like above
        let name = match String::from_utf8(name) {
            Ok(n) => n,
            Err(_) => {
                return Err(StringConversionError(String::from(
                    "unable to convert metadata property name",
                )))
            }
        };

        // now we check the data type
        r.read_exact(&mut buf)?;
        let data_type = to_i32!(buf, endianness);

        let data_type = TdmsDataType::try_from(data_type)?;
        let value = TDMSValue::from_reader(endianness, data_type, r)?;

        return Ok(MetadataProperty {
            name,
            data_type,
            value,
        });
    }
}

fn rem_quotes(value: &str) -> &str {
    let mut original = value.chars();
    let mut chars = value.clone().chars().peekable();

    match chars.peek() {
        None => (),
        Some(first) => {
            if first.to_string() == "'" {
                original.next();
            }
        }
    }

    let mut reversed = chars.rev().peekable();
    match reversed.peek() {
        None => (),
        Some(last) => {
            if last.to_string() == "'" {
                original.next_back();
            }
        }
    }

    original.as_str()
}

#[macro_export]
macro_rules! to_u32 {
    ( $x:ident, $t:ident ) => {
        match $t {
            Little => u32::from_le_bytes($x),
            Big => u32::from_be_bytes($x),
        }
    };
}

#[macro_export]
macro_rules! to_i32 {
    ( $x:ident, $t:ident ) => {
        match $t {
            Little => i32::from_le_bytes($x),
            Big => i32::from_be_bytes($x),
        }
    };
}

#[macro_export]
macro_rules! to_u64 {
    ( $x:ident, $t:ident ) => {
        match $t {
            Little => u64::from_le_bytes($x),
            Big => u64::from_be_bytes($x),
        }
    };
}

#[macro_export]
macro_rules! to_f64 {
    ( $x:ident, $t:ident ) => {
        match $t {
            Little => f64::from_le_bytes($x),
            Big => f64::from_be_bytes($x),
        }
    };
}