ndarray_stats/
errors.rs

1//! Custom errors returned from our methods and functions.
2use noisy_float::types::N64;
3use std::error::Error;
4use std::fmt;
5
6/// An error that indicates that the input array was empty.
7#[derive(Clone, Debug, Eq, PartialEq)]
8pub struct EmptyInput;
9
10impl fmt::Display for EmptyInput {
11    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12        write!(f, "Empty input.")
13    }
14}
15
16impl Error for EmptyInput {}
17
18/// An error computing a minimum/maximum value.
19#[derive(Clone, Debug, Eq, PartialEq)]
20pub enum MinMaxError {
21    /// The input was empty.
22    EmptyInput,
23    /// The ordering between a tested pair of values was undefined.
24    UndefinedOrder,
25}
26
27impl fmt::Display for MinMaxError {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            MinMaxError::EmptyInput => write!(f, "Empty input."),
31            MinMaxError::UndefinedOrder => {
32                write!(f, "Undefined ordering between a tested pair of values.")
33            }
34        }
35    }
36}
37
38impl Error for MinMaxError {}
39
40impl From<EmptyInput> for MinMaxError {
41    fn from(_: EmptyInput) -> MinMaxError {
42        MinMaxError::EmptyInput
43    }
44}
45
46/// An error used by methods and functions that take two arrays as argument and
47/// expect them to have exactly the same shape
48/// (e.g. `ShapeMismatch` is raised when `a.shape() == b.shape()` evaluates to `False`).
49#[derive(Clone, Debug, PartialEq)]
50pub struct ShapeMismatch {
51    pub first_shape: Vec<usize>,
52    pub second_shape: Vec<usize>,
53}
54
55impl fmt::Display for ShapeMismatch {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        write!(
58            f,
59            "Array shapes do not match: {:?} and {:?}.",
60            self.first_shape, self.second_shape
61        )
62    }
63}
64
65impl Error for ShapeMismatch {}
66
67/// An error for methods that take multiple non-empty array inputs.
68#[derive(Clone, Debug, PartialEq)]
69pub enum MultiInputError {
70    /// One or more of the arrays were empty.
71    EmptyInput,
72    /// The arrays did not have the same shape.
73    ShapeMismatch(ShapeMismatch),
74}
75
76impl MultiInputError {
77    /// Returns whether `self` is the `EmptyInput` variant.
78    pub fn is_empty_input(&self) -> bool {
79        match self {
80            MultiInputError::EmptyInput => true,
81            _ => false,
82        }
83    }
84
85    /// Returns whether `self` is the `ShapeMismatch` variant.
86    pub fn is_shape_mismatch(&self) -> bool {
87        match self {
88            MultiInputError::ShapeMismatch(_) => true,
89            _ => false,
90        }
91    }
92}
93
94impl fmt::Display for MultiInputError {
95    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96        match self {
97            MultiInputError::EmptyInput => write!(f, "Empty input."),
98            MultiInputError::ShapeMismatch(e) => write!(f, "Shape mismatch: {}", e),
99        }
100    }
101}
102
103impl Error for MultiInputError {}
104
105impl From<EmptyInput> for MultiInputError {
106    fn from(_: EmptyInput) -> Self {
107        MultiInputError::EmptyInput
108    }
109}
110
111impl From<ShapeMismatch> for MultiInputError {
112    fn from(err: ShapeMismatch) -> Self {
113        MultiInputError::ShapeMismatch(err)
114    }
115}
116
117/// An error computing a quantile.
118#[derive(Debug, Clone, Eq, PartialEq)]
119pub enum QuantileError {
120    /// The input was empty.
121    EmptyInput,
122    /// The `q` was not between `0.` and `1.` (inclusive).
123    InvalidQuantile(N64),
124}
125
126impl fmt::Display for QuantileError {
127    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128        match self {
129            QuantileError::EmptyInput => write!(f, "Empty input."),
130            QuantileError::InvalidQuantile(q) => {
131                write!(f, "{:} is not between 0. and 1. (inclusive).", q)
132            }
133        }
134    }
135}
136
137impl Error for QuantileError {}
138
139impl From<EmptyInput> for QuantileError {
140    fn from(_: EmptyInput) -> QuantileError {
141        QuantileError::EmptyInput
142    }
143}