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
#[derive(Debug)]
pub enum Error {
CategoriesCountDoesntEqual,
CategoriesCountIsLess,
DataIsEmpty,
SaveFileError(std::io::Error),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &*self {
Error::CategoriesCountDoesntEqual => "categories count doesn't equal to data elements count and it's not supported for the selected view".to_string().fmt(f),
Error::CategoriesCountIsLess => "categories count is less than data elements count and it's not supported for the selected view".to_string().fmt(f),
Error::DataIsEmpty => "provided data vector is empty".to_string().fmt(f),
Error::SaveFileError(err) => format!("failed to save file, error: {}", err).fmt(f),
}
}
}
impl std::error::Error for Error {}
impl std::convert::From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::SaveFileError(e)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn impl_error() {
#[derive(Debug)]
struct B(Option<Box<dyn std::error::Error + 'static>>);
impl std::fmt::Display for B {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "B")
}
}
impl std::error::Error for B {}
let err = B(Some(Box::new(Error::DataIsEmpty)));
let _err = &err as &(dyn std::error::Error);
}
}