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
use std::fmt::{self, Debug};
use std::ops::Deref;
use std::ptr;
use hdf5_sys::{
h5f::H5Fget_name,
h5i::{H5Iget_file_id, H5Iget_name},
h5o::{H5Oget_comment, H5Oset_comment},
};
use crate::internal_prelude::*;
#[repr(transparent)]
#[derive(Clone)]
pub struct Location(Handle);
impl ObjectClass for Location {
const NAME: &'static str = "location";
const VALID_TYPES: &'static [H5I_type_t] =
&[H5I_FILE, H5I_GROUP, H5I_DATATYPE, H5I_DATASET, H5I_ATTR];
fn from_handle(handle: Handle) -> Self {
Self(handle)
}
fn handle(&self) -> &Handle {
&self.0
}
fn short_repr(&self) -> Option<String> {
Some(format!("\"{}\"", self.name()))
}
}
impl Debug for Location {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.debug_fmt(f)
}
}
impl Deref for Location {
type Target = Object;
fn deref(&self) -> &Object {
unsafe { self.transmute() }
}
}
impl Location {
pub fn name(&self) -> String {
h5lock!(get_h5_str(|m, s| H5Iget_name(self.id(), m, s)).unwrap_or_else(|_| "".to_string()))
}
pub fn filename(&self) -> String {
h5lock!(get_h5_str(|m, s| H5Fget_name(self.id(), m, s)).unwrap_or_else(|_| "".to_string()))
}
pub fn file(&self) -> Result<File> {
File::from_id(h5try!(H5Iget_file_id(self.id())))
}
pub fn comment(&self) -> Option<String> {
let comment = h5lock!(get_h5_str(|m, s| H5Oget_comment(self.id(), m, s)).ok());
comment.and_then(|c| if c.is_empty() { None } else { Some(c) })
}
pub fn set_comment(&self, comment: &str) -> Result<()> {
let comment = to_cstring(comment)?;
h5call!(H5Oset_comment(self.id(), comment.as_ptr())).and(Ok(()))
}
pub fn clear_comment(&self) -> Result<()> {
h5call!(H5Oset_comment(self.id(), ptr::null_mut())).and(Ok(()))
}
}
#[cfg(test)]
pub mod tests {
use crate::internal_prelude::*;
#[test]
pub fn test_filename() {
with_tmp_path(|path| {
assert_eq!(File::create(&path).unwrap().filename(), path.to_str().unwrap());
})
}
#[test]
pub fn test_name() {
with_tmp_file(|file| {
assert_eq!(file.name(), "/");
})
}
#[test]
pub fn test_file() {
with_tmp_file(|file| {
assert_eq!(file.file().unwrap().id(), file.id());
})
}
#[test]
pub fn test_comment() {
with_tmp_file(|file| {
assert!(file.comment().is_none());
assert!(file.set_comment("foo").is_ok());
assert_eq!(file.comment().unwrap(), "foo");
assert!(file.clear_comment().is_ok());
assert!(file.comment().is_none());
})
}
}