#![allow(dead_code)]
use std::collections::HashMap;
use super::dataset::Hdf5Dataset;
use super::types::{DataStorage, Hdf5Error, Hdf5Result};
#[derive(Debug, Clone)]
pub struct CompoundRecord {
pub fields: HashMap<String, f64>,
}
impl CompoundRecord {
pub fn new() -> Self {
Self {
fields: HashMap::new(),
}
}
pub fn set(&mut self, field: &str, value: f64) {
self.fields.insert(field.to_string(), value);
}
pub fn get(&self, field: &str) -> Hdf5Result<f64> {
self.fields
.get(field)
.copied()
.ok_or_else(|| Hdf5Error::NotFound(format!("field '{field}'")))
}
}
impl Default for CompoundRecord {
fn default() -> Self {
Self::new()
}
}
pub fn write_compound_records(
ds: &mut Hdf5Dataset,
records: Vec<CompoundRecord>,
) -> Hdf5Result<()> {
let maps: Vec<HashMap<String, f64>> = records.into_iter().map(|r| r.fields).collect();
ds.data = DataStorage::Compound(maps);
Ok(())
}
pub fn read_compound_records(ds: &Hdf5Dataset) -> Hdf5Result<Vec<CompoundRecord>> {
match &ds.data {
DataStorage::Compound(maps) => Ok(maps
.iter()
.map(|m| CompoundRecord { fields: m.clone() })
.collect()),
_ => Err(Hdf5Error::Generic(
"dataset is not compound type".to_string(),
)),
}
}