permu_rs/
errors.rs

1use std::fmt;
2use std::io;
3
4/// Contains all errors `permu-rs` functions can return.
5#[derive(Debug)]
6pub enum Error {
7    /// Error returned when the shape of the given vector was not the expected.
8    LengthError,
9    /// Error to return when a `Permutation` is not an actual permutation.
10    NotPermutation,     
11    /// Error to return when an incorrect `Distribution` type is given.
12    IncorrectDistrType,
13    /// Error to return when a parsing error occurs.
14    ParseError,
15    /// IO error containing a std::io::Error that is caused.
16    Io(io::Error),
17    /// Error to return when an incorrect problem instance is given.
18    IncorrectProblemInstance,
19    /// Error to return when an incorrect `Population` is given.
20    IncorrectPopulation,
21}
22
23impl fmt::Display for Error {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match &self {
26            Error::LengthError => write!(f, "LenghtError: Please check the shape of the given argument"),
27            Error::NotPermutation => write!(f, "NotPermutation: permutation expected but no permutation vector was found"),
28            Error::IncorrectDistrType => write!(f, "IncorrectDistrType: Incorrect distribution given"),
29            Error::ParseError => write!(f, "ParseError: Error occurred during a parse operation"),
30            Error::Io(err) => write!(f, "IO Error: {}", err),
31            Error::IncorrectProblemInstance => write!(f, "Incorrect distribution type"),
32            Error::IncorrectPopulation => write!(f, "Incorrect population given"),
33        }
34    }
35}
36
37// Implement io error to permu-rs error conversion
38impl From<io::Error> for Error {
39    fn from(err: io::Error) -> Error {
40        Error::Io(err)
41    }
42}
43/*
44/// Error type to return when transforming between representations and the 
45/// length of one of the vectors is not correct
46#[derive(Debug)]
47pub struct LengthError {
48    message: Option<String>,
49}
50
51impl LengthError {
52
53    /// Creates a `LengthError` object with the default error messge.
54    /// # Example
55    /// ```
56    /// use permu_rs::LengthError;
57    /// let my_error = LengthError::new();
58    /// ```
59    pub fn new() -> LengthError {
60        LengthError { message : None }
61    }
62    
63    /// Creates a `LengthError` object including a given custom error message.
64    /// # Example
65    /// ```
66    /// use permu_rs::LengthError;
67    /// let my_error = LengthError::from(String::from("Super custom message"));
68    /// ```
69    pub fn from(m: String) -> LengthError {
70        LengthError { message : Some(m) }
71    }
72}
73
74impl fmt::Display for LengthError {
75    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        match &self.message {
77            Some(m) => write!(f, "{}", m),
78            None => write!(f, "Please check the lengths or shapes of the given arguments"),
79        }
80    }
81}
82
83/// Error type to return when a `Permutation` is not an actual permutation.
84#[derive(Debug)]
85pub struct NotPermutation;
86
87impl fmt::Display for NotPermutation {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        write!(f, "Permutation expected but no permutation found")
90    }
91}
92
93#[derive(Debug)]
94/// Error to return when an incorrect `Distribution` type is given.
95pub struct IncorrectDistrType;
96
97impl fmt::Display for IncorrectDistrType {
98    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99        write!(f, "Incorrect distribution given")
100    }
101}
102*/