use crate::error::DmapError;
use crate::record::Record;
use crate::types::{DmapField, DmapType};
use indexmap::IndexMap;
#[derive(Debug, PartialEq, Clone)]
pub struct DmapRecord {
pub data: IndexMap<String, DmapField>,
}
impl Record<'_> for DmapRecord {
fn inner(self) -> IndexMap<String, DmapField> {
self.data
}
fn get(&self, key: &str) -> Option<&DmapField> {
self.data.get(key)
}
fn keys(&self) -> Vec<&String> {
self.data.keys().collect()
}
fn new(fields: &mut IndexMap<String, DmapField>) -> Result<DmapRecord, DmapError> {
Ok(DmapRecord {
data: fields.to_owned(),
})
}
fn is_metadata_field(_name: &str) -> bool {
true
}
fn to_bytes(&self) -> Result<Vec<u8>, DmapError> {
let mut data_bytes: Vec<u8> = vec![];
let mut num_scalars: i32 = 0;
let mut num_vectors: i32 = 0;
for (name, val) in self.data.iter() {
if let x @ DmapField::Scalar(_) = val {
data_bytes.extend(name.as_bytes());
data_bytes.extend([0]); data_bytes.append(&mut x.as_bytes());
num_scalars += 1;
}
}
for (name, val) in self.data.iter() {
if let x @ DmapField::Vector(_) = val {
data_bytes.extend(name.as_bytes());
data_bytes.extend([0]); data_bytes.append(&mut x.as_bytes());
num_vectors += 1;
}
}
let mut bytes: Vec<u8> = vec![];
bytes.extend((65537_i32).as_bytes()); bytes.extend((data_bytes.len() as i32 + 16).as_bytes()); bytes.extend(num_scalars.as_bytes());
bytes.extend(num_vectors.as_bytes());
bytes.append(&mut data_bytes); Ok(bytes)
}
}
impl TryFrom<&mut IndexMap<String, DmapField>> for DmapRecord {
type Error = DmapError;
fn try_from(value: &mut IndexMap<String, DmapField>) -> Result<Self, Self::Error> {
DmapRecord::new(value)
}
}
impl TryFrom<IndexMap<String, DmapField>> for DmapRecord {
type Error = DmapError;
fn try_from(mut value: IndexMap<String, DmapField>) -> Result<Self, Self::Error> {
DmapRecord::new(&mut value)
}
}