cooplan_definitions_io_lib/
error.rs

1use std::fmt;
2
3#[derive(Debug, Copy, Clone, PartialEq)]
4pub enum ErrorKind {
5    MissingId,
6    ParentNotFound,
7    IdNotFound,
8    ParentNotAvailable,
9    FailedToBorrowCategory,
10    Other,
11}
12
13#[derive(Debug)]
14pub struct Error {
15    pub kind: ErrorKind,
16    pub message: String,
17}
18
19impl Error {
20    pub fn new(kind: ErrorKind, message: &str) -> Error {
21        Error {
22            kind,
23            message: message.to_string(),
24        }
25    }
26
27    pub fn kind(&self) -> ErrorKind {
28        self.kind
29    }
30}
31
32impl fmt::Display for Error {
33    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34        write!(f, "{}", self.message)
35    }
36}
37
38impl From<cooplan_definitions_lib::error::Error> for Error {
39    fn from(error: cooplan_definitions_lib::error::Error) -> Self {
40        let kind: ErrorKind = match error.kind() {
41            cooplan_definitions_lib::error::ErrorKind::FailedToBorrowCategory => {
42                ErrorKind::FailedToBorrowCategory
43            }
44            cooplan_definitions_lib::error::ErrorKind::MissingId => ErrorKind::MissingId,
45            cooplan_definitions_lib::error::ErrorKind::ParentNotAvailable => {
46                ErrorKind::ParentNotAvailable
47            }
48            _ => ErrorKind::Other,
49        };
50
51        Error {
52            kind,
53            message: error.message.clone(),
54        }
55    }
56}