1use std::str::Utf8Error;
2use std::error::Error;
3
4#[derive(Debug)]
5pub enum IOErrorCode {
6 BufferOverFlow = 1,
7 Utf8Error = 2,
8}
9
10#[derive(Debug)]
11pub struct IOError {
12 err_code: IOErrorCode,
13 err_msg: String,
14}
15
16impl IOError {
17 pub fn new(err_code: IOErrorCode, err_msg: &str) -> Self {
18 IOError {
19 err_code,
20 err_msg: err_msg.to_owned(),
21 }
22 }
23
24 pub fn create_buffer_overflow_err() -> Self {
25 Self::new(IOErrorCode::BufferOverFlow, "buffer overflow")
26 }
27}
28
29pub type IOResult<T> = Result<T, IOError>;
30
31impl From<Utf8Error> for IOError {
32 fn from(err: Utf8Error) -> Self {
33 IOError::new(IOErrorCode::Utf8Error, err.description())
34 }
35}