ndarray_histogram/
errors.rs1use num_traits::Float;
4use std::error::Error;
5use std::fmt;
6
7#[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#[derive(Clone, Debug, Eq, PartialEq)]
21pub enum MinMaxError {
22 EmptyInput,
24 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#[derive(Clone, Debug, PartialEq)]
51pub struct ShapeMismatch {
52 pub first_shape: Vec<usize>,
54 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#[derive(Clone, Debug, PartialEq)]
72pub enum MultiInputError {
73 EmptyInput,
75 ShapeMismatch(ShapeMismatch),
77}
78
79impl MultiInputError {
80 pub fn is_empty_input(&self) -> bool {
82 matches!(self, MultiInputError::EmptyInput)
83 }
84
85 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#[derive(Debug, Clone, Eq, PartialEq)]
116pub enum QuantileError<F: Float + fmt::Debug> {
117 EmptyInput,
119 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}