1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! Histogram related errors.

use crate::errors::{EmptyInput, MinMaxError};
use std::error;
use std::fmt;

/// Error to denote that no bin has been found for a certain observation.
#[derive(Debug, Clone)]
pub struct BinNotFound;

impl fmt::Display for BinNotFound {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(f, "No bin has been found.")
	}
}

impl error::Error for BinNotFound {
	fn description(&self) -> &str {
		"No bin has been found."
	}
}

/// Error computing the set of histogram bins.
#[derive(Debug, Clone)]
pub enum BinsBuildError {
	/// The input array was empty.
	EmptyInput,
	/// The strategy for computing appropriate bins failed.
	Strategy,
	#[doc(hidden)]
	__NonExhaustive,
}

impl BinsBuildError {
	/// Returns whether `self` is the `EmptyInput` variant.
	pub fn is_empty_input(&self) -> bool {
		matches!(self, BinsBuildError::EmptyInput)
	}

	/// Returns whether `self` is the `Strategy` variant.
	pub fn is_strategy(&self) -> bool {
		matches!(self, BinsBuildError::Strategy)
	}
}

impl fmt::Display for BinsBuildError {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(f, "The strategy failed to determine a non-zero bin width.")
	}
}

impl error::Error for BinsBuildError {
	fn description(&self) -> &str {
		"The strategy failed to determine a non-zero bin width."
	}
}

impl From<EmptyInput> for BinsBuildError {
	fn from(_: EmptyInput) -> Self {
		BinsBuildError::EmptyInput
	}
}

impl From<MinMaxError> for BinsBuildError {
	fn from(err: MinMaxError) -> BinsBuildError {
		match err {
			MinMaxError::EmptyInput => BinsBuildError::EmptyInput,
			MinMaxError::UndefinedOrder => BinsBuildError::Strategy,
		}
	}
}