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
//! Define Errors

use std::error;
use std::fmt;
use ndarray::Ixs;

#[derive(Debug)]
pub struct LapackError {
    pub return_code: i32,
}

impl fmt::Display for LapackError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "LAPACK: return_code = {}", self.return_code)
    }
}

impl error::Error for LapackError {
    fn description(&self) -> &str {
        "LAPACK subroutine returns non-zero code"
    }
}

impl From<i32> for LapackError {
    fn from(code: i32) -> LapackError {
        LapackError { return_code: code }
    }
}

#[derive(Debug)]
pub struct NotSquareError {
    pub rows: usize,
    pub cols: usize,
}

impl fmt::Display for NotSquareError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Not square: rows({}) != cols({})", self.rows, self.cols)
    }
}

impl error::Error for NotSquareError {
    fn description(&self) -> &str {
        "Matrix is not square"
    }
}

#[derive(Debug)]
pub struct StrideError {
    pub s0: Ixs,
    pub s1: Ixs,
}

impl fmt::Display for StrideError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "invalid stride: s0={}, s1={}", self.s0, self.s1)
    }
}

impl error::Error for StrideError {
    fn description(&self) -> &str {
        "invalid stride"
    }
}

#[derive(Debug)]
pub enum LinalgError {
    NotSquare(NotSquareError),
    Lapack(LapackError),
    Stride(StrideError),
}

impl fmt::Display for LinalgError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            LinalgError::NotSquare(ref err) => err.fmt(f),
            LinalgError::Lapack(ref err) => err.fmt(f),
            LinalgError::Stride(ref err) => err.fmt(f),
        }
    }
}

impl error::Error for LinalgError {
    fn description(&self) -> &str {
        match *self {
            LinalgError::NotSquare(ref err) => err.description(),
            LinalgError::Lapack(ref err) => err.description(),
            LinalgError::Stride(ref err) => err.description(),
        }
    }
}

impl From<NotSquareError> for LinalgError {
    fn from(err: NotSquareError) -> LinalgError {
        LinalgError::NotSquare(err)
    }
}

impl From<LapackError> for LinalgError {
    fn from(err: LapackError) -> LinalgError {
        LinalgError::Lapack(err)
    }
}

impl From<StrideError> for LinalgError {
    fn from(err: StrideError) -> LinalgError {
        LinalgError::Stride(err)
    }
}