hdf5-pure-rust 0.3.1

Pure Rust implementation of the HDF5 file format
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
use std::fs;

use crate::engine::writer::{
    CompoundFieldSpec, DatasetSpec, DtypeSpec, FillValueSpec, HdfFileWriter,
};
use crate::error::{Error, Result};
use crate::hl::types::{H5Type, TypeClass};

/// Builder for creating datasets with a fluent API.
pub struct DatasetBuilder<'a> {
    writer: &'a mut HdfFileWriter<fs::File>,
    parent: String,
    name: String,
    shape: Option<Vec<u64>>,
    max_shape: Option<Vec<u64>>,
    resizable: bool,
    chunk_dims: Option<Vec<u64>>,
    deflate_level: Option<u32>,
    shuffle: bool,
    fletcher32: bool,
    compact: bool,
    fill_value: Option<Vec<u8>>,
    alloc_time: u8,
    fill_time: u8,
    attrs: Vec<OwnedBuilderAttr>,
}

struct OwnedBuilderAttr {
    name: String,
    shape: Vec<u64>,
    dtype: DtypeSpec,
    data: Vec<u8>,
}

impl OwnedBuilderAttr {
    fn as_attr_spec(&self) -> crate::engine::writer::AttrSpec<'_> {
        crate::engine::writer::AttrSpec {
            name: &self.name,
            shape: &self.shape,
            dtype: self.dtype.clone(),
            data: &self.data,
        }
    }
}

impl<'a> DatasetBuilder<'a> {
    pub(crate) fn new(writer: &'a mut HdfFileWriter<fs::File>, parent: &str, name: &str) -> Self {
        Self {
            writer,
            parent: parent.to_string(),
            name: name.to_string(),
            shape: None,
            max_shape: None,
            resizable: false,
            chunk_dims: None,
            deflate_level: None,
            shuffle: false,
            fletcher32: false,
            compact: false,
            fill_value: None,
            alloc_time: 1,
            fill_time: 2,
            attrs: Vec::new(),
        }
    }

    /// Set the dataset shape.
    pub fn shape(mut self, dims: &[u64]) -> Self {
        self.shape = Some(dims.to_vec());
        self
    }

    /// Set chunk dimensions (enables chunked storage).
    pub fn chunk(mut self, dims: &[u64]) -> Self {
        self.chunk_dims = Some(dims.to_vec());
        self
    }

    /// Enable deflate (gzip) compression at the given level (0-9).
    pub fn deflate(mut self, level: u32) -> Self {
        self.deflate_level = Some(level);
        self
    }

    /// Enable byte shuffle filter (should be used with compression).
    pub fn shuffle(mut self) -> Self {
        self.shuffle = true;
        self
    }

    /// Enable Fletcher32 chunk checksums.
    pub fn fletcher32(mut self) -> Self {
        self.fletcher32 = true;
        self
    }

    /// Make the dataset resizable (unlimited max dimensions).
    /// Requires chunked storage.
    pub fn resizable(mut self) -> Self {
        self.resizable = true;
        self
    }

    /// Set explicit maximum dimensions.
    pub fn max_shape(mut self, dims: &[u64]) -> Self {
        self.max_shape = Some(dims.to_vec());
        self.resizable = false;
        self
    }

    /// Use compact storage (data embedded in object header, for small datasets).
    pub fn compact(mut self) -> Self {
        self.compact = true;
        self
    }

    /// Set raw HDF5 allocation-time and fill-write-time properties.
    pub fn fill_properties(mut self, alloc_time: u8, fill_time: u8) -> Self {
        self.alloc_time = alloc_time;
        self.fill_time = fill_time;
        self
    }

    /// Set a scalar fill value for missing or newly allocated dataset storage.
    pub fn fill_value<T: H5Type>(mut self, value: T) -> Self {
        let byte_ptr = &value as *const T as *const u8;
        let bytes = unsafe { std::slice::from_raw_parts(byte_ptr, T::type_size()) };
        self.fill_value = Some(bytes.to_vec());
        self
    }

    /// Add a scalar attribute to the dataset being created.
    pub fn attr<T: H5Type>(mut self, name: &str, value: T) -> Result<Self> {
        let dtype = dtype_for_type::<T>()?;
        let byte_ptr = &value as *const T as *const u8;
        let data = unsafe { std::slice::from_raw_parts(byte_ptr, T::type_size()) };
        self.push_attr(OwnedBuilderAttr {
            name: name.to_string(),
            shape: Vec::new(),
            dtype,
            data: data.to_vec(),
        })?;
        Ok(self)
    }

    /// Add a one-dimensional array attribute to the dataset being created.
    pub fn attr_array<T: H5Type>(mut self, name: &str, values: &[T]) -> Result<Self> {
        let dtype = dtype_for_type::<T>()?;
        let byte_len = values
            .len()
            .checked_mul(T::type_size())
            .ok_or_else(|| Error::InvalidFormat("attribute byte size overflow".into()))?;
        let data = unsafe { std::slice::from_raw_parts(values.as_ptr() as *const u8, byte_len) };
        self.push_attr(OwnedBuilderAttr {
            name: name.to_string(),
            shape: vec![values.len() as u64],
            dtype,
            data: data.to_vec(),
        })?;
        Ok(self)
    }

    /// Add a fixed-length ASCII string attribute to the dataset being created.
    pub fn fixed_ascii_attr(mut self, name: &str, value: &str, len: usize) -> Result<Self> {
        let (dtype, data) = fixed_string_attr(&[value], len, false)?;
        self.push_attr(OwnedBuilderAttr {
            name: name.to_string(),
            shape: Vec::new(),
            dtype,
            data,
        })?;
        Ok(self)
    }

    /// Add a fixed-length UTF-8 string attribute to the dataset being created.
    pub fn fixed_utf8_attr(mut self, name: &str, value: &str, len: usize) -> Result<Self> {
        let (dtype, data) = fixed_string_attr(&[value], len, true)?;
        self.push_attr(OwnedBuilderAttr {
            name: name.to_string(),
            shape: Vec::new(),
            dtype,
            data,
        })?;
        Ok(self)
    }

    /// Add a one-dimensional fixed-length ASCII string array attribute.
    pub fn fixed_ascii_attr_array(
        mut self,
        name: &str,
        values: &[&str],
        len: usize,
    ) -> Result<Self> {
        let (dtype, data) = fixed_string_attr(values, len, false)?;
        self.push_attr(OwnedBuilderAttr {
            name: name.to_string(),
            shape: vec![values.len() as u64],
            dtype,
            data,
        })?;
        Ok(self)
    }

    /// Add a one-dimensional fixed-length UTF-8 string array attribute.
    pub fn fixed_utf8_attr_array(
        mut self,
        name: &str,
        values: &[&str],
        len: usize,
    ) -> Result<Self> {
        let (dtype, data) = fixed_string_attr(values, len, true)?;
        self.push_attr(OwnedBuilderAttr {
            name: name.to_string(),
            shape: vec![values.len() as u64],
            dtype,
            data,
        })?;
        Ok(self)
    }

    /// Write data and create the dataset. Infers shape from data length if not set.
    pub fn write<T: H5Type>(self, data: &[T]) -> Result<()> {
        let dtype = dtype_for_type::<T>()?;
        let fill_value = self.fill_value.clone();
        let fill = Self::fill_spec(
            fill_value.as_deref(),
            dtype.size() as usize,
            self.alloc_time,
            self.fill_time,
        )?;
        let shape = self
            .shape
            .clone()
            .unwrap_or_else(|| vec![data.len() as u64]);
        Self::validate_element_count(&shape, data.len())?;
        let max_shape = self.effective_max_shape(&shape)?;

        // Convert data to bytes
        let byte_ptr = data.as_ptr() as *const u8;
        let byte_len = data.len() * T::type_size();
        let data_bytes = unsafe { std::slice::from_raw_parts(byte_ptr, byte_len) };

        let spec = DatasetSpec {
            name: &self.name,
            shape: &shape,
            max_shape: max_shape.as_deref(),
            dtype,
            data: data_bytes,
        };
        let attrs: Vec<_> = self
            .attrs
            .iter()
            .map(OwnedBuilderAttr::as_attr_spec)
            .collect();

        if self.compact {
            self.validate_compact_options()?;
            if attrs.is_empty() {
                self.writer
                    .create_compact_dataset_with_fill(&self.parent, &spec, fill)?;
            } else {
                self.writer.create_compact_dataset_with_attrs_and_fill(
                    &self.parent,
                    &spec,
                    &attrs,
                    fill,
                )?;
            }
        } else if self.chunk_dims.is_some()
            || self.deflate_level.is_some()
            || self.shuffle
            || self.fletcher32
        {
            let chunk_dims = self.chunk_dims.unwrap_or_else(|| shape.clone());
            self.writer.create_chunked_dataset_with_attrs_and_fill(
                &self.parent,
                &spec,
                &chunk_dims,
                self.deflate_level,
                self.shuffle,
                self.fletcher32,
                fill,
                &attrs,
            )?;
        } else {
            if attrs.is_empty() {
                self.writer
                    .create_dataset_with_fill(&self.parent, &spec, fill)?;
            } else {
                self.writer.create_dataset_with_attrs_and_fill(
                    &self.parent,
                    &spec,
                    &attrs,
                    fill,
                )?;
            }
        }

        Ok(())
    }

    /// Write raw element bytes with an explicit HDF5 datatype.
    ///
    /// This exposes writer datatypes that cannot be inferred from a Rust
    /// primitive type, such as enum, opaque, array, and nested compound
    /// datatypes. If no shape was set, a one-dimensional shape is inferred
    /// from `data.len() / dtype.size()`.
    pub fn write_raw_with_dtype(self, dtype: DtypeSpec, data: &[u8]) -> Result<()> {
        let dtype_size = dtype.size() as usize;
        if dtype_size == 0 {
            return Err(Error::InvalidFormat(
                "dataset datatype size must be nonzero".into(),
            ));
        }
        if data.len() % dtype_size != 0 {
            return Err(Error::InvalidFormat(format!(
                "raw dataset byte length {} is not a multiple of datatype size {dtype_size}",
                data.len()
            )));
        }

        let fill_value = self.fill_value.clone();
        let fill = Self::fill_spec(
            fill_value.as_deref(),
            dtype_size,
            self.alloc_time,
            self.fill_time,
        )?;
        let shape = self
            .shape
            .clone()
            .unwrap_or_else(|| vec![(data.len() / dtype_size) as u64]);
        let max_shape = self.effective_max_shape(&shape)?;
        let expected_count = shape_element_count(&shape)?;
        let expected_bytes = usize::try_from(expected_count)
            .map_err(|_| Error::InvalidFormat("dataset element count exceeds usize".into()))?
            .checked_mul(dtype_size)
            .ok_or_else(|| Error::InvalidFormat("dataset byte size overflow".into()))?;
        if expected_bytes != data.len() {
            return Err(Error::InvalidFormat(format!(
                "raw dataset byte length {} does not match shape element count {expected_count} * datatype size {dtype_size}",
                data.len()
            )));
        }

        let spec = DatasetSpec {
            name: &self.name,
            shape: &shape,
            max_shape: max_shape.as_deref(),
            dtype,
            data,
        };
        let attrs: Vec<_> = self
            .attrs
            .iter()
            .map(OwnedBuilderAttr::as_attr_spec)
            .collect();

        if self.compact {
            self.validate_compact_options()?;
            if attrs.is_empty() {
                self.writer
                    .create_compact_dataset_with_fill(&self.parent, &spec, fill)?;
            } else {
                self.writer.create_compact_dataset_with_attrs_and_fill(
                    &self.parent,
                    &spec,
                    &attrs,
                    fill,
                )?;
            }
        } else if self.chunk_dims.is_some()
            || self.deflate_level.is_some()
            || self.shuffle
            || self.fletcher32
        {
            let chunk_dims = self.chunk_dims.unwrap_or_else(|| shape.clone());
            self.writer.create_chunked_dataset_with_attrs_and_fill(
                &self.parent,
                &spec,
                &chunk_dims,
                self.deflate_level,
                self.shuffle,
                self.fletcher32,
                fill,
                &attrs,
            )?;
        } else if attrs.is_empty() {
            self.writer
                .create_dataset_with_fill(&self.parent, &spec, fill)?;
        } else {
            self.writer
                .create_dataset_with_attrs_and_fill(&self.parent, &spec, &attrs, fill)?;
        }

        Ok(())
    }

    /// Write a scalar value.
    pub fn write_scalar<T: H5Type>(self, value: T) -> Result<()> {
        if self.chunk_dims.is_some()
            || self.deflate_level.is_some()
            || self.shuffle
            || self.fletcher32
        {
            return Err(Error::Unsupported(
                "scalar dataset writer does not support chunked storage or filters".into(),
            ));
        }
        if self.resizable || self.max_shape.is_some() {
            return Err(Error::InvalidFormat(
                "scalar dataset writer does not support max dimensions".into(),
            ));
        }
        let dtype = dtype_for_type::<T>()?;
        let fill_value = self.fill_value.clone();
        let fill = Self::fill_spec(
            fill_value.as_deref(),
            dtype.size() as usize,
            self.alloc_time,
            self.fill_time,
        )?;
        let byte_ptr = &value as *const T as *const u8;
        let data_bytes = unsafe { std::slice::from_raw_parts(byte_ptr, T::type_size()) };

        let spec = DatasetSpec {
            name: &self.name,
            shape: &[],
            max_shape: None,
            dtype,
            data: data_bytes,
        };

        let attrs: Vec<_> = self
            .attrs
            .iter()
            .map(OwnedBuilderAttr::as_attr_spec)
            .collect();
        if self.compact {
            self.validate_compact_options()?;
            if attrs.is_empty() {
                self.writer
                    .create_compact_dataset_with_fill(&self.parent, &spec, fill)?;
            } else {
                self.writer.create_compact_dataset_with_attrs_and_fill(
                    &self.parent,
                    &spec,
                    &attrs,
                    fill,
                )?;
            }
        } else if attrs.is_empty() {
            self.writer
                .create_dataset_with_fill(&self.parent, &spec, fill)?;
        } else {
            self.writer
                .create_dataset_with_attrs_and_fill(&self.parent, &spec, &attrs, fill)?;
        }
        Ok(())
    }

    /// Write fixed-length ASCII strings. Strings longer than `len` bytes are rejected.
    pub fn write_fixed_ascii_strings(self, data: &[&str], len: usize) -> Result<()> {
        self.write_fixed_strings(data, len, false)
    }

    /// Write fixed-length UTF-8 strings. Strings longer than `len` bytes are rejected.
    pub fn write_fixed_utf8_strings(self, data: &[&str], len: usize) -> Result<()> {
        self.write_fixed_strings(data, len, true)
    }

    /// Write variable-length UTF-8 strings using HDF5 global heap storage.
    pub fn write_vlen_utf8_strings(self, data: &[&str]) -> Result<()> {
        if self.compact
            || self.chunk_dims.is_some()
            || self.deflate_level.is_some()
            || self.shuffle
            || self.fletcher32
        {
            return Err(Error::Unsupported(
                "variable-length string writer currently supports contiguous storage only".into(),
            ));
        }
        if self.fill_value.is_some() {
            return Err(Error::Unsupported(
                "variable-length string fill values are not supported yet".into(),
            ));
        }
        let shape = self
            .shape
            .clone()
            .unwrap_or_else(|| vec![data.len() as u64]);
        let max_shape = self.effective_max_shape(&shape)?;
        let attrs: Vec<_> = self
            .attrs
            .iter()
            .map(OwnedBuilderAttr::as_attr_spec)
            .collect();
        self.writer.create_vlen_utf8_string_dataset_with_attrs(
            &self.parent,
            &self.name,
            &shape,
            data,
            max_shape.as_deref(),
            &attrs,
        )?;
        Ok(())
    }

    fn write_fixed_strings(self, data: &[&str], len: usize, utf8: bool) -> Result<()> {
        if len > u32::MAX as usize {
            return Err(Error::InvalidFormat(
                "fixed string length exceeds u32".into(),
            ));
        }
        let dtype = if utf8 {
            DtypeSpec::FixedUtf8String {
                len: len as u32,
                padding: 1,
            }
        } else {
            DtypeSpec::FixedAsciiString {
                len: len as u32,
                padding: 1,
            }
        };
        let fill_value = self.fill_value.clone();
        let fill = Self::fill_spec(
            fill_value.as_deref(),
            dtype.size() as usize,
            self.alloc_time,
            self.fill_time,
        )?;
        let shape = self
            .shape
            .clone()
            .unwrap_or_else(|| vec![data.len() as u64]);
        let max_shape = self.effective_max_shape(&shape)?;
        let expected_count = shape_element_count(&shape)?;
        if expected_count != data.len() as u64 {
            return Err(Error::InvalidFormat(format!(
                "fixed string data length {} does not match dataset shape element count {expected_count}",
                data.len()
            )));
        }

        let data_capacity = data.len().checked_mul(len).ok_or_else(|| {
            Error::InvalidFormat("fixed string dataset payload size overflow".into())
        })?;
        let mut data_bytes = Vec::with_capacity(data_capacity);
        for value in data {
            let bytes = value.as_bytes();
            if bytes.len() > len {
                return Err(Error::InvalidFormat(format!(
                    "fixed string value has {} bytes, maximum is {len}",
                    bytes.len()
                )));
            }
            data_bytes.extend_from_slice(bytes);
            data_bytes.resize(data_bytes.len() + (len - bytes.len()), 0);
        }

        let spec = DatasetSpec {
            name: &self.name,
            shape: &shape,
            max_shape: max_shape.as_deref(),
            dtype,
            data: &data_bytes,
        };
        let attrs: Vec<_> = self
            .attrs
            .iter()
            .map(OwnedBuilderAttr::as_attr_spec)
            .collect();

        if self.compact {
            self.validate_compact_options()?;
            if attrs.is_empty() {
                self.writer
                    .create_compact_dataset_with_fill(&self.parent, &spec, fill)?;
            } else {
                self.writer.create_compact_dataset_with_attrs_and_fill(
                    &self.parent,
                    &spec,
                    &attrs,
                    fill,
                )?;
            }
        } else if self.chunk_dims.is_some()
            || self.deflate_level.is_some()
            || self.shuffle
            || self.fletcher32
        {
            let chunk_dims = self.chunk_dims.unwrap_or_else(|| shape.clone());
            self.writer.create_chunked_dataset_with_attrs_and_fill(
                &self.parent,
                &spec,
                &chunk_dims,
                self.deflate_level,
                self.shuffle,
                self.fletcher32,
                fill,
                &attrs,
            )?;
        } else if attrs.is_empty() {
            self.writer
                .create_dataset_with_fill(&self.parent, &spec, fill)?;
        } else {
            self.writer
                .create_dataset_with_attrs_and_fill(&self.parent, &spec, &attrs, fill)?;
        }
        Ok(())
    }

    fn push_attr(&mut self, attr: OwnedBuilderAttr) -> Result<()> {
        if self.attrs.iter().any(|existing| existing.name == attr.name) {
            return Err(Error::InvalidFormat(format!(
                "attribute '{}' already exists",
                attr.name
            )));
        }
        self.attrs.push(attr);
        Ok(())
    }

    fn fill_spec(
        value: Option<&[u8]>,
        dtype_size: usize,
        alloc_time: u8,
        fill_time: u8,
    ) -> Result<Option<FillValueSpec<'_>>> {
        if let Some(value) = value {
            if value.len() != dtype_size {
                return Err(Error::InvalidFormat(format!(
                    "fill value has {} bytes, expected {} for dataset datatype",
                    value.len(),
                    dtype_size
                )));
            }
            Ok(Some(FillValueSpec::with_value(
                alloc_time, fill_time, value,
            )))
        } else if alloc_time != 1 || fill_time != 2 {
            Ok(Some(FillValueSpec::undefined(alloc_time, fill_time)))
        } else {
            Ok(None)
        }
    }

    fn effective_max_shape(&self, shape: &[u64]) -> Result<Option<Vec<u64>>> {
        let max_shape = if self.resizable {
            Some(vec![u64::MAX; shape.len()])
        } else {
            self.max_shape.clone()
        };
        let Some(max_shape) = max_shape else {
            return Ok(None);
        };
        if max_shape.len() != shape.len() {
            return Err(Error::InvalidFormat(format!(
                "max shape rank {} does not match dataset rank {}",
                max_shape.len(),
                shape.len()
            )));
        }
        for (idx, (&dim, &max_dim)) in shape.iter().zip(max_shape.iter()).enumerate() {
            if max_dim != u64::MAX && dim > max_dim {
                return Err(Error::InvalidFormat(format!(
                    "dataset dimension {idx} size {dim} exceeds max dimension {max_dim}"
                )));
            }
        }
        Ok(Some(max_shape))
    }

    fn validate_compact_options(&self) -> Result<()> {
        if self.chunk_dims.is_some()
            || self.deflate_level.is_some()
            || self.shuffle
            || self.fletcher32
        {
            return Err(Error::Unsupported(
                "compact dataset writer does not support chunked storage or filters".into(),
            ));
        }
        if self.resizable || self.max_shape.is_some() {
            return Err(Error::InvalidFormat(
                "compact dataset writer does not support max dimensions".into(),
            ));
        }
        Ok(())
    }

    fn validate_element_count(shape: &[u64], data_len: usize) -> Result<()> {
        let expected_count = shape_element_count(shape)?;
        let actual_count = u64::try_from(data_len)
            .map_err(|_| Error::InvalidFormat("dataset element count exceeds u64".into()))?;
        if expected_count != actual_count {
            return Err(Error::InvalidFormat(format!(
                "dataset data length {actual_count} does not match shape element count {expected_count}"
            )));
        }
        Ok(())
    }
}

/// Map a Rust type to DtypeSpec.
pub(crate) fn dtype_for_type<T: H5Type>() -> Result<DtypeSpec> {
    let size = T::type_size();
    // Use TypeId to determine the exact type
    use std::any::TypeId;
    let id = TypeId::of::<T>();

    if id == TypeId::of::<f64>() {
        Ok(DtypeSpec::F64)
    } else if id == TypeId::of::<f32>() {
        Ok(DtypeSpec::F32)
    } else if id == TypeId::of::<i128>() {
        Ok(DtypeSpec::I128)
    } else if id == TypeId::of::<i64>() {
        Ok(DtypeSpec::I64)
    } else if id == TypeId::of::<i32>() {
        Ok(DtypeSpec::I32)
    } else if id == TypeId::of::<i16>() {
        Ok(DtypeSpec::I16)
    } else if id == TypeId::of::<i8>() {
        Ok(DtypeSpec::I8)
    } else if id == TypeId::of::<u128>() {
        Ok(DtypeSpec::U128)
    } else if id == TypeId::of::<u64>() {
        Ok(DtypeSpec::U64)
    } else if id == TypeId::of::<u32>() {
        Ok(DtypeSpec::U32)
    } else if id == TypeId::of::<u16>() {
        Ok(DtypeSpec::U16)
    } else if id == TypeId::of::<u8>() {
        Ok(DtypeSpec::U8)
    } else if let Some(fields) = T::compound_fields() {
        let mut out = Vec::with_capacity(fields.len());
        for field in fields {
            let dtype = match field.type_class {
                TypeClass::Integer { signed: true } => match field.size {
                    1 => DtypeSpec::I8,
                    2 => DtypeSpec::I16,
                    4 => DtypeSpec::I32,
                    8 => DtypeSpec::I64,
                    16 => DtypeSpec::I128,
                    other => {
                        return Err(Error::Unsupported(format!(
                            "unsupported signed compound field size {other}"
                        )))
                    }
                },
                TypeClass::Integer { signed: false } => match field.size {
                    1 => DtypeSpec::U8,
                    2 => DtypeSpec::U16,
                    4 => DtypeSpec::U32,
                    8 => DtypeSpec::U64,
                    16 => DtypeSpec::U128,
                    other => {
                        return Err(Error::Unsupported(format!(
                            "unsupported unsigned compound field size {other}"
                        )))
                    }
                },
                TypeClass::Float => match field.size {
                    4 => DtypeSpec::F32,
                    8 => DtypeSpec::F64,
                    other => {
                        return Err(Error::Unsupported(format!(
                            "unsupported floating compound field size {other}"
                        )))
                    }
                },
                TypeClass::Compound => {
                    return Err(Error::Unsupported(
                        "nested compound writer type descriptors are not supported".into(),
                    ))
                }
            };
            out.push(CompoundFieldSpec {
                name: field.name,
                offset: field.offset as u32,
                dtype,
            });
        }
        Ok(DtypeSpec::Compound {
            size: T::type_size() as u32,
            fields: out,
        })
    } else {
        Err(Error::Unsupported(format!(
            "unsupported type with size {size}"
        )))
    }
}

fn fixed_string_attr(values: &[&str], len: usize, utf8: bool) -> Result<(DtypeSpec, Vec<u8>)> {
    if len > u32::MAX as usize {
        return Err(Error::InvalidFormat(
            "fixed string length exceeds u32".into(),
        ));
    }
    let dtype = if utf8 {
        DtypeSpec::FixedUtf8String {
            len: len as u32,
            padding: 1,
        }
    } else {
        DtypeSpec::FixedAsciiString {
            len: len as u32,
            padding: 1,
        }
    };
    let capacity = values.len().checked_mul(len).ok_or_else(|| {
        Error::InvalidFormat("fixed string attribute payload size overflow".into())
    })?;
    let mut data = Vec::with_capacity(capacity);
    for value in values {
        let bytes = value.as_bytes();
        if bytes.len() > len {
            return Err(Error::InvalidFormat(format!(
                "fixed string attribute has {} bytes, maximum is {len}",
                bytes.len()
            )));
        }
        data.extend_from_slice(bytes);
        data.resize(data.len() + (len - bytes.len()), 0);
    }
    Ok((dtype, data))
}

fn shape_element_count(shape: &[u64]) -> Result<u64> {
    if shape.is_empty() {
        return Ok(1);
    }
    shape.iter().try_fold(1u64, |acc, &dim| {
        acc.checked_mul(dim)
            .ok_or_else(|| Error::InvalidFormat("dataset shape element count overflow".into()))
    })
}

#[cfg(test)]
mod tests {
    use super::shape_element_count;

    #[test]
    fn shape_element_count_rejects_overflow() {
        let err = shape_element_count(&[u64::MAX, 2]).unwrap_err();
        assert!(
            err.to_string()
                .contains("dataset shape element count overflow"),
            "unexpected error: {err}"
        );
    }
}