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
macro_rules! fail {
    ($err:expr) => (
        return Err(From::from($err));
    );

    ($fmt:expr, $($arg:tt)*) => (
        fail!(format!($fmt, $($arg)*))
    );
}

macro_rules! try_ref_clone {
    ($expr:expr) => (
        match $expr {
            Ok(ref val) => val,
            Err(ref err) => {
                return Err(From::from(err.clone()))
            }
        }
    )
}

macro_rules! ensure {
    ($expr:expr, $err:expr) => (
        if !($expr) {
            fail!($err);
        }
    );
    ($expr: expr, $fmt:expr, $($arg:tt)*) => (
        if !($expr) {
            fail!(format!($fmt, $($arg)*));
        }
    );
}

/// Panics if `$expr` is not an Err(err) with err.description() matching regexp `$err`.
macro_rules! assert_err {
    ($expr:expr, $err:expr) => {
        match &($expr) {
            &Ok(_) => {
                panic!("assertion failed: not an error in `{}`", stringify!($expr));
            }
            &Err(ref value) => {
                use regex::Regex;
                use std::error::Error as BaseError;
                let re = Regex::new($err).unwrap();
                let desc = value.description().to_string();
                if !re.is_match(desc.as_ref()) {
                    panic!(
                        "assertion failed: error message \"{}\" doesn't match \"{}\" in `{}`",
                        desc, re, stringify!($expr)
                    );
                }
            }
        }
    }
}

/// Run a safe expression in a closure synchronized by a global reentrant mutex.
#[macro_export]
macro_rules! h5lock_s {
    ($expr:expr) => (
        $crate::sync::sync(|| { $expr })
    )
}

/// Run an unsafe expression in a closure synchronized by a global reentrant mutex.
#[macro_export]
macro_rules! h5lock {
    ($expr:expr) => (
        h5lock_s!(unsafe { $expr })
    )
}

#[macro_export]
macro_rules! h5call_s {
    ($expr:expr) => (
        h5lock_s!($crate::error::h5check($expr))
    )
}

#[macro_export]
macro_rules! h5call {
    ($expr:expr) => (
        h5call_s!(unsafe { $expr })
    )
}

#[macro_export]
macro_rules! h5try_s {
    ($expr:expr) => (match h5call_s!($expr) {
        Ok(value) => value,
        Err(err)  => {
            return Err(From::from(err))
        },
    })
}

#[macro_export]
macro_rules! h5try {
    ($expr:expr) => (
        h5try_s!(unsafe { $expr })
    )
}