-
Notifications
You must be signed in to change notification settings - Fork 344
/
lines.rs
112 lines (94 loc) · 3.13 KB
/
lines.rs
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
use std::io;
use bytes::{Buf, BytesMut};
use memchr::memchr;
use super::{Decoder, Encoder};
/// Bytes codec.
///
/// Reads/Writes chunks of bytes from a stream.
#[derive(Debug, Copy, Clone)]
pub struct LinesCodec;
impl Encoder<String> for LinesCodec {
type Error = io::Error;
#[inline]
fn encode(&mut self, item: String, dst: &mut BytesMut) -> Result<(), Self::Error> {
dst.extend_from_slice(item.as_ref());
Ok(())
}
}
impl Decoder for LinesCodec {
type Item = String;
type Error = io::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
if src.is_empty() {
Ok(None)
} else {
let n = match memchr(b'\n', src.chunk()) {
Some(n) => n,
None => {
return Ok(None);
}
};
// split up to new line char
let mut buf = src.split_to(n);
let len = buf.len();
// remove new line char from source
src.advance(1);
match buf.last() {
Some(char) if *char == b'\r' => {
// remove one carriage returns at the end of buf
buf.truncate(len - 1);
}
None => return Ok(Some(String::new())),
_ => {}
}
to_utf8(buf)
}
}
fn decode_eof(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
match self.decode(src)? {
Some(frame) => Ok(Some(frame)),
None if src.is_empty() => Ok(None),
None => {
let len = src.len();
let buf = if src[len - 1] == b'\r' {
src.split_to(len - 1)
} else {
src.split()
};
if buf.is_empty() {
return Ok(None);
}
return to_utf8(buf);
}
}
}
}
fn to_utf8(buf: BytesMut) -> io::Result<Option<String>> {
String::from_utf8(buf.to_vec())
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))
.map(Some)
}
#[cfg(test)]
mod tests {
use bytes::BufMut as _;
use super::*;
#[test]
fn lines_decoder() {
let mut codec = LinesCodec;
let buf = &mut BytesMut::new();
buf.reserve(200);
buf.put_slice(b"\nline 1\nline 2\r\nline 3\n\r\n\r");
assert_eq!("", codec.decode(buf).unwrap().unwrap());
assert_eq!("line 1", codec.decode(buf).unwrap().unwrap());
assert_eq!("line 2", codec.decode(buf).unwrap().unwrap());
assert_eq!("line 3", codec.decode(buf).unwrap().unwrap());
assert_eq!("", codec.decode(buf).unwrap().unwrap());
assert_eq!(None, codec.decode(buf).unwrap());
assert_eq!(None, codec.decode_eof(buf).unwrap());
buf.put_slice(b"k");
assert_eq!(None, codec.decode(buf).unwrap());
assert_eq!("\rk", codec.decode_eof(buf).unwrap().unwrap());
assert_eq!(None, codec.decode(buf).unwrap());
assert_eq!(None, codec.decode_eof(buf).unwrap());
}
}