matrixlab/
error.rs

1//
2//    matrixlab, a library for working with sparse matricies
3//    Copyright (C) 2019 Waylon Cude
4//
5//    This program is free software: you can redistribute it and/or modify
6//    it under the terms of the GNU General Public License as published by
7//    the Free Software Foundation, either version 3 of the License, or
8//    (at your option) any later version.
9//
10//    This program is distributed in the hope that it will be useful,
11//    but WITHOUT ANY WARRANTY; without even the implied warranty of
12//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13//    GNU General Public License for more details.
14//
15//    You should have received a copy of the GNU General Public License
16//    along with this program.  If not, see <https://www.gnu.org/licenses/>.
17//    
18
19
20/// This is the general error type for our library. It can represent any failed
21/// operation on a matrix, including building a new matrix or a failed operation
22/// like matrix multiplication
23#[derive(Debug)]
24pub enum Error {
25    /// This is returned when an operation fails due to a mismatch between the
26    /// number of rows or columns in a matrix. This will happen when you multiply
27    /// a AxB matrix by a CxD matrix, where B != C.
28    SizeMismatch,
29    /// This is returned when we get an error due to input that doesn't represent
30    /// a valid matrix or vector
31    MalformedInput,
32    /// This is returned when there is an element that is out of bounds of the
33    /// matrix during creation of a matrix.
34    ElementOutOfBounds,
35    /// This is returned when there is an element that is at the same location
36    /// as another element during creation of a matrix.
37    DuplicateElements,
38    /// This error is a container for an IO error, returned when we would otherwise
39    /// get an error when reading from a file
40    IOError(std::io::Error),
41    InvalidFile,
42    /// This error contains our closest guess for GMRES
43    ExceededIterations(Vec<f64>)
44
45}
46impl PartialEq for Error {
47    fn eq(&self, other: &Self) -> bool {
48        use Error::*;
49        match (self,other) {
50            (SizeMismatch,SizeMismatch) => true,
51            (MalformedInput,MalformedInput) => true,
52            (ElementOutOfBounds,ElementOutOfBounds) => true,
53            (DuplicateElements,DuplicateElements) => true,
54            (IOError(_),IOError(_)) => true,
55            (InvalidFile,InvalidFile) => true,
56            (ExceededIterations(_),ExceededIterations(_)) => true,
57            _ => false
58        }
59    }
60}
61impl From<std::io::Error> for Error {
62    fn from(other: std::io::Error) -> Self {
63        Error::IOError(other)
64    }
65}
66impl From<std::num::ParseFloatError> for Error {
67    fn from(_other: std::num::ParseFloatError) -> Self {
68        Error::InvalidFile
69    }
70}