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
use std::collections::BTreeMap;

use crate::error::{Error, Result};
use crate::format::messages::link::LinkMessage;
use crate::hl::group::{Group, LinkInfo, LinkValue};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinkClass {
    pub id: u8,
    pub name: String,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct LinkClassRegistry {
    classes: BTreeMap<u8, LinkClass>,
    external_registered: bool,
}

impl LinkClassRegistry {
    pub fn init() -> Self {
        let mut registry = Self::default();
        registry.register(LinkClass {
            id: 0,
            name: "hard".to_string(),
        });
        registry.register(LinkClass {
            id: 1,
            name: "soft".to_string(),
        });
        registry.register_external();
        registry
    }

    pub fn init_package() -> Self {
        Self::init()
    }

    pub fn term_package(&mut self) {
        self.classes.clear();
        self.external_registered = false;
    }

    pub fn register_external(&mut self) {
        self.external_registered = true;
        self.register(LinkClass {
            id: 64,
            name: "external".to_string(),
        });
    }

    pub fn find_class_idx(&self, id: u8) -> Option<usize> {
        self.classes.keys().position(|class_id| *class_id == id)
    }

    pub fn find_class(&self, id: u8) -> Option<&LinkClass> {
        self.classes.get(&id)
    }

    pub fn register(&mut self, class: LinkClass) {
        self.classes.insert(class.id, class);
    }

    pub fn unregister(&mut self, id: u8) -> Option<LinkClass> {
        if id == 64 {
            self.external_registered = false;
        }
        self.classes.remove(&id)
    }

    pub fn is_registered(&self, id: u8) -> bool {
        self.classes.contains_key(&id)
    }

    pub fn register_api(&mut self, class: LinkClass) {
        self.register(class);
    }

    pub fn unregister_api(&mut self, id: u8) -> Option<LinkClass> {
        self.unregister(id)
    }

    pub fn is_registered_api(&self, id: u8) -> bool {
        self.is_registered(id)
    }
}

pub fn extern_traverse(group: &Group, name: &str) -> Result<Option<(String, String)>> {
    Ok(group.find_link_by_name(name)?.external_link)
}

pub fn link(group: &Group, name: &str) -> Result<LinkMessage> {
    group.find_link_by_name(name)
}

pub fn link_object(group: &Group, name: &str) -> Result<u64> {
    group
        .find_link_by_name(name)?
        .hard_link_addr
        .ok_or_else(|| {
            Error::InvalidFormat(format!("link '{name}' does not reference an object header"))
        })
}

pub fn create_soft_api_common(
    writer: &mut crate::hl::writable_file::WritableFile,
    name: &str,
    target: &str,
) -> Result<()> {
    writer.link_soft(name, target)
}

pub fn create_hard_api_common(
    writer: &mut crate::hl::writable_file::WritableFile,
    name: &str,
    target: &str,
) -> Result<()> {
    writer.link_hard(name, target)
}

pub fn create_real(group: &Group, name: &str) -> Result<u64> {
    link_object(group, name)
}

pub fn create_soft(
    writer: &mut crate::hl::writable_file::WritableFile,
    name: &str,
    target: &str,
) -> Result<()> {
    writer.link_soft(name, target)
}

pub fn create_soft_async(
    writer: &mut crate::hl::writable_file::WritableFile,
    name: &str,
    target: &str,
) -> Result<()> {
    create_soft(writer, name, target)
}

pub fn create_hard(
    writer: &mut crate::hl::writable_file::WritableFile,
    name: &str,
    target: &str,
) -> Result<()> {
    writer.link_hard(name, target)
}

pub fn create_hard_async(
    writer: &mut crate::hl::writable_file::WritableFile,
    name: &str,
    target: &str,
) -> Result<()> {
    create_hard(writer, name, target)
}

pub fn create_external(
    writer: &mut crate::hl::writable_file::WritableFile,
    name: &str,
    filename: &str,
    object_path: &str,
) -> Result<()> {
    writer.link_external(name, filename, object_path)
}

pub fn create_ud(
    writer: &mut crate::hl::writable_file::WritableFile,
    name: &str,
    filename: &str,
    object_path: &str,
) -> Result<()> {
    writer.link_external(name, filename, object_path)
}

pub fn create_ud_api(
    writer: &mut crate::hl::writable_file::WritableFile,
    name: &str,
    filename: &str,
    object_path: &str,
) -> Result<()> {
    create_ud(writer, name, filename, object_path)
}

pub fn get_val_cb(link: &LinkMessage) -> Option<LinkValue> {
    if let Some(target) = &link.soft_link_target {
        return Some(LinkValue::Soft(target.clone()));
    }
    link.external_link
        .as_ref()
        .map(|(filename, object_path)| LinkValue::External {
            filename: filename.clone(),
            object_path: object_path.clone(),
        })
}

pub fn get_val(group: &Group, name: &str) -> Result<Option<LinkValue>> {
    Ok(get_val_cb(&group.find_link_by_name(name)?))
}

pub fn get_val_by_idx_cb(link: &LinkMessage) -> Option<LinkValue> {
    get_val_cb(link)
}

pub fn get_val_by_idx(group: &Group, index: usize) -> Result<Option<LinkValue>> {
    group.link_value_by_idx(index)
}

pub fn exists_final_cb(group: &Group, name: &str) -> Result<bool> {
    group.link_exists(name)
}

pub fn exists_inter_cb(group: &Group, name: &str) -> Result<bool> {
    group.link_exists(name)
}

pub fn exists_tolerant(group: &Group, name: &str) -> bool {
    group.link_exists(name).unwrap_or(false)
}

pub fn exists(group: &Group, name: &str) -> Result<bool> {
    group.link_exists(name)
}

pub fn exists_api_common(group: &Group, name: &str) -> Result<bool> {
    exists(group, name)
}

pub fn get_info_by_idx_cb(link: &LinkMessage) -> Result<LinkInfo> {
    super::group::link_info_from_message(link)
}

pub fn get_info_by_idx(group: &Group, index: usize) -> Result<LinkInfo> {
    group.link_info_by_idx(index)
}

pub fn get_name_by_idx(group: &Group, index: usize) -> Result<String> {
    group.link_name_by_idx(index)
}

pub fn link_copy_file(link: &LinkMessage) -> LinkMessage {
    link.clone()
}

pub fn copy(link: &LinkMessage) -> LinkMessage {
    link_copy_file(link)
}

pub fn iterate(group: &Group) -> Result<Vec<LinkMessage>> {
    group.links()
}

pub fn iterate_by_name2(group: &Group) -> Result<Vec<LinkMessage>> {
    iterate(group)
}

pub fn visit2(group: &Group) -> Result<Vec<LinkMessage>> {
    iterate(group)
}

pub fn visit_by_name2(group: &Group) -> Result<Vec<LinkMessage>> {
    iterate(group)
}

pub fn iterate1(group: &Group) -> Result<Vec<LinkMessage>> {
    iterate(group)
}

pub fn iterate_by_name1(group: &Group) -> Result<Vec<LinkMessage>> {
    iterate(group)
}

pub fn visit1(group: &Group) -> Result<Vec<LinkMessage>> {
    iterate(group)
}

pub fn visit_by_name1(group: &Group) -> Result<Vec<LinkMessage>> {
    iterate(group)
}

pub fn get_ocrt_info(link: &LinkMessage) -> Option<u64> {
    link.creation_order
}

pub fn iterate_api_common(group: &Group) -> Result<Vec<LinkMessage>> {
    iterate(group)
}

pub fn iterate2_shim(group: &Group) -> Result<Vec<LinkMessage>> {
    iterate(group)
}

#[derive(Debug, Clone, Default)]
pub struct LinkTable {
    links: BTreeMap<String, LinkMessage>,
}

impl LinkTable {
    pub fn from_links(links: impl IntoIterator<Item = LinkMessage>) -> Self {
        let links = links
            .into_iter()
            .map(|link| (link.name.clone(), link))
            .collect();
        Self { links }
    }

    pub fn insert(&mut self, link: LinkMessage) -> Option<LinkMessage> {
        self.links.insert(link.name.clone(), link)
    }

    pub fn delete(&mut self, name: &str) -> Result<LinkMessage> {
        self.links
            .remove(name)
            .ok_or_else(|| Error::InvalidFormat(format!("link '{name}' not found")))
    }

    pub fn delete_by_idx(&mut self, index: usize) -> Result<LinkMessage> {
        let name = self
            .links
            .keys()
            .nth(index)
            .cloned()
            .ok_or_else(|| Error::InvalidFormat(format!("link index {index} out of range")))?;
        self.delete(&name)
    }

    pub fn move_link(&mut self, old_name: &str, new_name: &str) -> Result<()> {
        if self.links.contains_key(new_name) {
            return Err(Error::InvalidFormat(format!(
                "destination link '{new_name}' already exists"
            )));
        }
        let mut link = self.delete(old_name)?;
        link.name = new_name.to_string();
        self.insert(link);
        Ok(())
    }
}

#[allow(non_snake_case)]
pub fn H5L__delete_cb(table: &mut LinkTable, name: &str) -> Result<LinkMessage> {
    table.delete(name)
}

#[allow(non_snake_case)]
pub fn H5L__delete(table: &mut LinkTable, name: &str) -> Result<LinkMessage> {
    H5L__delete_cb(table, name)
}

#[allow(non_snake_case)]
pub fn H5L__delete_by_idx_cb(table: &mut LinkTable, index: usize) -> Result<LinkMessage> {
    table.delete_by_idx(index)
}

#[allow(non_snake_case)]
pub fn H5L__delete_by_idx(table: &mut LinkTable, index: usize) -> Result<LinkMessage> {
    H5L__delete_by_idx_cb(table, index)
}

#[allow(non_snake_case)]
pub fn H5L__delete_by_idx_api_common(table: &mut LinkTable, index: usize) -> Result<LinkMessage> {
    H5L__delete_by_idx(table, index)
}

#[allow(non_snake_case)]
pub fn H5L__move(table: &mut LinkTable, old_name: &str, new_name: &str) -> Result<()> {
    table.move_link(old_name, new_name)
}

#[allow(non_snake_case)]
pub fn H5Lmove(table: &mut LinkTable, old_name: &str, new_name: &str) -> Result<()> {
    H5L__move(table, old_name, new_name)
}

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

    #[test]
    fn link_registry_tracks_builtin_classes() {
        let mut registry = LinkClassRegistry::init();
        assert!(registry.is_registered(0));
        assert!(registry.is_registered(1));
        assert!(registry.is_registered(64));
        assert_eq!(registry.find_class_idx(64), Some(2));
        registry.unregister(64);
        assert!(!registry.is_registered(64));
    }

    #[test]
    fn link_table_deletes_and_moves_links() {
        let mut table = LinkTable::default();
        table.insert(LinkMessage {
            link_type: crate::format::messages::link::LinkType::Soft,
            creation_order: None,
            char_encoding: 0,
            name: "old".into(),
            hard_link_addr: None,
            soft_link_target: Some("/target".into()),
            external_link: None,
        });
        H5Lmove(&mut table, "old", "new").unwrap();
        assert!(H5L__delete(&mut table, "new").is_ok());
        assert!(H5L__delete(&mut table, "new").is_err());
    }
}