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
pub type AnyError = Box<dyn std::error::Error>;

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct BLASError(pub String);

impl std::error::Error for BLASError {}

impl BLASError {
    #[inline]
    pub fn assert(cond: bool, s: String) -> Result<(), BLASError> {
        match cond {
            true => Ok(()),
            false => Err(BLASError(s)),
        }
    }
}

#[macro_export]
macro_rules! blas_assert {
    ($cond:expr, $($arg:tt)*) => {
        BLASError::assert($cond,
            format!("{:}:{:}: ", file!(), line!()) + &format!($($arg)*)
        )
    };
}

#[macro_export]
macro_rules! blas_assert_eq {
    ($a:expr, $b:expr, $($arg:tt)*) => {
        BLASError::assert($a == $b,
            format!("{:}:{:}: ", file!(), line!()) + &format!($($arg)*) + &format!(": {:} = {:?}, {:} = {:?}", stringify!($a), $a, stringify!($b), $b)
        )
    };
    ($a:expr, $b:expr) => {
        BLASError::assert($a == $b,
            format!("{:}:{:}: ", file!(), line!()) + &format!(": {:} = {:?}, {:} = {:?}", stringify!($a), $a, stringify!($b), $b)
        )
    };
}

#[macro_export]
macro_rules! blas_raise {
    ($($arg:tt)*) => {
        Err(BLASError(format!("{:}:{:}: ", file!(), line!()) + &format!($($arg)*)))
    };
}

#[macro_export]
macro_rules! blas_invalid {
    ($word:expr) => {
        Err(BLASError(
            format!("{:}:{:}: ", file!(), line!())
                + &format!("Invalid keyowrd {:} = {:?}", stringify!($word), $word),
        ))
    };
}

impl std::fmt::Display for BLASError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}