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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use crate::{IntoOwned as _, IrcMessage, MessageError};
use std::io::{BufRead, BufReader, Read};
#[derive(Debug)]
#[non_exhaustive]
pub enum DecodeError {
Io(std::io::Error),
InvalidUtf8(std::str::Utf8Error),
ParseError(MessageError),
Eof,
}
impl std::fmt::Display for DecodeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Io(err) => write!(f, "io error: {}", err),
Self::InvalidUtf8(err) => write!(f, "invalid utf8: {}", err),
Self::ParseError(err) => write!(f, "parse error: {}", err),
Self::Eof => f.write_str("end of file reached"),
}
}
}
impl std::error::Error for DecodeError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(err) => Some(err),
Self::InvalidUtf8(err) => Some(err),
Self::ParseError(err) => Some(err),
_ => None,
}
}
}
pub struct Decoder<R> {
reader: BufReader<R>,
buf: Vec<u8>,
}
impl<R> std::fmt::Debug for Decoder<R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Decoder").finish()
}
}
impl<R> Decoder<R>
where
R: Read,
{
pub fn new(reader: R) -> Self {
Self {
reader: BufReader::new(reader),
buf: Vec::with_capacity(1024),
}
}
pub fn read_message(&mut self) -> Result<IrcMessage<'_>, DecodeError> {
self.buf.clear();
let n = self
.reader
.read_until(b'\n', &mut self.buf)
.map_err(DecodeError::Io)?;
if n == 0 {
return Err(DecodeError::Eof);
}
let str = std::str::from_utf8(&self.buf[..n]).map_err(DecodeError::InvalidUtf8)?;
crate::irc::parse_one(str)
.map_err(DecodeError::ParseError)
.map(|(_, msg)| msg)
}
pub fn iter(&mut self) -> &mut Self {
self
}
pub fn into_inner(self) -> R {
self.reader.into_inner()
}
}
impl<R: Read> Iterator for Decoder<R> {
type Item = Result<IrcMessage<'static>, DecodeError>;
fn next(&mut self) -> Option<Self::Item> {
match self.read_message() {
Err(DecodeError::Eof) => None,
Ok(msg) => Some(Ok(msg.into_owned())),
Err(err) => Some(Err(err)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn read_sync() {
let data = b"hello\r\nworld\r\ntesting this\r\nand another thing\r\n".to_vec();
let mut reader = std::io::Cursor::new(data);
let v = Decoder::new(&mut reader)
.iter()
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(v.len(), 4);
reader.set_position(0);
let mut dec = Decoder::new(reader);
for _ in 0..4 {
dec.read_message().unwrap();
}
assert!(matches!(dec.read_message().unwrap_err(), DecodeError::Eof))
}
}