mai/
codec.rs

1use std;
2use std::result::Result;
3use std::convert::From;
4
5#[derive(Debug)]
6pub struct BytesRead(pub usize);
7#[derive(Debug)]
8pub struct BytesWritten(pub usize);
9
10#[derive(Debug)]
11pub enum DecodingError {
12  ProtocolError,
13  IncompleteFrame
14}
15
16#[derive(Debug)]
17pub enum EncodingError {
18  ProtocolError,
19  InsufficientBuffer
20}
21
22impl From<std::io::Error> for EncodingError {
23    fn from(error: std::io::Error) -> EncodingError {
24        use std::io::ErrorKind::*;
25        match error.kind() {
26            WriteZero => return EncodingError::InsufficientBuffer,
27            _ => {}
28        }
29        panic!("IO Error occurred while encoding: {:?}", error);
30    }
31}
32
33pub struct DecodedFrame<F> {
34  pub frame: F,
35  pub bytes_read: BytesRead
36}
37
38impl <F> DecodedFrame<F> {
39  pub fn new(frame: F, bytes_read: BytesRead) -> DecodedFrame<F> {
40    DecodedFrame {
41      frame: frame,
42      bytes_read: bytes_read
43    }
44  }
45}
46
47// No need for EncodedFrame type yet (ever?)
48
49pub type DecodingResult<F> = Result<DecodedFrame<F>, DecodingError>;
50pub type EncodingResult = Result<BytesWritten, EncodingError>;
51
52pub trait Codec<F> {
53  fn new() -> Self;
54  fn encode(&mut self, frame: &F, &mut [u8]) -> EncodingResult;
55  fn decode(&mut self, &[u8]) -> DecodingResult<F>;
56}