Skip to main content

deb_control_codec/
codec.rs

1use asynchronous_codec::Decoder;
2use bytes::BytesMut;
3use std::io;
4
5#[derive(Default)]
6pub struct ControlDecoder;
7
8impl Decoder for ControlDecoder {
9    type Item = BytesMut;
10    type Error = io::Error;
11
12    fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
13        match memchr::memmem::find_iter(src.as_ref(), "\n\n").next() {
14            Some(pos) => {
15                let buf = src.split_to(pos + 2);
16                Ok(Some(buf))
17            }
18            None => Ok(None),
19        }
20    }
21
22    fn decode_eof(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
23        let buf = src.split_to(src.len());
24        Ok(Some(buf))
25    }
26}