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

#[derive(Debug)]
pub enum IOErrorCode {
    BufferOverFlow = 1,
    Utf8Error = 2,
}

#[derive(Debug)]
pub struct IOError {
    err_code: IOErrorCode,
    err_msg: String,
}

impl IOError {
    pub fn new(err_code: IOErrorCode, err_msg: &str) -> Self {
        IOError {
            err_code,
            err_msg: err_msg.to_owned(),
        }
    }

    pub fn create_buffer_overflow_err() -> Self {
        Self::new(IOErrorCode::BufferOverFlow, "buffer overflow")
    }
}

pub type IOResult<T> = Result<T, IOError>;

impl From<Utf8Error> for IOError {
    fn from(err: Utf8Error) -> Self {
        IOError::new(IOErrorCode::Utf8Error, err.description())
    }
}