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
use std::fmt::{Display, Formatter};

pub type DceResult<T> = Result<T, DceErr>;

#[derive(Debug)]
pub enum DceErr {
    /// openly error, will print in console and send to client
    Openly(DceError),
    Closed(DceError),
}

impl DceErr {
    pub fn value(&self) -> &DceError {
        match self {
            DceErr::Openly(v) => v,
            DceErr::Closed(v) => v,
        }
    }

    pub fn openly(code: isize, message: String) -> Self {
        Self::Openly(DceError {code, message})
    }

    pub fn closed(code: isize, message: String) -> Self {
        Self::Closed(DceError {code, message})
    }
}

#[derive(Debug)]
pub struct DceError {
    pub code: isize,
    pub message: String,
}


impl Display for DceErr {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let (message, code) = match self {
            DceErr::Openly(DceError{code, message}) => (code, message),
            DceErr::Closed(DceError{code, message}) => (code, message),
        };
        f.write_str(format!("{}: {}", code, message).as_str())
    }
}

impl std::error::Error for DceErr {}

pub const SERVICE_UNAVAILABLE: isize = 503;
pub const SERVICE_UNAVAILABLE_MESSAGE: &str = "Service Unavailable";