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
50
51
52
53
54
mod file;

use std::convert::From;
use std::{error, fmt};

#[derive(Debug)]
pub enum Error {
    CorruptSegmentHeader,
    CorruptMsgHeader,
    InsufficientLength(usize),
    IoError(Box<error::Error>),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> {
        write!(f, "{:?}", self)
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::CorruptSegmentHeader => "corrupted segment header",
            Error::CorruptMsgHeader => "corruped message header",
            Error::InsufficientLength(..) => "insufficient length",
            Error::IoError(..) => "I/O error",
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match *self {
            Error::IoError(ref cause) => Some(&**cause),
            _ => None,
        }
    }
}

impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Error {
        Error::IoError(Box::new(err))
    }
}

pub type Result<T> = std::result::Result<T, Error>;

pub trait Reader {
    fn read(&mut self) -> Result<Option<Vec<u8>>>;
}

pub trait Writer {
    fn write(&mut self, buf: &[u8]) -> Result<()>;
}

pub use file::{FileWriter, FileReader};