ndarray_stats/histogram/
errors.rs

1use crate::errors::{EmptyInput, MinMaxError};
2use std::error;
3use std::fmt;
4
5/// Error to denote that no bin has been found for a certain observation.
6#[derive(Debug, Clone)]
7pub struct BinNotFound;
8
9impl fmt::Display for BinNotFound {
10    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11        write!(f, "No bin has been found.")
12    }
13}
14
15impl error::Error for BinNotFound {
16    fn description(&self) -> &str {
17        "No bin has been found."
18    }
19}
20
21/// Error computing the set of histogram bins.
22#[derive(Debug, Clone)]
23pub enum BinsBuildError {
24    /// The input array was empty.
25    EmptyInput,
26    /// The strategy for computing appropriate bins failed.
27    Strategy,
28    #[doc(hidden)]
29    __NonExhaustive,
30}
31
32impl BinsBuildError {
33    /// Returns whether `self` is the `EmptyInput` variant.
34    pub fn is_empty_input(&self) -> bool {
35        match self {
36            BinsBuildError::EmptyInput => true,
37            _ => false,
38        }
39    }
40
41    /// Returns whether `self` is the `Strategy` variant.
42    pub fn is_strategy(&self) -> bool {
43        match self {
44            BinsBuildError::Strategy => true,
45            _ => false,
46        }
47    }
48}
49
50impl fmt::Display for BinsBuildError {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        write!(f, "The strategy failed to determine a non-zero bin width.")
53    }
54}
55
56impl error::Error for BinsBuildError {
57    fn description(&self) -> &str {
58        "The strategy failed to determine a non-zero bin width."
59    }
60}
61
62impl From<EmptyInput> for BinsBuildError {
63    fn from(_: EmptyInput) -> Self {
64        BinsBuildError::EmptyInput
65    }
66}
67
68impl From<MinMaxError> for BinsBuildError {
69    fn from(err: MinMaxError) -> BinsBuildError {
70        match err {
71            MinMaxError::EmptyInput => BinsBuildError::EmptyInput,
72            MinMaxError::UndefinedOrder => BinsBuildError::Strategy,
73        }
74    }
75}