thermorawfilereader 0.6.0

A (relatively) high level interface to Thermo Fisher's RawFileReader library
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
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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
use std::borrow::Cow;
use std::ffi::c_void;
use std::fmt::{Debug, Display};
use std::iter::{FusedIterator, ExactSizeIterator};
use std::path::PathBuf;
use std::sync::Arc;
use std::{io, ptr};

use netcorehost::hostfxr::ManagedFunction;
use netcorehost::{hostfxr::AssemblyDelegateLoader, pdcstr};

use flatbuffers::{root, root_unchecked, Vector};

use dotnetrawfilereader_sys::{try_get_runtime, RawVec};

use crate::constants::{IonizationMode, MSOrder, MassAnalyzer, ScanMode, TraceType};
use crate::schema::{
    root_as_spectrum_description, root_as_spectrum_description_unchecked, AcquisitionT,
    ChromatogramDescription as ChromatogramDescriptionT, ExtendedSpectrumDataT, FileDescriptionT,
    InstrumentMethodT, InstrumentModelT, Polarity, PrecursorT, SpectrumData as SpectrumDataT,
    SpectrumDescription, SpectrumMode, StatusLogCollectionT, TrailerValuesT,
};

macro_rules! view_proxy {
    ($meth:ident) => {
        pub fn $meth(&self) -> Option<&str> {
            self.view().$meth()
        }
    };
    ($meth:ident, $descr:literal) => {
        #[doc=$descr]
        pub fn $meth(&self) -> Option<&str> {
            self.view().$meth()
        }
    };
    ($meth:ident, $descr:literal, $ret:ty) => {
        #[doc=$descr]
        pub fn $meth(&self) -> $ret {
            self.view().$meth()
        }
    };
    ($meth:ident, $ret:ty) => {
        pub fn $meth(&self) -> $ret {
            self.view().$meth()
        }
    };
    ($meth:ident, $descr:literal, $ret:ty, cast) => {
        #[doc=$descr]
        pub fn $meth(&self) -> $ret {
            let data = self.view().$meth();
            #[cfg(target_endian = "big")]
            return Cow::Owned(data.iter().copied().collect());
            #[cfg(target_endian = "little")]
            return Cow::Borrowed(bytemuck::cast_slice(data.bytes()));
        }
    };
    ($meth:ident, $descr:literal, $ret:ty, optcast) => {
        #[doc=$descr]
        pub fn $meth(&self) -> $ret {
            let data = self.view().$meth();
            #[cfg(target_endian = "big")]
            return data.map(|data| Cow::Owned(data.iter().copied().collect()));
            #[cfg(target_endian = "little")]
            return data.map(|data| Cow::Borrowed(bytemuck::cast_slice(data.bytes())));
        }
    };
}

#[repr(u32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// A set of error codes to describe how creating and using a `RawFileReader` might fail (or not).
pub enum RawFileReaderError {
    /// No problem, move along
    Ok = 0,
    /// The file path given doesn't exist
    FileNotFound,
    /// The file path given does exist, but it's not a Thermo RAW file
    InvalidFormat,
    /// The handle provided doesn't exist, someone is doing something odd like making a new [`RawFileReader`]
    /// somehow other than [`RawFileReader::open`]
    HandleNotFound,
    /// Some other error occurred
    Error = 999,
}

impl Display for RawFileReaderError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl std::error::Error for RawFileReaderError {}

impl From<u32> for RawFileReaderError {
    fn from(value: u32) -> Self {
        match value {
            0 => Self::Ok,
            1 => Self::FileNotFound,
            2 => Self::InvalidFormat,
            _ => Self::Error,
        }
    }
}

#[derive()]
/// A wrapper around the `SpectrumDescription` FlatBuffer schema. It mirrors the data
/// stored there-in.
pub struct RawSpectrum {
    data: RawVec<u8>,
}

impl Debug for RawSpectrum {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RawSpectrum")
            .field("data-size", &self.data.len())
            .finish()
    }
}

impl RawSpectrum {
    /// Create a new [`RawSpectrum`] by wrapping an owning memory buffer
    pub fn new(data: RawVec<u8>) -> Self {
        Self { data }
    }

    /// Check that the buffer is a valid `SpectrumDescription`
    pub fn check(&self) -> bool {
        root_as_spectrum_description(&self.data).is_ok()
    }

    /// View the underlying buffer as a `SpectrumDescription`
    pub fn view(&self) -> SpectrumDescription<'_> {
        unsafe { root_as_spectrum_description_unchecked(&self.data) }
    }

    /// Generate the "native ID" string format for the spectrum
    pub fn native_id(&self) -> String {
        format!(
            "controllerType=0 controllerNumber=1 scan={}",
            self.index() + 1
        )
    }

    /// The 0-base index of the spectrum
    pub fn index(&self) -> usize {
        self.view().index() as usize
    }

    /// The MS exponentiation level
    pub fn ms_level(&self) -> u8 {
        self.view().ms_level()
    }

    /// The scan start time, in minutes
    pub fn time(&self) -> f64 {
        self.view().time()
    }

    /// Whether the spectrum is positive or negative mode.
    /// [`Polarity`] is a FlatBuffer enum
    pub fn polarity(&self) -> Polarity {
        self.view().polarity()
    }

    /// Whether the spectrum is profile or centroid.
    /// [`SpectrumMode`] is a FlatBuffer enum
    pub fn mode(&self) -> SpectrumMode {
        self.view().mode()
    }

    pub fn precursor(&self) -> Option<&PrecursorT> {
        self.view().precursor()
    }

    pub fn ms_order(&self) -> MSOrder {
        MSOrder::from(self.view().ms_order().0)
    }

    pub fn scan_mode(&self) -> ScanMode {
        ScanMode::from(self.view().scan_mode().0)
    }

    /// Retrieve a view of the spectrum array data in a raw
    /// [`SpectrumDataT`] FlatBuffer struct, which is less expressive
    /// than [`SpectrumData`].
    ///
    /// This will be absent if signal loading is disabled with [`RawFileReader::set_signal_loading`].
    /// This returns a raw FlatBuffer struct. See the [schema](https://github.com/mobiusklein/thermorawfilereader.rs/blob/main/schema/schema.fbs) for more details
    pub fn data_raw(&self) -> Option<SpectrumDataT<'_>> {
        self.view().data()
    }

    /// Retrieve a view of the spectrum array data as a [`SpectrumData`],
    /// which will try to decode the m/z and intensity vectors and expose
    /// them as Rust-interpretable slices.
    ///
    /// This will be absent if signal loading is disabled with [`RawFileReader::set_signal_loading`].
    pub fn data(&self) -> Option<SpectrumData<'_>> {
        if let Some(d) = self.view().data() {
            if let Some(m) = d.mz() {
                if let Some(i) = d.intensity() {
                    return Some(SpectrumData {
                        mz: m,
                        intensity: i,
                    });
                }
            }
        }
        return None;
    }

    /// Get the spectrum's filter string as a raw string. It's format is controlled
    /// by Thermo and some information has already been extracted into other fields.
    /// Some usecases still require parsing it directly as a string.
    pub fn filter_string(&self) -> Option<&str> {
        self.view().filter_string()
    }

    /// Get less essential information about *how* the spectrum was acquired.
    ///
    /// This is a raw FlatBuffer struct. See the [schema](https://github.com/mobiusklein/thermorawfilereader.rs/blob/main/schema/schema.fbs) for more details.
    pub fn acquisition(&self) -> Option<AcquisitionT<'_>> {
        self.view().acquisition()
    }
}


pub struct OwnedSpectrumData {
    data: RawVec<u8>,
}

impl OwnedSpectrumData {
    pub fn new(data: RawVec<u8>) -> Self {
        Self { data }
    }

    /// Check that the buffer is a valid `SpectrumDescription`
    pub fn check(&self) -> bool {
        root::<SpectrumDataT>(&self.data).is_ok()
    }

    /// View the underlying buffer as a `SpectrumDescription`
    pub fn raw_view(&self) -> SpectrumDataT<'_> {
        unsafe { root_unchecked::<SpectrumDataT>(&self.data) }
    }

    pub fn view(&self) -> SpectrumData<'_> {
        let view = self.raw_view();
        let mz = view.mz().unwrap_or_default();
        let intensity = view.intensity().unwrap_or_default();
        SpectrumData { mz, intensity }
    }

    pub fn mz(&self) -> Cow<'_, [f64]> {
        self.view().mz()
    }

    pub fn intensity(&self) -> Cow<'_, [f32]> {
        self.view().intensity()
    }

    pub fn is_empty(&self) -> bool {
        self.view().is_empty()
    }

    pub fn iter(&self) -> std::iter::Zip<flatbuffers::VectorIter<'_, f64>, flatbuffers::VectorIter<'_, f32>> {
        self.view().iter()
    }

    pub fn len(&self) -> usize {
        self.view().len()
    }
}


pub struct OwnedSpectrumDataIter {
    inner: OwnedSpectrumData,
    i: usize
}

impl Iterator for OwnedSpectrumDataIter {
    type Item = (f64, f32);

    fn next(&mut self) -> Option<Self::Item> {
        let x = self.at(self.i);
        self.i += 1;
        x
    }
}

impl OwnedSpectrumDataIter {
    fn at(&self, i: usize) -> Option<(f64, f32)> {
        let mz = self.inner.mz().get(i).copied()?;
        let int = self.inner.intensity().get(i).copied()?;
        Some((mz, int))
    }
}


/// A sub-set of a [`RawSpectrum`] corresponding to the m/z and intensity arrays
/// of a mass spectrum. All data is borrowed internally from the [`RawSpectrum`]'s
/// buffer.
#[derive(Debug)]
pub struct SpectrumData<'a> {
    mz: Vector<'a, f64>,
    intensity: Vector<'a, f32>,
}

impl<'a> SpectrumData<'a> {
    /// The m/z array of the spectrum
    pub fn mz(&self) -> Cow<'a, [f64]> {
        #[cfg(target_endian = "big")]
        return Cow::Owned(self.mz.iter().copied().collect());
        #[cfg(target_endian = "little")]
        Cow::Borrowed(bytemuck::cast_slice(self.mz.bytes()))
    }

    /// The intensity array of the spectrum
    pub fn intensity(&self) -> Cow<'a, [f32]> {
        #[cfg(target_endian = "big")]
        return Cow::Owned(self.intensity.iter().copied().collect());
        #[cfg(target_endian = "little")]
        Cow::Borrowed(bytemuck::cast_slice(self.intensity.bytes()))
    }

    pub fn iter(
        &self,
    ) -> std::iter::Zip<flatbuffers::VectorIter<'a, f64>, flatbuffers::VectorIter<'a, f32>> {
        let it = self.mz.iter().zip(self.intensity.iter());
        it
    }

    pub fn len(&self) -> usize {
        self.mz.len()
    }

    pub fn is_empty(&self) -> bool {
        self.mz.is_empty()
    }
}

impl<'a> IntoIterator for SpectrumData<'a> {
    type Item = (f64, f32);

    type IntoIter =
        std::iter::Zip<flatbuffers::VectorIter<'a, f64>, flatbuffers::VectorIter<'a, f32>>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

pub struct TrailerValues {
    data: RawVec<u8>,
}

impl Debug for TrailerValues {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let items: Vec<_> = self.iter().collect();
        f.debug_struct("TrailerValues")
            .field("data-size", &self.data.len())
            .field("entries", &items)
            .finish()
    }
}

/// A single Trailer Extra value entry.
///
/// This borrows its storage from the original buffer.
#[derive(Debug, Clone, Copy)]
pub struct TrailerValue<'a> {
    /// The human-readable label for this trailer
    pub label: &'a str,
    /// The raw value string for this trailer
    pub value: &'a str,
}

impl TrailerValues {
    pub fn new(data: RawVec<u8>) -> Self {
        Self { data }
    }

    /// Check that the buffer is a valid `TrailerValuesT`
    pub fn check(&self) -> bool {
        root::<TrailerValuesT>(&self.data).is_ok()
    }

    /// View the underlying buffer as a `TrailerValuesT`
    pub fn view(&self) -> TrailerValuesT<'_>{
        unsafe { root_unchecked::<TrailerValuesT>(&self.data) }
    }

    pub fn len(&self) -> usize {
        self.view()
            .trailers()
            .into_iter()
            .next()
            .map(|v| v.len())
            .unwrap_or_default()
    }

    pub fn is_empty(&self) -> bool {
        self.view().trailers().is_some_and(|v| v.is_empty())
    }

    pub fn iter(&self) -> impl Iterator<Item = TrailerValue<'_>> + '_ {
        self.view()
            .trailers()
            .into_iter()
            .flatten()
            .map(|i| -> TrailerValue<'_> {
                TrailerValue {
                    label: i.label().unwrap(),
                    value: i.value().unwrap(),
                }
            })
    }

    pub fn get(&self, index: usize) -> Option<TrailerValue<'_>> {
        self.view().trailers().into_iter().next().and_then(|vec| {
            if index >= vec.len() {
                None
            } else {
                let i = vec.get(index);
                Some(TrailerValue {
                    label: i.label().unwrap(),
                    value: i.value().unwrap(),
                })
            }
        })
    }

    pub fn get_label(&self, label: &str) -> Option<TrailerValue<'_>> {
        self.iter().find(|i| i.label == label)
    }
}

pub struct ExtendedSpectrumData {
    data: RawVec<u8>,
}

impl ExtendedSpectrumData {
    pub fn new(data: RawVec<u8>) -> Self {
        Self { data }
    }

    /// Check that the buffer is a valid `ExtendedSpectrumData`
    pub fn check(&self) -> bool {
        root::<ExtendedSpectrumDataT>(&self.data).is_ok()
    }

    /// View the underlying buffer as a `ExtendedSpectrumData`
    pub fn view(&self) -> ExtendedSpectrumDataT<'_> {
        unsafe { root_unchecked::<ExtendedSpectrumDataT>(&self.data) }
    }

    view_proxy!(
        noise,
        "Access the peak-local noise array, if available",
        Option<Cow<'_, [f32]>>,
        optcast
    );

    view_proxy!(
        baseline,
        "Access the baseline signal array, if available",
        Option<Cow<'_, [f32]>>,
        optcast
    );

    view_proxy!(
        charge,
        "Access the peak charge array, if available",
        Option<Cow<'_, [f32]>>,
        optcast
    );

    view_proxy!(
        resolution,
        "Access the peak resolution array, if available",
        Option<Cow<'_, [f32]>>,
        optcast
    );

    view_proxy!(
        sampled_noise,
        "Access the sampled noise array, if available",
        Option<Cow<'_, [f32]>>,
        optcast
    );

    view_proxy!(
        sampled_noise_baseline,
        "Access the sampled noise baseline array, if available",
        Option<Cow<'_, [f32]>>,
        optcast
    );

    view_proxy!(
        sampled_noise_mz,
        "Access the sampled noise m/z array, if available",
        Option<Cow<'_, [f32]>>,
        optcast
    );
}

/// The signal trace of a chromatogram
pub struct ChromatogramData<'a> {
    time: Vector<'a, f64>,
    intensity: Vector<'a, f32>,
}

impl<'a> ChromatogramData<'a> {
    /// The time array of the spectrum, in minutes
    pub fn time(&self) -> Cow<'a, [f64]> {
        #[cfg(target_endian = "big")]
        return Cow::Owned(self.time.iter().copied().collect());
        #[cfg(target_endian = "little")]
        Cow::Borrowed(bytemuck::cast_slice(self.time.bytes()))
    }

    /// The intensity array of the spectrum
    pub fn intensity(&self) -> Cow<'a, [f32]> {
        #[cfg(target_endian = "big")]
        return Cow::Owned(self.intensity.iter().copied().collect());
        #[cfg(target_endian = "little")]
        Cow::Borrowed(bytemuck::cast_slice(self.intensity.bytes()))
    }

    /// Iterate over time-intensity pairs
    pub fn iter(
        &self,
    ) -> std::iter::Zip<flatbuffers::VectorIter<'a, f64>, flatbuffers::VectorIter<'a, f32>> {
        let it = self.time.iter().zip(self.intensity.iter());
        it
    }

    pub fn len(&self) -> usize {
        self.time.len()
    }

    pub fn is_empty(&self) -> bool {
        self.time.is_empty()
    }
}

impl<'a> IntoIterator for ChromatogramData<'a> {
    type Item = (f64, f32);

    type IntoIter =
        std::iter::Zip<flatbuffers::VectorIter<'a, f64>, flatbuffers::VectorIter<'a, f32>>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// A wrapper around the `InstrumentModel` FlatBuffer schema. It mirrors the data
/// stored there-in.
///
/// It contains a description of the instrument hardware and control software.
pub struct InstrumentModel {
    data: RawVec<u8>,
}

/// An instrument configuration is a set of hardware components
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InstrumentConfiguration {
    /// The mass analyzer used in this configuration
    pub mass_analyzer: MassAnalyzer,
    /// The ionization mode used in this configuration
    pub ionization_mode: IonizationMode,
}

impl Display for InstrumentConfiguration {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

impl InstrumentModel {
    /// Create a new [`InstrumentModel`] by wrapping an owning memory buffer
    pub fn new(data: RawVec<u8>) -> Self {
        Self { data }
    }

    /// Check that the buffer is a valid `InstrumentModelT`
    pub fn check(&self) -> bool {
        root::<InstrumentModelT>(&self.data).is_ok()
    }

    /// View the underlying buffer as a `InstrumentModelT`
    pub fn view(&self) -> InstrumentModelT<'_> {
        root::<InstrumentModelT>(&self.data).unwrap()
    }

    /// The instrument model's name as a string.
    ///
    /// The majority of instrument models map onto the PSI-MS
    /// controlled vocabulary uniquely, but not all do.
    pub fn model(&self) -> Option<&str> {
        self.view().model()
    }

    /// The instrument's name as configured by the
    /// operator.
    pub fn name(&self) -> Option<&str> {
        self.view().name()
    }

    /// The instrument's serial number as specified by
    /// the manufacturer.
    pub fn serial_number(&self) -> Option<&str> {
        self.view().serial_number()
    }

    /// The hardware version string.
    pub fn hardware_version(&self) -> Option<&str> {
        self.view().hardware_version()
    }

    /// The acquisition software version string.
    ///
    /// This corresponds to the version of Xcalibur.
    pub fn software_version(&self) -> Option<&str> {
        self.view().software_version()
    }

    /// The set of distinct instrument configuration names.
    pub fn configurations(&self) -> impl Iterator<Item = InstrumentConfiguration> + '_ {
        self.view().configurations().into_iter().flatten().map(|c| {
            let ionization_mode = c.ionization_mode().0.into();
            let mass_analyzer = c.mass_analyzer().0.into();
            InstrumentConfiguration {
                ionization_mode,
                mass_analyzer,
            }
        })
    }
}

/// A wrapper around the `FileDescription` FlatBuffer schema. It mirrors the data
/// stored there-in.
///
/// It describes the contents of the RAW file and a small amount information about
/// how it was created.
pub struct FileDescription {
    data: RawVec<u8>,
}

impl FileDescription {
    pub fn new(data: RawVec<u8>) -> Self {
        Self { data }
    }

    /// Check that the buffer is a valid `FileDescriptionT`
    pub fn check(&self) -> bool {
        root::<FileDescriptionT>(&self.data).is_ok()
    }

    /// View the underlying buffer as a `FileDescriptionT`
    pub fn view(&self) -> FileDescriptionT<'_> {
        root::<FileDescriptionT>(&self.data).unwrap()
    }

    view_proxy!(
        sample_id,
        "The sample identifier provided by the user, if one is present"
    );
    view_proxy!(
        sample_vial,
        "The sample vial name provided by the user or sample handling system, if present"
    );
    view_proxy!(
        sample_comment,
        "The comment describing the sample as provided by the user, if present"
    );
    view_proxy!(
        sample_name,
        "The sample name provided by the user, if one is present"
    );
    view_proxy!(
        source_file,
        "The name of the RAW file being described, as it was recorded by the control software"
    );
    view_proxy!(
        creation_date,
        "The date the RAW file was created, or that the instrument run was performed"
    );

    /// The number of spectra at MS levels 1-10.
    ///
    /// This returns a [`flatbuffers::Vector`] of counts where index `i` corresponds
    /// to the number of MS level `i+1` spectra in the RAW file.
    pub fn spectra_per_ms_level(&self) -> Option<flatbuffers::Vector<'_, u32>> {
        self.view().spectra_per_ms_level()
    }

    /// Trailer headers for the RAW file.
    pub fn trailer_headers(&self) -> Option<Vector<'_, flatbuffers::ForwardsUOffset<&str>>> {
        self.view().trailer_headers()
    }
}

/// The text describing how the instrument was told to operate.
///
/// The text is rendered however the particular instrument reads or
/// presents it, and no consistent formatting can be expected.
///
/// There can be multiple methods per file, such as a chromatography
/// method and a mass spectrometry method. The chromatography method
/// is usually the 0th method and the mass spectrometry method is the
/// 1st method.
pub struct InstrumentMethod {
    data: RawVec<u8>,
}

impl InstrumentMethod {
    pub fn new(data: RawVec<u8>) -> Self {
        Self { data }
    }

    /// Check that the buffer is a valid `InstrumentMethodT`
    pub fn check(&self) -> bool {
        root::<InstrumentMethodT>(&self.data).is_ok()
    }

    /// View the underlying buffer as a `InstrumentMethodT`
    pub fn view(&self) -> InstrumentMethodT<'_> {
        root::<InstrumentMethodT>(&self.data).unwrap()
    }

    pub fn index(&self) -> u8 {
        self.view().index()
    }

    pub fn text(&self) -> Option<&str> {
        self.view().text()
    }
}

/// Describes a chromatogram, which is a signal over time.
///
/// The time unit is always in *minutes*, but the signal intensity's
/// unit depends upon the trace type, `TraceTypeT`.
pub struct ChromatogramDescription {
    data: RawVec<u8>,
}

impl ChromatogramDescription {
    pub fn new(data: RawVec<u8>) -> Self {
        Self { data }
    }

    /// Check that the buffer is a valid `ChromatogramDescriptionT`
    pub fn check(&self) -> bool {
        root::<ChromatogramDescriptionT>(&self.data).is_ok()
    }

    /// View the underlying buffer as a `ChromatogramDescriptionT`
    pub fn view(&self) -> ChromatogramDescriptionT<'_> {
        root::<ChromatogramDescriptionT>(&self.data).unwrap()
    }

    /// Read the trace type
    pub fn trace_type(&self) -> TraceType {
        self.view().trace_type().into()
    }

    /// The scan number the chromatogram starts at
    pub fn start_index(&self) -> usize {
        (self.view().start_index() as usize).saturating_sub(1)
    }

    /// The scan number the chromatogram ends at
    pub fn end_index(&self) -> usize {
        self.view().end_index() as usize
    }

    /// Access the time-intensity array pair
    pub fn data(&self) -> Option<ChromatogramData<'_>> {
        let view = self.view();
        if let Some(data_view) = view.data() {
            Some(ChromatogramData {
                time: data_view.time().unwrap(),
                intensity: data_view.intensity().unwrap(),
            })
        } else {
            None
        }
    }
}

/// Describes a scan acquisition.
///
/// Acts as a wrapper around [`AcquisitionT`] that translates
/// raw FlatBuffer encodings.
pub struct Acquisition<'a> {
    data: AcquisitionT<'a>,
}

impl<'a> Acquisition<'a> {
    pub fn new(data: AcquisitionT<'a>) -> Self {
        Self { data }
    }

    #[inline(always)]
    pub fn low_mz(&self) -> f64 {
        self.data.low_mz()
    }

    #[inline(always)]
    pub fn high_mz(&self) -> f64 {
        self.data.high_mz()
    }

    #[inline(always)]
    pub fn injection_time(&self) -> f32 {
        self.data.injection_time()
    }

    #[inline(always)]
    pub fn compensation_voltage(&self) -> Option<f32> {
        self.data.compensation_voltages()?.iter().next()
    }

    #[inline(always)]
    pub fn compensation_voltages(&self) -> Option<Vec<f32>> {
        self.data.compensation_voltages().map(|v| v.iter().collect())
    }

    #[inline(always)]
    pub fn mass_analyzer(&self) -> MassAnalyzer {
        self.data.mass_analyzer().0.into()
    }

    #[inline(always)]
    pub fn scan_event(&self) -> i32 {
        self.data.scan_event()
    }

    #[inline(always)]
    pub fn ionization_mode(&self) -> IonizationMode {
        self.data.ionization_mode().0.into()
    }

    #[inline(always)]
    pub fn resolution(&self) -> Option<f32> {
        self.data.resolution()
    }
}

/// A collection of time series information describing the instrument run
pub struct StatusLogCollection {
    data: RawVec<u8>,
}

impl StatusLogCollection {
    pub fn new(data: RawVec<u8>) -> Self {
        Self { data }
    }

    /// Check that the buffer is a valid `StatusLogCollectionT`
    pub fn check(&self) -> bool {
        root::<StatusLogCollectionT>(&self.data).is_ok()
    }

    /// View the underlying buffer as a `StatusLogCollectionT`
    pub fn view(&self) -> StatusLogCollectionT<'_> {
        root::<StatusLogCollectionT>(&self.data).unwrap()
    }

    pub fn str_logs(&self) -> impl Iterator<Item=StatusLog<'_, flatbuffers::ForwardsUOffset<&'_ str>>> {
        self.view().string_logs().into_iter().flatten().map(|log| {
            let name= log.name().map(|name| name.to_string()).unwrap_or_default();
            let time = log.times().unwrap_or_default();
            let values: Vector<'_, flatbuffers::ForwardsUOffset<&str>> = log.values().unwrap_or_default();
            StatusLog {
                name, times: time, values
            }
        })
    }

    pub fn int_logs(&self) -> impl Iterator<Item=StatusLog<'_, i64>> {
        self.view().int_logs().into_iter().flatten().map(|log| {
            let name= log.name().map(|name| name.to_string()).unwrap_or_default();
            let time = log.times().unwrap_or_default();
            let values = log.values().unwrap_or_default();
            StatusLog {
                name, times: time, values
            }
        })
    }

    pub fn float_logs(&self) -> impl Iterator<Item=StatusLog<'_, f64>> {
        self.view().float_logs().into_iter().flatten().map(|log| {
            let name= log.name().map(|name| name.to_string()).unwrap_or_default();
            let time = log.times().unwrap_or_default();
            let values = log.values().unwrap_or_default();
            StatusLog {
                name, times: time, values
            }
        })
    }

    pub fn bool_logs(&self) -> impl Iterator<Item=StatusLog<'_, bool>> {
        self.view().bool_logs().into_iter().flatten().map(|log| {
            let name= log.name().map(|name| name.to_string()).unwrap_or_default();
            let time = log.times().unwrap_or_default();
            let values = log.values().unwrap_or_default();
            StatusLog {
                name, times: time, values
            }
        })
    }
}

pub struct StatusLog<'a, T> {
    pub name: String,
    times: Vector<'a, f64>,
    values: Vector<'a, T>
}

impl<'a, T> StatusLog<'a, T> {
    pub fn new(name: String, time: Vector<'a, f64>, values: Vector<'a, T>) -> Self {
        Self { name, times: time, values }
    }

    pub fn raw_values(&self) -> &Vector<'_, T> {
        &self.values
    }

    pub fn times(&self) -> Cow<'_, [f64]> {
        let data = &self.times;
        #[cfg(target_endian = "big")]
        return Cow::Owned(data.iter().copied().collect());
        #[cfg(target_endian = "little")]
        return Cow::Borrowed(bytemuck::cast_slice(data.bytes()));
    }
}

impl<'a> StatusLog<'a, flatbuffers::ForwardsUOffset<&str>> {
    pub fn strings(&self) -> &Vector<'_, flatbuffers::ForwardsUOffset<&str>> {
        &self.values
    }

    pub fn iter_strings(&self) -> impl Iterator<Item=(f64, &str)> {
        self.times.iter().zip(
            self.strings().iter()
        )
    }
}

impl<'a> StatusLog<'a, bool> {
    pub fn flags(&self) -> &Vector<'_, bool> {
        &self.values
    }

    pub fn iter_flags(&self) -> impl Iterator<Item=(f64, bool)> + '_ {
        self.times.iter().zip(
            self.flags().iter()
        )
    }
}

impl<'a, T: bytemuck::Pod> StatusLog<'a, T> {
    pub fn values(&self) -> Cow<'a, [T]> {
        let data = &self.values;
        #[cfg(target_endian = "big")]
        return Cow::Owned(data.iter().copied().collect());
        #[cfg(target_endian = "little")]
        return Cow::Borrowed(bytemuck::cast_slice(data.bytes()));
    }

    pub fn iter(&self) -> impl Iterator<Item=(f64, T)> + '_ where for<'t> T: flatbuffers::Follow<'t, Inner=T> {
        self.times.iter().zip(self.values.iter())
    }
}

/// A wrapper around a .NET `RawFileReader` instance. It carries a reference to a
/// .NET runtime and a FFI pointer to access data through. The dotnet runtime is
/// controlled via locks and is expected to be thread-safe.
///
/// This object's lifetime controls a shared resource in the .NET runtime.
pub struct RawFileReader {
    /// The token controlling the `RawFileReader` this object references
    raw_file_reader: *mut c_void,
    /// A reference to the .NET runtime
    context: Arc<AssemblyDelegateLoader>,
    /// A cache for the number of spectra in the RAW file
    size: usize,
    include_signal: bool,
    centroid_spectra: bool,
    /// A FFI function pointer to get spectra through.
    vget: ManagedFunction<extern "system" fn(*mut c_void, i32, i32, i32) -> RawVec<u8>>,
}

unsafe impl Send for RawFileReader {}
unsafe impl Sync for RawFileReader {}

impl Drop for RawFileReader {
    fn drop(&mut self) {
        self.close()
    }
}

impl Debug for RawFileReader {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RawFileReader")
            .field("raw_file_reader", &self.raw_file_reader)
            .field("context", &"?")
            .field("size", &self.size)
            .field("include_signal", &self.include_signal)
            .field("centroid_spectra", &self.centroid_spectra)
            .finish()
    }
}

impl RawFileReader {
    /// Open a ThermoFisher RAW file from a path. This may also create the .NET runtime
    /// if this is the first time it was called.
    pub fn open<P: Into<PathBuf>>(path: P) -> io::Result<Self> {
        let context = try_get_runtime().map_err(|e| match e {
            dotnetrawfilereader_sys::DotNetRuntimeCreationError::FailedToWriteDLLBundle(r) => {
                io::Error::new(io::ErrorKind::Other, r)
            }
            dotnetrawfilereader_sys::DotNetRuntimeCreationError::LoadHostfxrError(r) => {
                io::Error::new(io::ErrorKind::Other, r)
            }
            dotnetrawfilereader_sys::DotNetRuntimeCreationError::HostingError(r) => {
                io::Error::new(io::ErrorKind::Other, r)
            }
            dotnetrawfilereader_sys::DotNetRuntimeCreationError::IOError(r) => r,
        })?;
        let open_fn = context.get_function_with_unmanaged_callers_only::<fn(text_ptr: *const u8, text_length: i32) -> *mut c_void>(
            pdcstr!("librawfilereader.Exports, librawfilereader"),
            pdcstr!("Open")
        ).unwrap();
        let path: PathBuf = path.into();
        let path = path.to_string_lossy().to_string();
        let raw_file_reader = open_fn(path.as_ptr(), path.len() as i32);

        let buffer_fn = context
            .get_function_with_unmanaged_callers_only::<fn(*mut c_void, i32, i32, i32) -> RawVec<u8>>(
                pdcstr!("librawfilereader.Exports, librawfilereader"),
                pdcstr!("SpectrumDescriptionForWithOptions"),
            )
            .unwrap();

        let mut handle = Self {
            raw_file_reader,
            context,
            include_signal: true,
            centroid_spectra: false,
            size: 0,
            vget: buffer_fn,
        };

        match &handle.status() {
            RawFileReaderError::Ok => {}
            RawFileReaderError::FileNotFound => {
                return Err(io::Error::new(io::ErrorKind::NotFound, "File not found"))
            }
            RawFileReaderError::InvalidFormat => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!(
                        "File does not appear to be a valid RAW file. {}",
                        handle.error_message().unwrap_or_default()
                    ),
                ))
            }
            RawFileReaderError::Error | RawFileReaderError::HandleNotFound => {
                return Err(io::Error::new(
                    io::ErrorKind::Other,
                    format!(
                        "An unknown error occured {}",
                        handle.error_message().unwrap_or_default()
                    ),
                ))
            }
        }

        handle.size = handle._impl_len();

        Ok(handle)
    }

    /// Get the scan number of the first spectrum
    pub fn first_spectrum(&self) -> i32 {
        self.validate_impl();
        let index_fn = self
            .context
            .get_function_with_unmanaged_callers_only::<fn(*mut c_void) -> i32>(
                pdcstr!("librawfilereader.Exports, librawfilereader"),
                pdcstr!("FirstSpectrum"),
            )
            .unwrap();
        index_fn(self.raw_file_reader)
    }

    /// Get the scan number of the last spectrum
    pub fn last_spectrum(&self) -> i32 {
        let index_fn = self
            .context
            .get_function_with_unmanaged_callers_only::<fn(*mut c_void) -> i32>(
                pdcstr!("librawfilereader.Exports, librawfilereader"),
                pdcstr!("LastSpectrum"),
            )
            .unwrap();
        index_fn(self.raw_file_reader)
    }

    /// Get whether or not to retrieve the spectrum signal data when retrieving spectra
    pub fn get_signal_loading(&self) -> bool {
        self.include_signal
    }

    /// Set whether or not to retrieve the spectrum signal data when retrieving spectra
    pub fn set_signal_loading(&mut self, value: bool) {
        self.include_signal = value;
    }

    /// Get whether or not to centroid spectra if they were stored in profile mode
    pub fn get_centroid_spectra(&self) -> bool {
        self.centroid_spectra
    }

    /// Set whether or not to centroid spectra if they were stored in profile mode
    pub fn set_centroid_spectra(&mut self, value: bool) {
        self.centroid_spectra = value;
    }

    /// Get a [`InstrumentModel`] message describing the instrument configuration used
    /// to acquire the RAW file.
    pub fn instrument_model(&self) -> InstrumentModel {
        self.validate_impl();
        let instrument_fn = self
            .context
            .get_function_with_unmanaged_callers_only::<fn(*mut c_void) -> RawVec<u8>>(
                pdcstr!("librawfilereader.Exports, librawfilereader"),
                pdcstr!("InstrumentModel"),
            )
            .unwrap();
        let buf = instrument_fn(self.raw_file_reader);
        root::<InstrumentModelT>(&buf).unwrap();
        InstrumentModel::new(buf)
    }

    /// Retrieve descriptive metadata about the file and summary measures
    pub fn file_description(&self) -> FileDescription {
        self.validate_impl();
        let descr_fn = self
            .context
            .get_function_with_unmanaged_callers_only::<fn(*mut c_void) -> RawVec<u8>>(
                pdcstr!("librawfilereader.Exports, librawfilereader"),
                pdcstr!("FileDescription"),
            )
            .unwrap();
        let buf = descr_fn(self.raw_file_reader);
        root::<FileDescriptionT>(&buf).unwrap();
        FileDescription::new(buf)
    }

    /// Read the `index`-th instrument method.
    ///
    /// If no instrument method is found, the Thermo library returns an
    /// empty string. Instead, this returns `None`.
    pub fn instrument_method(&self, index: u8) -> Option<InstrumentMethod> {
        self.validate_impl();
        let descr_fn = self
            .context
            .get_function_with_unmanaged_callers_only::<fn(*mut c_void, i32) -> RawVec<u8>>(
                pdcstr!("librawfilereader.Exports, librawfilereader"),
                pdcstr!("InstrumentMethod"),
            )
            .unwrap();
        let buf = descr_fn(self.raw_file_reader, index as i32);
        root::<InstrumentMethodT>(&buf).unwrap();
        let method = InstrumentMethod::new(buf);
        if method.text().is_none() || method.text().is_some_and(|s| s.is_empty()) {
            None
        } else {
            Some(method)
        }
    }

    /// Get the number of instrument methods that are present in the file
    pub fn instrument_method_count(&self) -> usize {
        self.validate_impl();
        let descr_fn = self
            .context
            .get_function_with_unmanaged_callers_only::<fn(*mut c_void) -> u32>(
                pdcstr!("librawfilereader.Exports, librawfilereader"),
                pdcstr!("InstrumentMethodCount"),
            )
            .unwrap();
        let n = descr_fn(self.raw_file_reader);
        n as usize
    }

    /// Read the total ion current chromatogram spanning the entire MS run
    pub fn tic(&self) -> ChromatogramDescription {
        self.validate_impl();
        let descr_fn = self
            .context
            .get_function_with_unmanaged_callers_only::<fn(*mut c_void) -> RawVec<u8>>(
                pdcstr!("librawfilereader.Exports, librawfilereader"),
                pdcstr!("GetTIC"),
            )
            .unwrap();
        let buf = descr_fn(self.raw_file_reader);
        ChromatogramDescription::new(buf)
    }

    /// Read the base peak current chromatogram spanning the entire MS run
    pub fn bpc(&self) -> ChromatogramDescription {
        self.validate_impl();
        let descr_fn = self
            .context
            .get_function_with_unmanaged_callers_only::<fn(*mut c_void) -> RawVec<u8>>(
                pdcstr!("librawfilereader.Exports, librawfilereader"),
                pdcstr!("GetBPC"),
            )
            .unwrap();
        let buf = descr_fn(self.raw_file_reader);
        ChromatogramDescription::new(buf)
    }

    #[inline]
    fn validate_impl(&self) {
        if self.raw_file_reader.is_null() {
            panic!("Internal handle already closed.")
        }
    }

    fn _impl_len(&self) -> usize {
        self.validate_impl();
        let index_fn = self
            .context
            .get_function_with_unmanaged_callers_only::<fn(*mut c_void) -> i32>(
                pdcstr!("librawfilereader.Exports, librawfilereader"),
                pdcstr!("SpectrumCount"),
            )
            .unwrap();
        index_fn(self.raw_file_reader) as usize
    }

    #[inline]
    /// Get the number of spectra in the RAW file
    pub fn len(&self) -> usize {
        if self.size != 0 {
            self.size
        } else {
            self._impl_len()
        }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Close the RAW file, releasing resources held by the .NET
    /// runtime. This places the object in an unusable state.
    ///
    /// This method is called on `drop`.
    fn close(&mut self) {
        if !self.raw_file_reader.is_null() {
            let close_fn = self
                .context
                .get_function_with_unmanaged_callers_only::<fn(*mut c_void)>(
                    pdcstr!("librawfilereader.Exports, librawfilereader"),
                    pdcstr!("Close"),
                )
                .unwrap();
            close_fn(self.raw_file_reader);
            self.raw_file_reader = ptr::null_mut();
        }
    }

    /// Get the spectrum at index `index`
    ///
    /// **Note**: The index of a spectrum is one less than its scan number
    pub fn get(&self, index: usize) -> Option<RawSpectrum> {
        if index >= self.len() {
            return None;
        }
        self.validate_impl();
        let buffer_fn = &self.vget;
        let buffer = buffer_fn(
            self.raw_file_reader,
            (index as i32) + 1,
            self.include_signal as i32,
            self.centroid_spectra as i32,
        );
        Some(RawSpectrum::new(buffer))
    }

    /// Retrieve extra signal information like the baseline, charge and noise
    /// supplemental arrays for a spectrum.
    ///
    /// ## Note
    /// This method is experimental and may not work reliably.
    pub fn get_extended_spectrum_data(&self, index: usize, include_sampled_noise: bool) -> Option<ExtendedSpectrumData> {
        if index >= self.len() {
            return None;
        }
        self.validate_impl();

        let buffer_fn = self
            .context
            .get_function_with_unmanaged_callers_only::<fn(*mut c_void, i32, i32) -> RawVec<u8>>(
                pdcstr!("librawfilereader.Exports, librawfilereader"),
                pdcstr!("AdvancedPacketDataFor"),
            )
            .unwrap();

        let buff = buffer_fn(self.raw_file_reader, (index as i32) + 1, include_sampled_noise as i32);
        Some(ExtendedSpectrumData::new(buff))
    }

    /// Read spectrum signal data separately and explicitly without reading all related metadata
    pub fn get_spectrum_data(&self, index: usize, centroid_spectra: bool) -> Option<OwnedSpectrumData> {
        if index >= self.len() {
            return None;
        }
        self.validate_impl();
        let buffer_fn = self.context.get_function_with_unmanaged_callers_only::<fn(*mut c_void, i32, i32) -> RawVec<u8>>(
            pdcstr!("librawfilereader.Exports, librawfilereader"),
            pdcstr!("SpectrumDataFor")
        ).unwrap();

        let buff = buffer_fn(self.raw_file_reader, (index as i32) + 1, centroid_spectra as i32);
        Some(OwnedSpectrumData::new(buff))
    }

    /// Get the trailer extra values for scan at `index`.
    ///
    /// The trailer extra values are key-value pairs whose exact meaning
    /// is under dependent upon the instrument and method used. All values
    /// are passed as strings which must be parsed into the appropriate Rust
    /// type to be useful.
    pub fn get_raw_trailers_for(&self, index: usize) -> Option<TrailerValues> {
        if index >= self.len() {
            return None;
        }
        self.validate_impl();

        let buffer_fn = self
            .context
            .get_function_with_unmanaged_callers_only::<fn(*mut c_void, i32) -> RawVec<u8>>(
                pdcstr!("librawfilereader.Exports, librawfilereader"),
                pdcstr!("GetRawTrailerValuesFor"),
            )
            .unwrap();

        let buff = buffer_fn(self.raw_file_reader, (index as i32) + 1);
        Some(TrailerValues::new(buff))
    }

    pub fn get_status_logs(&self) -> Option<StatusLogCollection> {
        self.validate_impl();

        let descr_fn = self
            .context
            .get_function_with_unmanaged_callers_only::<fn(*mut c_void) -> RawVec<u8>>(
                pdcstr!("librawfilereader.Exports, librawfilereader"),
                pdcstr!("GetStatusLogs"),
            )
            .unwrap();

        let buff = descr_fn(self.raw_file_reader);
        Some(StatusLogCollection::new(buff))
    }

    /// A utility for debugging, get a spectrum and access some of its fields, printing them
    /// to `STDOUT`
    pub fn describe(&self, index: usize) {
        if let Some(buf) = self.get(index) {
            let descr = buf.view();
            println!(
                "{}|{:?} -> {:?} | has data? {}",
                descr.index(),
                descr.polarity(),
                descr.precursor(),
                descr.data().is_some()
            );
            println!("Filter: {}", descr.filter_string().unwrap());
            let acq = descr.acquisition().unwrap();
            println!(
                "{:?} {:?} {}-{}",
                acq.mass_analyzer(),
                acq.ionization_mode(),
                acq.low_mz(),
                acq.high_mz()
            );
            if let Some(dat) = descr.data() {
                let intens_opt = dat.intensity();
                let intens = intens_opt.as_ref().unwrap();
                let val = intens.iter().max_by(|a, b| a.total_cmp(b)).unwrap();
                println!(
                    "Received {} data points, base peak intensity {}",
                    intens.len(),
                    val
                );
            }
        } else {
            println!("Spectrum at {index}/{} not found", self.len())
        }
    }

    /// Create an iterator over the RAW file, reading successive spectra
    pub fn iter(&self) -> RawFileReaderIter<'_> {
        RawFileReaderIter::new(self)
    }

    /// Retrieve the status of the .NET `RawFileReader`
    pub fn status(&self) -> RawFileReaderError {
        self.validate_impl();
        let status_fn = self
            .context
            .get_function_with_unmanaged_callers_only::<fn(*mut c_void) -> u32>(
                pdcstr!("librawfilereader.Exports, librawfilereader"),
                pdcstr!("Status"),
            )
            .unwrap();
        let code = status_fn(self.raw_file_reader);
        code.into()
    }

    /// Retrieve the "file error" status message. This message may
    /// or may not be meaningful depending upon what went wrong.
    pub fn error_message(&self) -> Option<String> {
        self.validate_impl();
        let status_fn = self
            .context
            .get_function_with_unmanaged_callers_only::<fn(*mut c_void) -> RawVec<u8>>(
                pdcstr!("librawfilereader.Exports, librawfilereader"),
                pdcstr!("GetErrorMessageFor"),
            )
            .unwrap();
        let result = status_fn(self.raw_file_reader);
        if result.len() == 0 || result.len() == 1 && result[0] == 0 {
            return None;
        }
        let message =
            String::from_utf8(result.to_vec()).expect("Failed to decode message, invalid UTF8");
        Some(message)
    }
}

/// Iterator for [`RawFileReader`]
#[derive(Debug)]
pub struct RawFileReaderIter<'a> {
    handle: &'a RawFileReader,
    index: usize,
    size: usize,
}

unsafe impl<'a> Send for RawFileReaderIter<'a> {}

impl<'a> RawFileReaderIter<'a> {
    fn new(handle: &'a RawFileReader) -> Self {
        let size = handle.len();
        Self {
            handle,
            index: 0,
            size,
        }
    }
}

impl<'a> Iterator for RawFileReaderIter<'a> {
    type Item = RawSpectrum;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index < self.size {
            let buffer = self.handle.get(self.index);
            self.index += 1;
            buffer
        } else {
            None
        }
    }
}


/// IntoIterator for [`RawFileReader`]
#[derive(Debug)]
pub struct RawFileReaderIntoIter {
    handle: RawFileReader,
    index: usize,
    size: usize,
}

impl RawFileReaderIntoIter {
    fn new(handle: RawFileReader) -> Self {
        let size = handle.len();
        Self {
            handle,
            index: 0,
            size,
        }
    }
}

impl Iterator for RawFileReaderIntoIter {
    type Item = RawSpectrum;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index < self.size {
            let buffer = self.handle.get(self.index);
            self.index += 1;
            buffer
        } else {
            None
        }
    }
}

impl IntoIterator for RawFileReader {
    type Item = RawSpectrum;

    type IntoIter = RawFileReaderIntoIter;

    fn into_iter(self) -> Self::IntoIter {
        RawFileReaderIntoIter::new(self)
    }
}

impl<'a> IntoIterator for &'a RawFileReader {
    type Item = RawSpectrum;

    type IntoIter = RawFileReaderIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a> FusedIterator for RawFileReaderIter<'a> {}

impl<'a> ExactSizeIterator for RawFileReaderIter<'a> {
    fn len(&self) -> usize {
        self.size
    }
}

impl FusedIterator for RawFileReaderIntoIter {}

impl ExactSizeIterator for RawFileReaderIntoIter {
    fn len(&self) -> usize {
        self.size
    }
}


#[cfg(test)]
mod test {
    use std::io;

    use super::*;

    #[test]
    fn test_read() -> io::Result<()> {
        let handle = RawFileReader::open("../tests/data/small.RAW")?;

        assert_eq!(handle.len(), 48);
        let buf = handle.get(5).unwrap();
        let descr = buf.view();
        assert_eq!(descr.index(), 5);

        assert_eq!(buf.mode(), SpectrumMode::Centroid);
        let dat = handle.get_spectrum_data(5, false).unwrap();
        assert_eq!(dat.len(), 0);
        let dat = handle.get_spectrum_data(5, true).unwrap();
        assert_eq!(dat.len(), buf.data().unwrap().len());

        Ok(())
    }

    #[test]
    fn test_read_trailers() -> io::Result<()> {
        let handle = RawFileReader::open("../tests/data/small.RAW")?;

        assert_eq!(handle.len(), 48);

        let trailers = handle.get_raw_trailers_for(5).unwrap();
        assert!(trailers.iter().all(|kv| !kv.label.ends_with(':')));
        assert_eq!(trailers.len(), 26);

        Ok(())
    }

    #[test]
    fn test_read_512kb() -> io::Result<()> {
        let handle = RawFileReader::open("../tests/data/small.RAW")?;

        assert_eq!(handle.len(), 48);
        let buf = handle.get(1).unwrap();
        let descr = buf.view();
        assert_eq!(descr.index(), 1);

        Ok(())
    }

    #[test]
    fn test_iter() -> io::Result<()> {
        let handle = RawFileReader::open("../tests/data/small.RAW")?;

        let (m1, mn) =
            handle
                .iter()
                .map(|s| s.view().ms_level())
                .fold((0, 0), |(m1, mn), level| {
                    if level > 1 {
                        (m1, mn + 1)
                    } else {
                        (m1 + 1, mn)
                    }
                });

        assert_eq!(m1 + mn, 48);
        assert_eq!(m1, 14);
        assert_eq!(mn, 34);
        Ok(())
    }

    #[test]
    fn test_tic() -> io::Result<()> {
        let handle = RawFileReader::open("../tests/data/small.RAW")?;
        let tic = handle.tic();
        assert_eq!(tic.trace_type(), TraceType::TIC);
        assert_eq!(tic.start_index(), 0);
        assert_eq!(tic.end_index(), 48);
        let data = tic.data().unwrap();
        assert_eq!(data.time().len(), 48);
        assert_eq!(data.intensity().len(), 48);
        let expected = 196618480.0f32;
        let total = data.intensity().iter().sum::<f32>();
        assert!(
            (expected - total).abs() < 1e-3,
            "sum = {total}, expected {expected} ({})",
            (expected - total).abs()
        );
        Ok(())
    }

    #[test]
    fn test_bpc() -> io::Result<()> {
        let handle = RawFileReader::open("../tests/data/small.RAW")?;
        let bpc = handle.bpc();
        assert_eq!(bpc.trace_type(), TraceType::BasePeak);
        assert_eq!(bpc.start_index(), 0);
        assert_eq!(bpc.end_index(), 48);
        let data = bpc.data().unwrap();
        assert_eq!(data.time().len(), 48);
        assert_eq!(data.intensity().len(), 48);
        let expected = 16132207.0f32;
        let total = data.intensity().iter().sum::<f32>();
        assert!(
            (expected - total).abs() < 1e-3,
            "sum = {total}, expected {expected} ({})",
            (expected - total).abs()
        );
        Ok(())
    }

    #[test]
    fn test_instrument_model() -> io::Result<()> {
        let handle = RawFileReader::open("../tests/data/small.RAW")?;
        let model = handle.instrument_model();

        assert_eq!(model.model(), Some("LTQ FT"));

        Ok(())
    }

    #[test]
    fn test_file_description() -> io::Result<()> {
        let handle = RawFileReader::open("../tests/data/small.RAW")?;
        let fd = handle.file_description();
        assert_eq!(fd.sample_id(), Some("1"));
        assert_eq!(fd.source_file(), Some("../tests/data/small.RAW"));
        let counts = fd.spectra_per_ms_level().unwrap();
        assert_eq!(counts.get(0), 14);
        assert_eq!(counts.get(1), 34);
        Ok(())
    }

    #[test]
    fn test_open_unicode_filename() -> io::Result<()> {
        let handle = RawFileReader::open("../tests/data/small_µ.RAW")?;
        let fd = handle.file_description();
        assert_eq!(fd.sample_id(), Some("1"));
        assert_eq!(fd.source_file(), Some("../tests/data/small_µ.RAW"));
        let counts = fd.spectra_per_ms_level().unwrap();
        assert_eq!(counts.get(0), 14);
        assert_eq!(counts.get(1), 34);
        Ok(())
    }

    #[test]
    fn test_fail_gracefully_opening_non_raw_file() {
        assert!(RawFileReader::open("../test/data/small.mgf").is_err())
    }

    #[test]
    fn test_status_logs() -> io::Result<()> {
        let handle = RawFileReader::open("../tests/data/small.RAW")?;
        let logs = handle.get_status_logs().unwrap();
        assert!(logs.check());
        let view = logs.view();

        let float_stats = view.float_logs().unwrap();
        for stat_log in float_stats.iter() {
            let times = stat_log.times().unwrap();
            let vals = stat_log.values().unwrap();
            // eprintln!("{} {} {:?} {:?}", stat_log.name().unwrap(), times.len(), times.get(0), times.iter().last());
            assert!(times.len() > 0);
            assert_eq!(times.len(), vals.len());
        }

        let str_stats = view.string_logs().unwrap();
        for stat_log in str_stats.iter() {
            let times = stat_log.times().unwrap();
            let vals = stat_log.values().unwrap();
            // eprintln!("{} {} {:?} {:?}", stat_log.name().unwrap(), times.len(), vals.get(0), vals.iter().last());
            assert!(times.len() > 0);
            assert_eq!(times.len(), vals.len());
        }
        Ok(())
    }
}