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
//! Interface to [HDF5][1].
//!
//! ## Example
//!
//! ```
//! extern crate hdf5;
//! # extern crate temporary;
//!
//! use hdf5::File;
//! # use temporary::Directory;
//!
//! # fn main() {
//! let path = "data.h5";
//! # let directory = Directory::new("hdf5").unwrap();
//! # let path = directory.join(path);
//! let file = File::new(path).unwrap();
//!
//! file.write("foo", 42).unwrap();
//! file.write("bar", &vec![42.0, 69.0]).unwrap();
//! # }
//! ```
//!
//! Structural data can be written using [`rustc-serialize`][2] as follows:
//!
//! ```
//! extern crate hdf5;
//! extern crate rustc_serialize;
//! # extern crate temporary;
//!
//! use hdf5::File;
//! # use temporary::Directory;
//!
//! #[derive(RustcEncodable)]
//! struct Foo {
//!     bar: Vec<f64>,
//!     baz: Baz,
//! }
//!
//! #[derive(RustcEncodable)]
//! struct Baz {
//!     qux: f64,
//! }
//!
//! # fn main() {
//! let foo = Foo {
//!     bar: vec![42.0],
//!     baz: Baz {
//!         qux: 69.0,
//!     },
//! };
//!
//! let path = "data.h5";
//! # let directory = Directory::new("hdf5").unwrap();
//! # let path = directory.join(path);
//! let file = File::new(path).unwrap();
//!
//! file.encode("foo", &foo).unwrap();
//! # }
//!
//! [1]: http://www.hdfgroup.org/HDF5
//! [2]: https://crates.io/crates/rustc-serialize

extern crate hdf5_sys as ffi;
extern crate libc;

#[cfg(feature = "serialize")]
extern crate rustc_serialize;

use std::{error, fmt};

/// An error.
#[derive(Clone, Debug)]
pub struct Error(String);

#[doc(hidden)]
pub type ID = ffi::hid_t;

#[doc(hidden)]
pub trait Identity {
    fn id(&self) -> ID;
}

#[doc(hidden)]
pub trait Location: Identity {
}

/// A result.
pub type Result<T> = std::result::Result<T, Error>;

macro_rules! identity(
    ($name:ident) => (
        impl ::Identity for $name {
            #[inline]
            fn id(&self) -> ::ID {
                self.id
            }
        }
    );
);

macro_rules! location(
    ($name:ident) => (
        impl ::Location for $name {
        }
    );
);

macro_rules! ok(
    ($call:expr) => ({
        let result = unsafe { $call };
        if result < 0 {
            raise!("failed to call a native function (error code {})", result);
        }
        result
    });
    ($call:expr, $($arg:tt)+) => ({
        let result = unsafe { $call };
        if result < 0 {
            raise!($($arg)+);
        }
        result
    });
);

macro_rules! path_to_cstr(
    ($path:expr) => ({
        let path = $path;
        match path.to_str() {
            Some(path) => match ::std::ffi::CString::new(path) {
                Ok(string) => string,
                _ => raise!("failed to process a path {:?}", path),
            },
            _ => raise!("failed to process a path {:?}", path),
        }
    });
);

macro_rules! product(
    ($vector:expr) => (
        $vector.iter().fold(1, |result, &next| result * next)
    );
);

macro_rules! raise(
    ($($arg:tt)*) => (return Err(::Error(format!($($arg)*))));
);

macro_rules! str_to_cstr(
    ($string:expr) => ({
        let string = $string;
        match ::std::ffi::CString::new(string) {
            Ok(string) => string,
            _ => raise!("failed to process a string {:?}", string),
        }
    });
);

macro_rules! whatever(
    ($call:expr) => ({
        let _ = unsafe { $call };
    });
);

impl fmt::Display for Error {
    #[inline]
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        self.0.fmt(formatter)
    }
}

impl error::Error for Error {
    #[inline]
    fn description(&self) -> &str {
        &self.0
    }
}

impl<'l, T: Identity> Identity for &'l T {
    #[inline]
    fn id(&self) -> ID {
        (*self).id()
    }
}

impl<'l, T: Location> Location for &'l T {
}

/// Return the version number of HDF5.
pub fn version() -> Result<(usize, usize, usize)> {
    let (mut major, mut minor, mut patch) = (0, 0, 0);
    ok!(ffi::H5get_libversion(&mut major as *mut _ as *mut _, &mut minor as *mut _ as *mut _,
                              &mut patch as *mut _ as *mut _));
    Ok((major, minor, patch))
}

mod data;
mod dataset;
mod dataspace;
mod datatype;
mod file;
mod link;
mod writer;

#[cfg(feature = "serialize")]
mod decoder;
#[cfg(feature = "serialize")]
mod encoder;

pub use data::{Data, IntoData, Slice};
pub use datatype::Datatype;
pub use file::File;
pub use writer::Writer;

#[cfg(feature = "serialize")]
pub use decoder::Decoder;
#[cfg(feature = "serialize")]
pub use encoder::Encoder;