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::error::Error;
use std::fmt;

pub mod env;
pub mod msg;
pub mod output;
pub mod request;

mod ffi;

#[derive(Debug)]
pub enum DotsError {
    // Generic error used for invalid return value.
    Unknown(i32),

    Libc,
    Interface,
    Invalid,
    Internal,
}

impl DotsError {
    fn from_ret(ret: i32) -> DotsError {
        match ret {
            -1 => DotsError::Libc,
            -2 => DotsError::Interface,
            -3 => DotsError::Invalid,
            -4 => DotsError::Internal,
            _ => DotsError::Unknown(ret),
        }
    }
}

impl fmt::Display for DotsError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s = match self {
            DotsError::Libc => String::from("libdots libc error"),
            DotsError::Interface => String::from("libdots interface error"),
            DotsError::Invalid => String::from("libdots invalid argument error"),
            DotsError::Internal => String::from("libdots internal error"),
            DotsError::Unknown(ret) => format!("libdots unknown error: {}", ret),
        };
        f.write_str(s.as_str())
    }
}

impl Error for DotsError {}

pub type DotsResult<T> = Result<T, DotsError>;