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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::byte_writer::ByteWriter;
use crate::error::{InError, OutError};
use crate::util;
use std::io::{Bytes, Read, Write};
pub struct Reader<R: Read> {
in_bytes: Bytes<R>,
}
impl<R: Read> Reader<R> {
pub fn new(read: R) -> Self {
Reader {
in_bytes: read.bytes(),
}
}
}
impl<R: Read> Iterator for Reader<R> {
type Item = Result<u8, InError>;
fn next(&mut self) -> Option<Self::Item> {
match self.in_bytes.next()? {
Ok(c) => {
if c.is_ascii() {
Some(Ok(c))
} else {
Some(Err(InError::InvalidByte(c as char))) }
}
Err(e) => Some(Err(InError::StdIO(e))),
}
}
}
pub struct Writer<W: Write> {
out_bytes: W,
}
impl<W: Write> Writer<W> {
pub fn new(out_bytes: W) -> Self {
Writer { out_bytes }
}
}
impl<W: Write> ByteWriter for Writer<W> {
fn write(&mut self, byte: u8) -> Result<(), OutError> {
if byte.is_ascii() {
util::write(&mut self.out_bytes, &[byte], 1)
} else {
Err(OutError::InvalidByte(byte))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::util::literals::*;
#[test]
fn read() {
let input = [_B, _A, _EXCL, _STAR];
let mut reader = Reader::new(input.as_slice());
for b in input {
assert_eq!(b, reader.next().unwrap().unwrap());
}
assert!(reader.next().is_none());
}
#[test]
fn write() {
let input = _STAR;
let mut output = [0u8, 1];
let mut writer = Writer::new(output.as_mut_slice());
writer.write(input).unwrap();
assert_eq!(input, output[0]);
}
}