Skip to main content

scirs2_io/hdf5/
types_3.rs

1//! Auto-generated module
2//!
3//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
4
5use std::collections::HashMap;
6
7use super::types::{AttributeValue, DataArray};
8
9/// File access mode
10#[derive(Debug, Clone, PartialEq)]
11pub enum FileMode {
12    /// Read-only mode
13    ReadOnly,
14    /// Read-write mode
15    ReadWrite,
16    /// Create new file (fail if exists)
17    Create,
18    /// Create or truncate existing file
19    Truncate,
20}
21/// String encoding types
22#[derive(Debug, Clone, PartialEq)]
23pub enum StringEncoding {
24    /// UTF-8 encoding
25    UTF8,
26    /// ASCII encoding
27    ASCII,
28}
29/// File statistics
30#[derive(Debug, Clone, Default)]
31pub struct FileStats {
32    /// Number of groups in the file
33    pub num_groups: usize,
34    /// Number of datasets in the file
35    pub num_datasets: usize,
36    /// Number of attributes in the file
37    pub num_attributes: usize,
38    /// Total data size in bytes
39    pub total_data_size: usize,
40}
41/// HDF5 group
42#[derive(Debug, Clone)]
43pub struct Group {
44    /// Group name
45    pub name: String,
46    /// Child groups
47    pub groups: HashMap<String, Group>,
48    /// Datasets in this group
49    pub datasets: HashMap<String, Dataset>,
50    /// Attributes
51    pub attributes: HashMap<String, AttributeValue>,
52}
53impl Group {
54    /// Create a new empty group
55    pub fn new(name: String) -> Self {
56        Self {
57            name,
58            groups: HashMap::new(),
59            datasets: HashMap::new(),
60            attributes: HashMap::new(),
61        }
62    }
63    /// Create a subgroup
64    pub fn create_group(&mut self, name: &str) -> &mut Group {
65        self.groups
66            .entry(name.to_string())
67            .or_insert_with(|| Group::new(name.to_string()))
68    }
69    /// Get a subgroup
70    pub fn get_group(&self, name: &str) -> Option<&Group> {
71        self.groups.get(name)
72    }
73    /// Get a mutable subgroup
74    pub fn get_group_mut(&mut self, name: &str) -> Option<&mut Group> {
75        self.groups.get_mut(name)
76    }
77    /// Add an attribute
78    pub fn set_attribute(&mut self, name: &str, value: AttributeValue) {
79        self.attributes.insert(name.to_string(), value);
80    }
81    /// Get an attribute by name
82    pub fn get_attribute(&self, name: &str) -> Option<&AttributeValue> {
83        self.attributes.get(name)
84    }
85    /// Remove an attribute
86    pub fn remove_attribute(&mut self, name: &str) -> Option<AttributeValue> {
87        self.attributes.remove(name)
88    }
89    /// List all attribute names
90    pub fn attribute_names(&self) -> Vec<&str> {
91        self.attributes.keys().map(|s| s.as_str()).collect()
92    }
93    /// Check if group has a specific attribute
94    pub fn has_attribute(&self, name: &str) -> bool {
95        self.attributes.contains_key(name)
96    }
97    /// Get dataset by name
98    pub fn get_dataset(&self, name: &str) -> Option<&Dataset> {
99        self.datasets.get(name)
100    }
101    /// Get mutable dataset by name
102    pub fn get_dataset_mut(&mut self, name: &str) -> Option<&mut Dataset> {
103        self.datasets.get_mut(name)
104    }
105    /// List all dataset names
106    pub fn dataset_names(&self) -> Vec<&str> {
107        self.datasets.keys().map(|s| s.as_str()).collect()
108    }
109    /// List all group names
110    pub fn group_names(&self) -> Vec<&str> {
111        self.groups.keys().map(|s| s.as_str()).collect()
112    }
113    /// Check if group has a specific dataset
114    pub fn has_dataset(&self, name: &str) -> bool {
115        self.datasets.contains_key(name)
116    }
117    /// Check if group has a specific subgroup
118    pub fn has_group(&self, name: &str) -> bool {
119        self.groups.contains_key(name)
120    }
121    /// Remove a dataset
122    pub fn remove_dataset(&mut self, name: &str) -> Option<Dataset> {
123        self.datasets.remove(name)
124    }
125    /// Remove a subgroup
126    pub fn remove_group(&mut self, name: &str) -> Option<Group> {
127        self.groups.remove(name)
128    }
129}
130/// HDF5 dataset
131#[derive(Debug, Clone)]
132pub struct Dataset {
133    /// Dataset name
134    pub name: String,
135    /// Data type
136    pub dtype: HDF5DataType,
137    /// Shape
138    pub shape: Vec<usize>,
139    /// Data (stored as flattened array)
140    pub data: DataArray,
141    /// Attributes
142    pub attributes: HashMap<String, AttributeValue>,
143    /// Dataset options
144    pub options: DatasetOptions,
145}
146impl Dataset {
147    /// Create a new dataset
148    pub fn new(
149        name: String,
150        dtype: HDF5DataType,
151        shape: Vec<usize>,
152        data: DataArray,
153        options: DatasetOptions,
154    ) -> Self {
155        Self {
156            name,
157            dtype,
158            shape,
159            data,
160            attributes: HashMap::new(),
161            options,
162        }
163    }
164    /// Set an attribute on the dataset
165    pub fn set_attribute(&mut self, name: &str, value: AttributeValue) {
166        self.attributes.insert(name.to_string(), value);
167    }
168    /// Get an attribute by name
169    pub fn get_attribute(&self, name: &str) -> Option<&AttributeValue> {
170        self.attributes.get(name)
171    }
172    /// Remove an attribute
173    pub fn remove_attribute(&mut self, name: &str) -> Option<AttributeValue> {
174        self.attributes.remove(name)
175    }
176    /// Get the number of elements in the dataset
177    pub fn len(&self) -> usize {
178        self.shape.iter().product()
179    }
180    /// Check if the dataset is empty
181    pub fn is_empty(&self) -> bool {
182        self.len() == 0
183    }
184    /// Get the number of dimensions
185    pub fn ndim(&self) -> usize {
186        self.shape.len()
187    }
188    /// Get the total size in bytes (estimate)
189    pub fn size_bytes(&self) -> usize {
190        let element_size = match &self.dtype {
191            HDF5DataType::Integer { size, .. } => *size,
192            HDF5DataType::Float { size } => *size,
193            HDF5DataType::String { .. } => 8,
194            HDF5DataType::Array { .. } => 8,
195            HDF5DataType::Compound { .. } => 8,
196            HDF5DataType::Enum { .. } => 8,
197        };
198        self.len() * element_size
199    }
200    /// Get data as float vector (if possible)
201    pub fn as_float_vec(&self) -> Option<Vec<f64>> {
202        match &self.data {
203            DataArray::Float(data) => Some(data.clone()),
204            DataArray::Integer(data) => Some(data.iter().map(|&x| x as f64).collect()),
205            _ => None,
206        }
207    }
208    /// Get data as integer vector (if possible)
209    pub fn as_integer_vec(&self) -> Option<Vec<i64>> {
210        match &self.data {
211            DataArray::Integer(data) => Some(data.clone()),
212            DataArray::Float(data) => Some(data.iter().map(|&x| x as i64).collect()),
213            _ => None,
214        }
215    }
216    /// Get data as string vector (if possible)
217    pub fn as_string_vec(&self) -> Option<Vec<String>> {
218        match &self.data {
219            DataArray::String(data) => Some(data.clone()),
220            _ => None,
221        }
222    }
223}
224/// HDF5 data type enumeration
225#[derive(Debug, Clone, PartialEq)]
226pub enum HDF5DataType {
227    /// Integer types
228    Integer {
229        /// Size in bytes (1, 2, 4, or 8)
230        size: usize,
231        /// Whether the integer is signed
232        signed: bool,
233    },
234    /// Floating point types
235    Float {
236        /// Size in bytes (4 or 8)
237        size: usize,
238    },
239    /// String type
240    String {
241        /// String encoding (UTF-8 or ASCII)
242        encoding: StringEncoding,
243    },
244    /// Array type
245    Array {
246        /// Base data type of array elements
247        base_type: Box<HDF5DataType>,
248        /// Shape of the array
249        shape: Vec<usize>,
250    },
251    /// Compound type
252    Compound {
253        /// Fields in the compound type (name, type) pairs
254        fields: Vec<(String, HDF5DataType)>,
255    },
256    /// Enum type
257    Enum {
258        /// Enumeration values (name, value) pairs
259        values: Vec<(String, i64)>,
260    },
261}
262/// HDF5 compression options
263#[derive(Debug, Clone, Default)]
264pub struct CompressionOptions {
265    /// Enable gzip compression
266    pub gzip: Option<u8>,
267    /// Enable szip compression
268    pub szip: Option<(u32, u32)>,
269    /// Enable LZF compression
270    pub lzf: bool,
271    /// Enable shuffle filter
272    pub shuffle: bool,
273}
274/// HDF5 dataset creation options
275#[derive(Debug, Clone, Default)]
276pub struct DatasetOptions {
277    /// Chunk size for chunked storage
278    pub chunk_size: Option<Vec<usize>>,
279    /// Compression options
280    pub compression: CompressionOptions,
281    /// Fill value for uninitialized elements
282    pub fill_value: Option<f64>,
283    /// Enable fletcher32 checksum
284    pub fletcher32: bool,
285}