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
#[cfg(feature = "std")]
extern crate std;

extern crate alloc;

use alloc::string::String;
use core::num::TryFromIntError;
use derive_builder::UninitializedFieldError;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BLASError {
    OverflowDimension(String),
    InvalidDim(String),
    InvalidFlag(String),
    FailedCheck(String),
    UninitializedField(&'static str),
    ExplicitCopy(String),
    Miscellaneous(String),
}

/* #region impl BLASError */

#[cfg(feature = "std")]
impl std::error::Error for BLASError {}

impl From<UninitializedFieldError> for BLASError {
    fn from(e: UninitializedFieldError) -> BLASError {
        BLASError::UninitializedField(e.field_name())
    }
}

impl From<TryFromIntError> for BLASError {
    fn from(_: TryFromIntError) -> BLASError {
        BLASError::OverflowDimension(String::from("TryFromIntError"))
    }
}

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

/* #endregion */

/* #region macros */

#[macro_export]
macro_rules! blas_assert {
    ($cond:expr, $errtype:ident, $($arg:tt)*) => {
        if $cond {
            Ok(())
        } else {
            extern crate alloc;
            use alloc::string::String;
            Err(BLASError::$errtype(String::from(concat!(
                file!(), ":", line!(), ": ", "BLASError::", stringify!($errtype), " : ",
                $($arg),*, ": ", stringify!($cond)
            ))))
        }
    };
    ($cond:expr, $errtype:ident) => {
        if $cond {
            Ok(())
        } else {
            extern crate alloc;
            use alloc::string::String;
            Err(BLASError::$errtype(String::from(concat!(
                file!(), ":", line!(), ": ", "BLASError::", stringify!($errtype), " : ",
                stringify!($cond)
            ))))
        }
    };
}

#[macro_export]
macro_rules! blas_assert_eq {
    ($a:expr, $b:expr, $errtype:ident) => {
        if $a == $b {
            Ok(())
        } else {
            extern crate alloc;
            use alloc::string::String;
            use core::fmt::Write;
            let mut s = String::from(concat!(
                file!(),
                ":",
                line!(),
                ": ",
                "BLASError::",
                stringify!($errtype),
                " : "
            ));
            write!(s, "{:?} = {:?} not equal to {:?} = {:?}", stringify!($a), $a, stringify!($b), $b)
                .unwrap();
            Err(BLASError::$errtype(s))
        }
    };
}

#[macro_export]
macro_rules! blas_raise {
    ($errtype:ident) => {{
        extern crate alloc;
        use alloc::string::String;
        Err(BLASError::$errtype(String::from(concat!(
            file!(), ":", line!(), ": ", "BLASError::", stringify!($errtype)
        ))))
    }};
    ($errtype:ident, $($arg:tt)*) => {{
        extern crate alloc;
        use alloc::string::String;
        Err(BLASError::$errtype(String::from(concat!(
            file!(), ":", line!(), ": ", "BLASError::", stringify!($errtype), " : ",
            $($arg),*
        ))))
    }};
}

#[macro_export]
macro_rules! blas_invalid {
    ($word:expr) => {{
        extern crate alloc;
        use alloc::string::String;
        use core::fmt::Write;
        let mut s = String::from(concat!(file!(), ":", line!(), ": ", "BLASError::InvalidFlag", " : "));
        write!(s, "{:?} = {:?}", stringify!($word), $word).unwrap();
        Err(BLASError::InvalidFlag(s))
    }};
}

/* #endregion */

/* #region macros (warning) */

#[macro_export]
macro_rules! blas_warn_layout_clone {
    ($array:expr) => {{
        #[cfg(feature = "std")]
        extern crate std;

        if cfg!(all(feature = "std", feature = "warn_on_copy")) {
            std::eprintln!(
                "Warning: Copying array due to non-standard layout, shape={:?}, strides={:?}",
                $array.shape(),
                $array.strides()
            );
            Result::<(), BLASError>::Ok(())
        } else if cfg!(feature = "error_on_copy") {
            blas_raise!(ExplicitCopy)
        } else {
            Result::<(), BLASError>::Ok(())
        }
    }};
    ($array:expr, $msg:tt) => {{
        #[cfg(feature = "std")]
        extern crate std;

        if cfg!(all(feature = "std", feature = "warn_on_copy")) {
            std::eprintln!("Warning: {:?}, shape={:?}, strides={:?}", $msg, $array.shape(), $array.strides());
            Result::<(), BLASError>::Ok(())
        } else if cfg!(feature = "error_on_copy") {
            blas_raise!(ExplicitCopy)
        } else {
            Result::<(), BLASError>::Ok(())
        }
    }};
}

/* #endregion */