1#[derive(Debug)]
3pub enum Error {
4 CategoriesCountDoesntEqual,
7
8 CategoriesCountIsLess,
11
12 DataIsEmpty,
14
15 SaveFileError(std::io::Error),
17}
18
19impl std::fmt::Display for Error {
20 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21 match &*self {
22 Error::CategoriesCountDoesntEqual => "categories count doesn't equal to data elements count and it's not supported for the selected view".to_string().fmt(f),
23 Error::CategoriesCountIsLess => "categories count is less than data elements count and it's not supported for the selected view".to_string().fmt(f),
24 Error::DataIsEmpty => "provided data vector is empty".to_string().fmt(f),
25 Error::SaveFileError(err) => format!("failed to save file, error: {}", err).fmt(f),
26 }
27 }
28}
29
30impl std::error::Error for Error {}
31
32impl std::convert::From<std::io::Error> for Error {
33 fn from(e: std::io::Error) -> Self {
34 Error::SaveFileError(e)
35 }
36}
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41
42 #[test]
43 fn impl_error() {
44 #[derive(Debug)]
45 struct B(Option<Box<dyn std::error::Error + 'static>>);
46
47 impl std::fmt::Display for B {
48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49 write!(f, "B")
50 }
51 }
52
53 impl std::error::Error for B {}
54
55 let err = B(Some(Box::new(Error::DataIsEmpty)));
56
57 let _err = &err as &(dyn std::error::Error);
58 }
59}