use std::fmt;
use std::error::Error;
#[derive(Debug)]
pub enum IndicatorError {
EmptyData,
DifferentDataLength,
ImproperDataLength,
ImproperSetting,
}
impl fmt::Display for IndicatorError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s = match self {
IndicatorError::EmptyData => "Input data can not be empty",
IndicatorError::DifferentDataLength => {
"Input data can not have a different length than input"
}
IndicatorError::ImproperDataLength => "Improper data length",
IndicatorError::ImproperSetting => "Improper setting",
};
write!(f, "{}", s)
}
}
impl Error for IndicatorError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_indicator_error_display_empty_data() {
let err = IndicatorError::EmptyData;
assert_eq!(err.to_string(), "Input data can not be empty");
}
#[test]
fn test_indicator_error_display_different_length() {
let err = IndicatorError::DifferentDataLength;
assert_eq!(
err.to_string(),
"Input data can not have a different length than input"
);
}
#[test]
fn test_indicator_error_display_improper_length() {
let err = IndicatorError::ImproperDataLength;
assert_eq!(err.to_string(), "Improper data length");
}
#[test]
fn test_indicator_error_display_improper_setting() {
let err = IndicatorError::ImproperSetting;
assert_eq!(err.to_string(), "Improper setting");
}
#[test]
fn test_indicator_error_implements_error_trait() {
fn assert_error<T: std::error::Error>() {}
assert_error::<IndicatorError>();
}
}