dmap/formats/
dmap.rs

1//! The generic [DMAP file format](https://radar-software-toolkit-rst.readthedocs.io/en/latest/references/general/dmap_data/).
2//!
3//! Defines [`DmapRecord`] which implements [`Record`], which can be used
4//! for reading/writing DMAP files without checking that certain fields are or
5//! are not present, or have a given type.
6
7use crate::error::DmapError;
8use crate::record::Record;
9use crate::types::{DmapField, DmapType};
10use indexmap::IndexMap;
11
12#[derive(Debug, PartialEq, Clone)]
13pub struct DmapRecord {
14    pub data: IndexMap<String, DmapField>,
15}
16
17impl Record<'_> for DmapRecord {
18    fn inner(self) -> IndexMap<String, DmapField> {
19        self.data
20    }
21    fn get(&self, key: &str) -> Option<&DmapField> {
22        self.data.get(key)
23    }
24    fn keys(&self) -> Vec<&String> {
25        self.data.keys().collect()
26    }
27    fn new(fields: &mut IndexMap<String, DmapField>) -> Result<DmapRecord, DmapError> {
28        Ok(DmapRecord {
29            data: fields.to_owned(),
30        })
31    }
32    fn to_bytes(&self) -> Result<Vec<u8>, DmapError> {
33        let mut data_bytes: Vec<u8> = vec![];
34        let mut num_scalars: i32 = 0;
35        let mut num_vectors: i32 = 0;
36
37        // Do a first pass, to get all the scalar fields
38        for (name, val) in self.data.iter() {
39            if let x @ DmapField::Scalar(_) = val {
40                data_bytes.extend(name.as_bytes());
41                data_bytes.extend([0]); // null-terminate string
42                data_bytes.append(&mut x.as_bytes());
43                num_scalars += 1;
44            }
45        }
46        // Do a second pass to convert all the vector fields
47        for (name, val) in self.data.iter() {
48            if let x @ DmapField::Vector(_) = val {
49                data_bytes.extend(name.as_bytes());
50                data_bytes.extend([0]); // null-terminate string
51                data_bytes.append(&mut x.as_bytes());
52                num_vectors += 1;
53            }
54        }
55        let mut bytes: Vec<u8> = vec![];
56        bytes.extend((65537_i32).as_bytes()); // No idea why this is what it is, copied from backscatter
57        bytes.extend((data_bytes.len() as i32 + 16).as_bytes()); // +16 for code, length, num_scalars, num_vectors
58        bytes.extend(num_scalars.as_bytes());
59        bytes.extend(num_vectors.as_bytes());
60        bytes.append(&mut data_bytes); // consumes data_bytes
61        Ok(bytes)
62    }
63}
64
65impl TryFrom<&mut IndexMap<String, DmapField>> for DmapRecord {
66    type Error = DmapError;
67
68    fn try_from(value: &mut IndexMap<String, DmapField>) -> Result<Self, Self::Error> {
69        DmapRecord::new(value)
70    }
71}
72
73impl TryFrom<IndexMap<String, DmapField>> for DmapRecord {
74    type Error = DmapError;
75
76    fn try_from(mut value: IndexMap<String, DmapField>) -> Result<Self, Self::Error> {
77        DmapRecord::new(&mut value)
78    }
79}