bytestream_rs/
bytestream.rs

1use std;
2use std::io::Cursor;
3
4#[derive(Debug)]
5pub struct ByteStream {
6    pub cursor: Cursor<Vec<u8>>,
7    pub offset: usize,
8    pub length: usize,
9    pub message: String,
10    // reader: std::io::BufReader,
11    // writer: std::io::BufWriter,
12}
13
14impl ByteStream {
15    // meant for reading
16    pub fn new_from_buffer(buffer: Vec<u8>) -> ByteStream {
17        ByteStream {
18            length: buffer.len(),
19            offset: 0,
20            cursor: Cursor::new(buffer),
21            message: String::new(),
22        }
23    }
24
25    // meant for writing
26    pub fn new() -> ByteStream {
27        ByteStream {
28            length: 0,
29            offset: 0,
30            cursor: Cursor::new(Vec::new()),
31            message: String::new(),
32        }
33    }
34}
35
36#[derive(Debug)]
37pub enum ByteStreamError {
38    IoError(std::io::Error),
39    InvalidBytesRead(usize),
40    InvalidBytesWritten(usize),
41    InvalidStringLength(usize),
42    InvalidString(String),
43    NotEnoughBytes,
44    NoMoreBytes,
45}
46
47impl From<std::io::Error> for ByteStreamError {
48    fn from(error: std::io::Error) -> Self {
49        ByteStreamError::IoError(error)
50    }
51}