use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq)]
pub enum Hdf5DataType {
Int8,
Int16,
Int32,
Int64,
UInt8,
UInt16,
UInt32,
UInt64,
Float32,
Float64,
FixedString(usize),
VarString,
Compound(Vec<(String, Hdf5DataType, usize)>),
Array(Box<Hdf5DataType>, Vec<usize>),
Opaque(usize),
Unknown(u8, usize),
}
impl Hdf5DataType {
pub fn element_size(&self) -> usize {
match self {
Self::Int8 | Self::UInt8 => 1,
Self::Int16 | Self::UInt16 => 2,
Self::Int32 | Self::UInt32 | Self::Float32 => 4,
Self::Int64 | Self::UInt64 | Self::Float64 => 8,
Self::FixedString(n) => *n,
Self::VarString => 16, Self::Compound(fields) => fields
.iter()
.map(|(_, dt, _)| dt.element_size())
.sum::<usize>()
.max(1),
Self::Array(elem, dims) => {
let count: usize = dims.iter().product();
elem.element_size() * count
}
Self::Opaque(n) => *n,
Self::Unknown(_, size) => *size,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Hdf5Value {
Int8(Vec<i8>),
Int16(Vec<i16>),
Int32(Vec<i32>),
Int64(Vec<i64>),
UInt8(Vec<u8>),
UInt16(Vec<u16>),
UInt32(Vec<u32>),
UInt64(Vec<u64>),
Float32(Vec<f32>),
Float64(Vec<f64>),
Strings(Vec<String>),
Raw(Vec<u8>),
}
impl Hdf5Value {
pub fn len(&self) -> usize {
match self {
Self::Int8(v) => v.len(),
Self::Int16(v) => v.len(),
Self::Int32(v) => v.len(),
Self::Int64(v) => v.len(),
Self::UInt8(v) => v.len(),
Self::UInt16(v) => v.len(),
Self::UInt32(v) => v.len(),
Self::UInt64(v) => v.len(),
Self::Float32(v) => v.len(),
Self::Float64(v) => v.len(),
Self::Strings(v) => v.len(),
Self::Raw(v) => v.len(),
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn as_f64(&self) -> Option<Vec<f64>> {
match self {
Self::Int8(v) => Some(v.iter().map(|x| *x as f64).collect()),
Self::Int16(v) => Some(v.iter().map(|x| *x as f64).collect()),
Self::Int32(v) => Some(v.iter().map(|x| *x as f64).collect()),
Self::Int64(v) => Some(v.iter().map(|x| *x as f64).collect()),
Self::UInt8(v) => Some(v.iter().map(|x| *x as f64).collect()),
Self::UInt16(v) => Some(v.iter().map(|x| *x as f64).collect()),
Self::UInt32(v) => Some(v.iter().map(|x| *x as f64).collect()),
Self::UInt64(v) => Some(v.iter().map(|x| *x as f64).collect()),
Self::Float32(v) => Some(v.iter().map(|x| *x as f64).collect()),
Self::Float64(v) => Some(v.clone()),
_ => None,
}
}
}
#[derive(Debug, Clone)]
pub struct Hdf5Attribute {
pub name: String,
pub dtype: Hdf5DataType,
pub value: Hdf5Value,
}
#[derive(Debug, Clone)]
pub struct Hdf5Dataset {
pub name: String,
pub path: String,
pub dtype: Hdf5DataType,
pub shape: Vec<usize>,
pub data: Hdf5Value,
pub attributes: HashMap<String, Hdf5Attribute>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Hdf5NodeType {
Group,
Dataset,
}
#[derive(Debug, Clone)]
pub struct Hdf5Node {
pub name: String,
pub path: String,
pub node_type: Hdf5NodeType,
}
#[derive(Debug, Clone)]
pub struct Hdf5Group {
pub name: String,
pub path: String,
pub children: Vec<String>,
pub nodes: Vec<Hdf5Node>,
pub attributes: HashMap<String, Hdf5Attribute>,
}