ndarray_histogram/histogram/
errors.rs

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