1use alloc::string::{String, ToString};
4use alloc::vec::Vec;
5
6use crate::error::{HDF5Error, Result};
7use crate::model::{DTypeKind, DatasetRec, FileModel};
8use crate::{HDF5DType, HDF5_MAX_DIMS, HDF5_MAX_NAME_LEN};
9
10#[derive(Clone, Debug)]
12pub struct Hdf5File {
13 model: FileModel,
14}
15
16impl Default for Hdf5File {
17 fn default() -> Self {
18 Self::create()
19 }
20}
21
22impl Hdf5File {
23 pub fn create() -> Self {
25 Self {
26 model: FileModel::new(),
27 }
28 }
29
30 pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
33 Ok(Self {
34 model: crate::decode::decode(bytes)?,
35 })
36 }
37
38 pub fn to_bytes(&self) -> Result<Vec<u8>> {
40 crate::encode::encode(&self.model)
41 }
42
43 #[cfg(feature = "std")]
47 pub fn open<P: AsRef<std::path::Path>>(path: P) -> Result<Self> {
48 let bytes = std::fs::read(path)?;
49 Self::from_bytes(&bytes)
50 }
51
52 #[cfg(feature = "std")]
54 pub fn create_file<P: AsRef<std::path::Path>>(_path: P) -> Result<Self> {
55 Ok(Self::create())
56 }
57
58 #[cfg(feature = "std")]
62 pub fn save<P: AsRef<std::path::Path>>(&self, path: P) -> Result<()> {
63 let bytes = self.to_bytes()?;
64 std::fs::write(path, bytes)?;
65 Ok(())
66 }
67
68 pub fn list_datasets(&self) -> Vec<String> {
70 self.model.list_datasets()
71 }
72
73 pub fn list_groups(&self) -> Vec<String> {
75 self.model.list_groups()
76 }
77
78 pub fn dataset_shape(&self, path: &str) -> Result<Vec<usize>> {
83 let ds = self.dataset_meta(path)?;
84 Ok(ds.shape.iter().map(|d| *d as usize).collect())
85 }
86
87 pub fn dataset_dtype(&self, path: &str) -> Result<HDF5DType> {
91 let p = FileModel::normalize(path)?;
92 let ds = self.model.datasets.get(&p).ok_or(HDF5Error::NotFound(p))?;
93 Ok(ds.kind.to_public())
94 }
95
96 pub fn create_group(&mut self, path: &str) -> Result<()> {
100 self.model.create_group(path)
101 }
102
103 pub fn write_f64(&mut self, path: &str, shape: &[usize], bits: &[u64]) -> Result<()> {
110 self.write_ieee64(path, shape, bits)
111 }
112
113 pub fn write_ieee64(&mut self, path: &str, shape: &[usize], bits: &[u64]) -> Result<()> {
117 self.write_bits(path, shape, DTypeKind::Float64, lanes_u64(bits))
118 }
119
120 pub fn write_f32(&mut self, path: &str, shape: &[usize], bits: &[u32]) -> Result<()> {
124 self.write_ieee32(path, shape, bits)
125 }
126
127 pub fn write_ieee32(&mut self, path: &str, shape: &[usize], bits: &[u32]) -> Result<()> {
129 self.write_bits(path, shape, DTypeKind::Float32, lanes_u32(bits))
130 }
131
132 pub fn write_opaque(
137 &mut self,
138 path: &str,
139 shape: &[usize],
140 elem_size: usize,
141 data: &[u8],
142 ) -> Result<()> {
143 self.write_bits(path, shape, DTypeKind::Opaque(elem_size), data.to_vec())
144 }
145
146 pub fn write_i8(&mut self, path: &str, shape: &[usize], bits: &[i8]) -> Result<()> {
148 self.write_bits(path, shape, DTypeKind::Int8, i8_lanes(bits))
149 }
150 pub fn write_i16(&mut self, path: &str, shape: &[usize], bits: &[i16]) -> Result<()> {
152 self.write_bits(path, shape, DTypeKind::Int16, i16_lanes(bits))
153 }
154 pub fn write_i32(&mut self, path: &str, shape: &[usize], bits: &[i32]) -> Result<()> {
156 self.write_bits(path, shape, DTypeKind::Int32, i32_lanes(bits))
157 }
158 pub fn write_i64(&mut self, path: &str, shape: &[usize], bits: &[i64]) -> Result<()> {
160 self.write_bits(path, shape, DTypeKind::Int64, i64_lanes(bits))
161 }
162 pub fn write_u8(&mut self, path: &str, shape: &[usize], bits: &[u8]) -> Result<()> {
164 self.write_bits(path, shape, DTypeKind::UInt8, bits.to_vec())
165 }
166 pub fn write_u16(&mut self, path: &str, shape: &[usize], bits: &[u16]) -> Result<()> {
168 self.write_bits(path, shape, DTypeKind::UInt16, u16_lanes(bits))
169 }
170 pub fn write_u32(&mut self, path: &str, shape: &[usize], bits: &[u32]) -> Result<()> {
172 self.write_bits(path, shape, DTypeKind::UInt32, lanes_u32(bits))
173 }
174 pub fn write_u64(&mut self, path: &str, shape: &[usize], bits: &[u64]) -> Result<()> {
176 self.write_bits(path, shape, DTypeKind::UInt64, lanes_u64(bits))
177 }
178
179 pub fn read_f64(&self, path: &str) -> Result<(Vec<usize>, Vec<u64>)> {
184 self.read_ieee64(path)
185 }
186
187 pub fn read_ieee64(&self, path: &str) -> Result<(Vec<usize>, Vec<u64>)> {
189 let ds = self.dataset(path)?;
190 if ds.kind != DTypeKind::Float64 {
191 return Err(HDF5Error::TypeMismatch);
192 }
193 let shape = ds.shape.iter().map(|d| *d as usize).collect();
194 Ok((shape, from_u64_le(&ds.data)))
195 }
196
197 pub fn read_f32(&self, path: &str) -> Result<(Vec<usize>, Vec<u32>)> {
201 self.read_ieee32(path)
202 }
203
204 pub fn read_ieee32(&self, path: &str) -> Result<(Vec<usize>, Vec<u32>)> {
206 let ds = self.dataset(path)?;
207 if ds.kind != DTypeKind::Float32 {
208 return Err(HDF5Error::TypeMismatch);
209 }
210 let shape = ds.shape.iter().map(|d| *d as usize).collect();
211 Ok((shape, from_u32_le(&ds.data)))
212 }
213
214 pub fn read_opaque(&self, path: &str) -> Result<(Vec<usize>, usize, Vec<u8>)> {
219 let ds = self.dataset(path)?;
220 let DTypeKind::Opaque(sz) = ds.kind else {
221 return Err(HDF5Error::TypeMismatch);
222 };
223 let shape = ds.shape.iter().map(|d| *d as usize).collect();
224 Ok((shape, sz, ds.data.clone()))
225 }
226
227 pub fn compound_fields(&self, path: &str) -> Result<Vec<crate::CompoundField>> {
231 let ds = self.dataset_meta(path)?;
232 Ok(ds.compound_fields.clone())
233 }
234
235 pub fn read_raw(&self, path: &str) -> Result<(Vec<usize>, usize, Vec<u8>)> {
243 let ds = self.dataset(path)?;
244 let shape = ds.shape.iter().map(|d| *d as usize).collect();
245 Ok((shape, ds.kind.elem_size(), ds.data.clone()))
246 }
247
248 pub fn read_i8(&self, path: &str) -> Result<(Vec<usize>, Vec<i8>)> {
250 self.read_kind(path, DTypeKind::Int8, from_i8)
251 }
252 pub fn read_i16(&self, path: &str) -> Result<(Vec<usize>, Vec<i16>)> {
254 self.read_kind(path, DTypeKind::Int16, from_i16_le)
255 }
256 pub fn read_i32(&self, path: &str) -> Result<(Vec<usize>, Vec<i32>)> {
258 self.read_kind(path, DTypeKind::Int32, from_i32_le)
259 }
260 pub fn read_i64(&self, path: &str) -> Result<(Vec<usize>, Vec<i64>)> {
262 self.read_kind(path, DTypeKind::Int64, from_i64_le)
263 }
264 pub fn read_u8(&self, path: &str) -> Result<(Vec<usize>, Vec<u8>)> {
266 let ds = self.dataset(path)?;
267 if ds.kind != DTypeKind::UInt8 {
268 return Err(HDF5Error::TypeMismatch);
269 }
270 let shape = ds.shape.iter().map(|d| *d as usize).collect();
271 Ok((shape, ds.data.clone()))
272 }
273 pub fn read_u16(&self, path: &str) -> Result<(Vec<usize>, Vec<u16>)> {
275 self.read_kind(path, DTypeKind::UInt16, from_u16_le)
276 }
277 pub fn read_u32(&self, path: &str) -> Result<(Vec<usize>, Vec<u32>)> {
279 self.read_kind(path, DTypeKind::UInt32, from_u32_le)
280 }
281 pub fn read_u64(&self, path: &str) -> Result<(Vec<usize>, Vec<u64>)> {
283 self.read_kind(path, DTypeKind::UInt64, from_u64_le)
284 }
285
286 pub fn read_f64_slice(
291 &self,
292 path: &str,
293 row_start: usize,
294 row_end: usize,
295 ) -> Result<(Vec<usize>, Vec<u64>)> {
296 let (shape, bits) = self.read_f64(path)?;
297 if row_end < row_start {
298 return Err(HDF5Error::ShapeMismatch);
299 }
300 if shape.is_empty() {
301 return Err(HDF5Error::ShapeMismatch);
302 }
303 if row_end > shape[0] {
304 return Err(HDF5Error::ShapeMismatch);
305 }
306 let rest: usize = shape[1..]
307 .iter()
308 .try_fold(1usize, |a, b| a.checked_mul(*b))
309 .ok_or(HDF5Error::ShapeMismatch)?;
310 let start = row_start.saturating_mul(rest);
311 let end = row_end.saturating_mul(rest);
312 let mut out_shape = alloc::vec![row_end - row_start];
313 out_shape.extend_from_slice(&shape[1..]);
314 Ok((out_shape, bits[start..end].to_vec()))
315 }
316
317 pub fn append_f64(&mut self, path: &str, shape: &[usize], bits: &[u64]) -> Result<()> {
323 self.append_ieee64(path, shape, bits)
324 }
325
326 pub fn append_ieee64(&mut self, path: &str, shape: &[usize], bits: &[u64]) -> Result<()> {
328 self.append_kind(
329 path,
330 shape,
331 DTypeKind::Float64,
332 &lanes_u64(bits),
333 bits.len(),
334 )
335 }
336
337 pub fn append_f32(&mut self, path: &str, shape: &[usize], bits: &[u32]) -> Result<()> {
339 self.append_ieee32(path, shape, bits)
340 }
341
342 pub fn append_ieee32(&mut self, path: &str, shape: &[usize], bits: &[u32]) -> Result<()> {
344 self.append_kind(
345 path,
346 shape,
347 DTypeKind::Float32,
348 &lanes_u32(bits),
349 bits.len(),
350 )
351 }
352
353 pub fn append_i32(&mut self, path: &str, shape: &[usize], bits: &[i32]) -> Result<()> {
355 self.append_kind(path, shape, DTypeKind::Int32, &i32_lanes(bits), bits.len())
356 }
357
358 pub fn append_u64(&mut self, path: &str, shape: &[usize], bits: &[u64]) -> Result<()> {
360 self.append_kind(path, shape, DTypeKind::UInt64, &lanes_u64(bits), bits.len())
361 }
362
363 pub fn append_opaque(&mut self, path: &str, shape: &[usize], data: &[u8]) -> Result<()> {
366 let p = FileModel::normalize(path)?;
367 let elem = match self.model.datasets.get(&p) {
368 Some(ds) => ds.kind.elem_size(),
369 None => return Err(HDF5Error::NotFound(p)),
370 };
371 let n: usize = if shape.is_empty() {
372 1
373 } else {
374 shape
375 .iter()
376 .try_fold(1usize, |a, b| a.checked_mul(*b))
377 .ok_or(HDF5Error::ShapeMismatch)?
378 };
379 let need = n.checked_mul(elem).ok_or(HDF5Error::ShapeMismatch)?;
380 if data.len() != need {
381 return Err(HDF5Error::ShapeMismatch);
382 }
383 self.append_kind(path, shape, DTypeKind::Opaque(elem), data, n)
384 }
385
386 pub fn read_attr_str(&self, path: &str, name: &str) -> Result<String> {
390 let ds = self.dataset_meta(path)?;
391 ds.attrs
392 .iter()
393 .find(|(n, _)| n == name)
394 .map(|(_, v)| v.clone())
395 .ok_or_else(|| HDF5Error::NotFound(name.to_string()))
396 }
397
398 pub fn list_attrs(&self, path: &str) -> Result<Vec<(String, String)>> {
400 Ok(self.dataset_meta(path)?.attrs.clone())
401 }
402
403 pub fn write_attr_str(&mut self, path: &str, name: &str, value: &str) -> Result<()> {
407 if name.len() > HDF5_MAX_NAME_LEN || value.len() > HDF5_MAX_NAME_LEN {
408 return Err(HDF5Error::NameTooLong);
409 }
410 let p = FileModel::normalize(path)?;
411 let ds = self
412 .model
413 .datasets
414 .get_mut(&p)
415 .ok_or(HDF5Error::NotFound(p))?;
416 if let Some(slot) = ds.attrs.iter_mut().find(|(n, _)| n == name) {
417 slot.1 = value.to_string();
418 } else {
419 ds.attrs.push((name.to_string(), value.to_string()));
420 }
421 Ok(())
422 }
423
424 fn dataset_meta(&self, path: &str) -> Result<&DatasetRec> {
425 let p = FileModel::normalize(path)?;
426 self.model.datasets.get(&p).ok_or(HDF5Error::NotFound(p))
427 }
428
429 fn dataset(&self, path: &str) -> Result<&DatasetRec> {
430 let ds = self.dataset_meta(path)?;
431 if ds.filtered {
432 return Err(HDF5Error::FilteredNotSupported);
433 }
434 if ds.chunked {
435 return Err(HDF5Error::ChunkedNotSupported);
436 }
437 Ok(ds)
438 }
439
440 fn read_kind<T>(
441 &self,
442 path: &str,
443 kind: DTypeKind,
444 decode: fn(&[u8]) -> Vec<T>,
445 ) -> Result<(Vec<usize>, Vec<T>)> {
446 let ds = self.dataset(path)?;
447 if ds.kind != kind {
448 return Err(HDF5Error::TypeMismatch);
449 }
450 let shape = ds.shape.iter().map(|d| *d as usize).collect();
451 Ok((shape, decode(&ds.data)))
452 }
453
454 fn append_kind(
455 &mut self,
456 path: &str,
457 shape: &[usize],
458 kind: DTypeKind,
459 extra: &[u8],
460 n_elems: usize,
461 ) -> Result<()> {
462 let p = FileModel::normalize(path)?;
463 let ds = self
464 .model
465 .datasets
466 .get_mut(&p)
467 .ok_or_else(|| HDF5Error::NotFound(p.clone()))?;
468 if ds.filtered {
469 return Err(HDF5Error::FilteredNotSupported);
470 }
471 if ds.chunked {
472 return Err(HDF5Error::ChunkedNotSupported);
473 }
474 if ds.kind != kind {
475 return Err(HDF5Error::TypeMismatch);
476 }
477 let expect: usize = if shape.is_empty() {
478 1
479 } else {
480 shape
481 .iter()
482 .try_fold(1usize, |a, b| a.checked_mul(*b))
483 .ok_or(HDF5Error::ShapeMismatch)?
484 };
485 if n_elems != expect {
486 return Err(HDF5Error::ShapeMismatch);
487 }
488 if ds.shape.is_empty() {
489 return Err(HDF5Error::ShapeMismatch);
490 }
491 if ds.shape.len() != shape.len() {
492 return Err(HDF5Error::ShapeMismatch);
493 }
494 if ds.shape.len() >= 2
495 && ds.shape[1..] != shape[1..].iter().map(|d| *d as u64).collect::<Vec<_>>()
496 {
497 return Err(HDF5Error::ShapeMismatch);
498 }
499 ds.shape[0] = ds.shape[0].saturating_add(shape[0] as u64);
500 ds.data.extend_from_slice(extra);
501 Ok(())
502 }
503
504 fn write_bits(
505 &mut self,
506 path: &str,
507 shape: &[usize],
508 kind: DTypeKind,
509 data: Vec<u8>,
510 ) -> Result<()> {
511 if shape.len() > HDF5_MAX_DIMS {
512 return Err(HDF5Error::RankNotSupported);
513 }
514 let n: usize = if shape.is_empty() {
515 1
516 } else {
517 shape
518 .iter()
519 .try_fold(1usize, |a, b| a.checked_mul(*b))
520 .ok_or(HDF5Error::ShapeMismatch)?
521 };
522 let need = n
523 .checked_mul(kind.elem_size())
524 .ok_or(HDF5Error::ShapeMismatch)?;
525 if data.len() != need {
526 return Err(HDF5Error::ShapeMismatch);
527 }
528 let rec = DatasetRec {
529 shape: shape.iter().map(|d| *d as u64).collect(),
530 kind,
531 data,
532 attrs: FileModel::default_attrs(),
533 chunked: false,
534 filtered: false,
535 compound_fields: Vec::new(),
536 };
537 self.model.put_dataset(path, rec)
538 }
539}
540
541fn lanes_u64(bits: &[u64]) -> Vec<u8> {
542 let mut o = Vec::with_capacity(bits.len().saturating_mul(8));
543 for b in bits {
544 o.extend_from_slice(&b.to_le_bytes());
545 }
546 o
547}
548
549fn lanes_u32(bits: &[u32]) -> Vec<u8> {
550 let mut o = Vec::with_capacity(bits.len().saturating_mul(4));
551 for b in bits {
552 o.extend_from_slice(&b.to_le_bytes());
553 }
554 o
555}
556
557fn u16_lanes(bits: &[u16]) -> Vec<u8> {
558 let mut o = Vec::with_capacity(bits.len().saturating_mul(2));
559 for b in bits {
560 o.extend_from_slice(&b.to_le_bytes());
561 }
562 o
563}
564
565fn i8_lanes(bits: &[i8]) -> Vec<u8> {
566 bits.iter().map(|b| b.to_le_bytes()[0]).collect()
567}
568
569fn i16_lanes(bits: &[i16]) -> Vec<u8> {
570 let mut o = Vec::with_capacity(bits.len().saturating_mul(2));
571 for b in bits {
572 o.extend_from_slice(&b.to_le_bytes());
573 }
574 o
575}
576
577fn i32_lanes(bits: &[i32]) -> Vec<u8> {
578 let mut o = Vec::with_capacity(bits.len().saturating_mul(4));
579 for b in bits {
580 o.extend_from_slice(&b.to_le_bytes());
581 }
582 o
583}
584
585fn i64_lanes(bits: &[i64]) -> Vec<u8> {
586 let mut o = Vec::with_capacity(bits.len().saturating_mul(8));
587 for b in bits {
588 o.extend_from_slice(&b.to_le_bytes());
589 }
590 o
591}
592
593fn from_u64_le(data: &[u8]) -> Vec<u64> {
594 data.as_chunks::<8>()
595 .0
596 .iter()
597 .map(|c| u64::from_le_bytes(*c))
598 .collect()
599}
600
601fn from_u32_le(data: &[u8]) -> Vec<u32> {
602 data.as_chunks::<4>()
603 .0
604 .iter()
605 .map(|c| u32::from_le_bytes(*c))
606 .collect()
607}
608
609fn from_u16_le(data: &[u8]) -> Vec<u16> {
610 data.as_chunks::<2>()
611 .0
612 .iter()
613 .map(|c| u16::from_le_bytes(*c))
614 .collect()
615}
616
617fn from_i8(data: &[u8]) -> Vec<i8> {
618 data.iter().map(|b| i8::from_le_bytes([*b])).collect()
619}
620
621fn from_i16_le(data: &[u8]) -> Vec<i16> {
622 data.as_chunks::<2>()
623 .0
624 .iter()
625 .map(|c| i16::from_le_bytes(*c))
626 .collect()
627}
628
629fn from_i32_le(data: &[u8]) -> Vec<i32> {
630 data.as_chunks::<4>()
631 .0
632 .iter()
633 .map(|c| i32::from_le_bytes(*c))
634 .collect()
635}
636
637fn from_i64_le(data: &[u8]) -> Vec<i64> {
638 data.as_chunks::<8>()
639 .0
640 .iter()
641 .map(|c| i64::from_le_bytes(*c))
642 .collect()
643}
644
645pub fn list_datasets(file: &Hdf5File) -> Vec<String> {
647 file.list_datasets()
648}
649
650pub fn list_groups(file: &Hdf5File) -> Vec<String> {
652 file.list_groups()
653}
654
655pub fn dataset_shape(file: &Hdf5File, path: &str) -> Result<Vec<usize>> {
657 file.dataset_shape(path)
658}
659
660pub fn dataset_dtype(file: &Hdf5File, path: &str) -> Result<HDF5DType> {
662 file.dataset_dtype(path)
663}