Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,090 changes: 1,090 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[workspace]
members = ["crates/fd5"]
resolver = "2"

[workspace.dependencies]
hdf5-metno = { version = "0.11.0", features = ["blosc-all", "lzf", "static", "zlib"] }
sha2 = "0.10"
serde = { version = "1", features = ["derive"] }
serde_json = "1.0"
17 changes: 17 additions & 0 deletions crates/fd5/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "fd5"
version = "0.1.0"
edition = "2021"
description = "Rust implementation of fd5 Merkle-tree hashing, verification, and editing"
license = "Apache-2.0"

[dependencies]
hdf5-metno = { workspace = true }
hdf5-metno-sys = "0.10.1"
sha2 = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = "2"

[dev-dependencies]
tempfile = "3"
154 changes: 154 additions & 0 deletions crates/fd5/src/attr_ser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
//! Deterministic attribute-to-bytes serialization.
//!
//! Must produce byte-identical output to Python's `_serialize_attr` (hash.py L76-85):
//!
//! - `str` → `.encode("utf-8")`
//! - `bytes` → as-is
//! - `np.ndarray` → `.tobytes()` (row-major C-order)
//! - `np.generic` → `np.array(value).tobytes()`
//! - fallback → `str(value).encode("utf-8")`
//!
//! In HDF5-metno, attributes arrive as typed values. We read raw bytes for
//! numeric types and UTF-8 for strings to match Python exactly.
//!
//! **Important**: Uses `read_raw` (not `read_1d`) for arrays because
//! attributes can be multi-dimensional (e.g. 4×4 affine matrices).

use hdf5_metno::types::{FloatSize, IntSize, TypeDescriptor, VarLenAscii, VarLenUnicode};
use hdf5_metno::Attribute;

use crate::error::Fd5Result;

/// Serialize an HDF5 attribute value to bytes, matching Python's `_serialize_attr`.
pub fn serialize_attr(attr: &Attribute) -> Fd5Result<Vec<u8>> {
let td = attr.dtype()?.to_descriptor()?;

if attr.is_scalar() {
serialize_scalar(attr, &td)
} else {
serialize_array(attr, &td)
}
}

fn serialize_scalar(attr: &Attribute, td: &TypeDescriptor) -> Fd5Result<Vec<u8>> {
match td {
// String types → UTF-8 bytes (matching Python str.encode("utf-8"))
TypeDescriptor::VarLenUnicode => {
let v: VarLenUnicode = attr.read_scalar()?;
Ok(v.as_str().as_bytes().to_vec())
}
TypeDescriptor::VarLenAscii => {
let v: VarLenAscii = attr.read_scalar()?;
Ok(v.as_str().as_bytes().to_vec())
}
TypeDescriptor::FixedAscii(_) | TypeDescriptor::FixedUnicode(_) => {
// Read raw, trim trailing nulls, return UTF-8
let raw = attr.read_raw::<u8>()?;
let s = String::from_utf8_lossy(&raw);
let trimmed = s.trim_end_matches('\0');
Ok(trimmed.as_bytes().to_vec())
}

// Numeric scalars → np.array(value).tobytes()
TypeDescriptor::Integer(int_size) => Ok(match int_size {
IntSize::U1 => attr.read_scalar::<i8>()?.to_ne_bytes().to_vec(),
IntSize::U2 => attr.read_scalar::<i16>()?.to_ne_bytes().to_vec(),
IntSize::U4 => attr.read_scalar::<i32>()?.to_ne_bytes().to_vec(),
IntSize::U8 => attr.read_scalar::<i64>()?.to_ne_bytes().to_vec(),
}),
TypeDescriptor::Unsigned(int_size) => Ok(match int_size {
IntSize::U1 => attr.read_scalar::<u8>()?.to_ne_bytes().to_vec(),
IntSize::U2 => attr.read_scalar::<u16>()?.to_ne_bytes().to_vec(),
IntSize::U4 => attr.read_scalar::<u32>()?.to_ne_bytes().to_vec(),
IntSize::U8 => attr.read_scalar::<u64>()?.to_ne_bytes().to_vec(),
}),
TypeDescriptor::Float(float_size) => Ok(match float_size {
FloatSize::U4 => attr.read_scalar::<f32>()?.to_ne_bytes().to_vec(),
FloatSize::U8 => attr.read_scalar::<f64>()?.to_ne_bytes().to_vec(),
}),
TypeDescriptor::Boolean => {
let v: bool = attr.read_scalar()?;
Ok(vec![v as u8])
}

// Fallback: str(value).encode("utf-8")
_ => {
let raw = attr.read_raw::<u8>()?;
Ok(raw)
}
}
}

/// Serialize a non-scalar attribute to bytes.
///
/// Uses `read_raw` to handle any dimensionality (1D, 2D, etc.).
fn serialize_array(attr: &Attribute, td: &TypeDescriptor) -> Fd5Result<Vec<u8>> {
match td {
TypeDescriptor::Integer(int_size) => Ok(match int_size {
IntSize::U1 => {
let v = attr.read_raw::<i8>()?;
v.iter().flat_map(|x| x.to_ne_bytes()).collect()
}
IntSize::U2 => {
let v = attr.read_raw::<i16>()?;
v.iter().flat_map(|x| x.to_ne_bytes()).collect()
}
IntSize::U4 => {
let v = attr.read_raw::<i32>()?;
v.iter().flat_map(|x| x.to_ne_bytes()).collect()
}
IntSize::U8 => {
let v = attr.read_raw::<i64>()?;
v.iter().flat_map(|x| x.to_ne_bytes()).collect()
}
}),
TypeDescriptor::Unsigned(int_size) => Ok(match int_size {
IntSize::U1 => attr.read_raw::<u8>()?,
IntSize::U2 => {
let v = attr.read_raw::<u16>()?;
v.iter().flat_map(|x| x.to_ne_bytes()).collect()
}
IntSize::U4 => {
let v = attr.read_raw::<u32>()?;
v.iter().flat_map(|x| x.to_ne_bytes()).collect()
}
IntSize::U8 => {
let v = attr.read_raw::<u64>()?;
v.iter().flat_map(|x| x.to_ne_bytes()).collect()
}
}),
TypeDescriptor::Float(float_size) => Ok(match float_size {
FloatSize::U4 => {
let v = attr.read_raw::<f32>()?;
v.iter().flat_map(|x| x.to_ne_bytes()).collect()
}
FloatSize::U8 => {
let v = attr.read_raw::<f64>()?;
v.iter().flat_map(|x| x.to_ne_bytes()).collect()
}
}),
TypeDescriptor::Boolean => {
let v = attr.read_raw::<bool>()?;
Ok(v.iter().map(|&b| b as u8).collect())
}
// For string arrays in attributes, concatenate UTF-8 bytes
TypeDescriptor::VarLenUnicode => {
let v = attr.read_raw::<VarLenUnicode>()?;
let mut buf = Vec::new();
for s in &v {
buf.extend_from_slice(s.as_str().as_bytes());
}
Ok(buf)
}
TypeDescriptor::VarLenAscii => {
let v = attr.read_raw::<VarLenAscii>()?;
let mut buf = Vec::new();
for s in &v {
buf.extend_from_slice(s.as_str().as_bytes());
}
Ok(buf)
}
// Fallback: try reading raw bytes
_ => Ok(attr.read_raw::<u8>()?),
}
}
Loading
Loading