ndarray_histogram/
errors.rs

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