1use 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 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]); data_bytes.append(&mut x.as_bytes());
43 num_scalars += 1;
44 }
45 }
46 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]); 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()); bytes.extend((data_bytes.len() as i32 + 16).as_bytes()); bytes.extend(num_scalars.as_bytes());
59 bytes.extend(num_vectors.as_bytes());
60 bytes.append(&mut data_bytes); 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}