statrs/
error.rs

1use std::error::Error;
2use std::fmt;
3
4/// Enumeration of possible errors thrown within the `statrs` library
5#[derive(Debug)]
6pub enum StatsError {
7    /// Generic bad input parameter error
8    BadParams,
9    /// An argument should have been positive and was not
10    ArgMustBePositive(&'static str),
11    /// An argument should have been non-negative and was not
12    ArgNotNegative(&'static str),
13    /// An argument should have fallen between an inclusive range but didn't
14    ArgIntervalIncl(&'static str, f64, f64),
15    /// An argument should have fallen between an exclusive range but didn't
16    ArgIntervalExcl(&'static str, f64, f64),
17    /// An argument should have fallen in a range excluding the min but didn't
18    ArgIntervalExclMin(&'static str, f64, f64),
19    /// An argument should have falled in a range excluding the max but didn't
20    ArgIntervalExclMax(&'static str, f64, f64),
21    /// An argument must have been greater than a value but wasn't
22    ArgGt(&'static str, f64),
23    /// An argument must have been greater than another argument but wasn't
24    ArgGtArg(&'static str, &'static str),
25    /// An argument must have been greater than or equal to a value but wasn't
26    ArgGte(&'static str, f64),
27    /// An argument must have been greater than or equal to another argument
28    /// but wasn't
29    ArgGteArg(&'static str, &'static str),
30    /// An argument must have been less than a value but wasn't
31    ArgLt(&'static str, f64),
32    /// An argument must have been less than another argument but wasn't
33    ArgLtArg(&'static str, &'static str),
34    /// An argument must have been less than or equal to a value but wasn't
35    ArgLte(&'static str, f64),
36    /// An argument must have been less than or equal to another argument but
37    /// wasn't
38    ArgLteArg(&'static str, &'static str),
39    /// Containers of the same length were expected
40    ContainersMustBeSameLength,
41    /// Computation failed to converge,
42    ComputationFailedToConverge,
43    /// Elements in a container were expected to sum to a value but didn't
44    ContainerExpectedSum(&'static str, f64),
45    /// Elements in a container were expected to sum to a variable but didn't
46    ContainerExpectedSumVar(&'static str, &'static str),
47    /// Special case exception
48    SpecialCase(&'static str),
49}
50
51impl Error for StatsError {
52    fn description(&self) -> &str {
53        "Error performing statistical calculation"
54    }
55}
56
57impl fmt::Display for StatsError {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        match *self {
60            StatsError::BadParams => write!(f, "Bad distribution parameters"),
61            StatsError::ArgMustBePositive(s) => write!(f, "Argument {} must be positive", s),
62            StatsError::ArgNotNegative(s) => write!(f, "Argument {} must be non-negative", s),
63            StatsError::ArgIntervalIncl(s, min, max) => {
64                write!(f, "Argument {} not within interval [{}, {}]", s, min, max)
65            }
66            StatsError::ArgIntervalExcl(s, min, max) => {
67                write!(f, "Argument {} not within interval ({}, {})", s, min, max)
68            }
69            StatsError::ArgIntervalExclMin(s, min, max) => {
70                write!(f, "Argument {} not within interval ({}, {}]", s, min, max)
71            }
72            StatsError::ArgIntervalExclMax(s, min, max) => {
73                write!(f, "Argument {} not within interval [{}, {})", s, min, max)
74            }
75            StatsError::ArgGt(s, val) => write!(f, "Argument {} must be greater than {}", s, val),
76            StatsError::ArgGtArg(s, val) => {
77                write!(f, "Argument {} must be greater than {}", s, val)
78            }
79            StatsError::ArgGte(s, val) => {
80                write!(f, "Argument {} must be greater than or equal to {}", s, val)
81            }
82            StatsError::ArgGteArg(s, val) => {
83                write!(f, "Argument {} must be greater than or equal to {}", s, val)
84            }
85            StatsError::ArgLt(s, val) => write!(f, "Argument {} must be less than {}", s, val),
86            StatsError::ArgLtArg(s, val) => write!(f, "Argument {} must be less than {}", s, val),
87            StatsError::ArgLte(s, val) => {
88                write!(f, "Argument {} must be less than or equal to {}", s, val)
89            }
90            StatsError::ArgLteArg(s, val) => {
91                write!(f, "Argument {} must be less than or equal to {}", s, val)
92            }
93            StatsError::ContainersMustBeSameLength => {
94                write!(f, "Expected containers of same length")
95            }
96            StatsError::ComputationFailedToConverge => write!(f, "Computation failed to converge"),
97            StatsError::ContainerExpectedSum(s, sum) => {
98                write!(f, "Elements in container {} expected to sum to {}", s, sum)
99            }
100            StatsError::ContainerExpectedSumVar(s, sum) => {
101                write!(f, "Elements in container {} expected to sum to {}", s, sum)
102            }
103            StatsError::SpecialCase(s) => write!(f, "{}", s),
104        }
105    }
106}