use std::{fmt, io, rc::Rc};
use ntex_bytes::{BytePages, Bytes, BytesMut};
pub trait Encoder {
type Item;
type Error: fmt::Debug;
fn encode(&self, item: Self::Item, dst: &mut BytePages) -> Result<(), Self::Error>;
}
pub trait Decoder {
type Item: fmt::Debug;
type Error: fmt::Debug;
fn decode(&self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error>;
fn decode_eof(&self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
self.decode(src)
}
}
impl<T> Encoder for Rc<T>
where
T: Encoder,
{
type Item = T::Item;
type Error = T::Error;
fn encode(&self, item: Self::Item, dst: &mut BytePages) -> Result<(), Self::Error> {
(**self).encode(item, dst)
}
}
impl<T> Decoder for Rc<T>
where
T: Decoder,
{
type Item = T::Item;
type Error = T::Error;
fn decode(&self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
(**self).decode(src)
}
fn decode_eof(&self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
(**self).decode_eof(src)
}
}
#[derive(Debug, Copy, Clone)]
pub struct BytesCodec;
impl Encoder for BytesCodec {
type Item = Bytes;
type Error = io::Error;
#[inline]
fn encode(&self, item: Bytes, dst: &mut BytePages) -> Result<(), Self::Error> {
dst.append(item);
Ok(())
}
}
impl Decoder for BytesCodec {
type Item = Bytes;
type Error = io::Error;
fn decode(&self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
if src.is_empty() {
Ok(None)
} else {
Ok(Some(src.split_to(src.len())))
}
}
}
#[cfg(test)]
mod tests {
use std::cell::Cell;
use super::*;
#[derive(Default)]
struct ByteCodec {
eof: Cell<usize>,
}
impl Decoder for ByteCodec {
type Item = u8;
type Error = io::Error;
fn decode(&self, src: &mut BytesMut) -> Result<Option<u8>, io::Error> {
if src.is_empty() {
Ok(None)
} else {
Ok(Some(src.split_to(1)[0]))
}
}
fn decode_eof(&self, src: &mut BytesMut) -> Result<Option<u8>, io::Error> {
self.eof.set(self.eof.get() + 1);
self.decode(src)
}
}
struct DefaultEof;
impl Decoder for DefaultEof {
type Item = usize;
type Error = io::Error;
fn decode(&self, src: &mut BytesMut) -> Result<Option<usize>, io::Error> {
let len = src.len();
src.clear();
Ok(Some(len))
}
}
#[test]
fn bytes_codec() {
let codec = BytesCodec;
let codec2 = codec;
assert_eq!(format!("{:?}", codec2.clone()), "BytesCodec");
let mut src = BytesMut::new();
assert!(codec.decode(&mut src).unwrap().is_none());
assert!(codec.decode_eof(&mut src).unwrap().is_none());
src.extend_from_slice(b"hello");
assert_eq!(codec.decode(&mut src).unwrap().unwrap(), "hello");
assert!(src.is_empty());
src.extend_from_slice(b"eof");
assert_eq!(codec.decode_eof(&mut src).unwrap().unwrap(), "eof");
assert!(src.is_empty());
let mut dst = BytePages::default();
codec
.encode(Bytes::from_static(b"hello "), &mut dst)
.unwrap();
codec
.encode(Bytes::from_static(b"world"), &mut dst)
.unwrap();
codec.encode(Bytes::new(), &mut dst).unwrap();
assert_eq!(dst.len(), 11);
assert_eq!(dst.freeze(), "hello world");
}
#[test]
fn default_decode_eof() {
let mut src = BytesMut::from(&b"abc"[..]);
assert_eq!(DefaultEof.decode_eof(&mut src).unwrap(), Some(3));
assert_eq!(DefaultEof.decode_eof(&mut src).unwrap(), Some(0));
}
#[test]
fn rc_codec() {
let codec = Rc::new(ByteCodec::default());
let mut src = BytesMut::from(&b"ab"[..]);
assert_eq!(codec.decode(&mut src).unwrap(), Some(b'a'));
assert_eq!(codec.eof.get(), 0);
assert_eq!(codec.decode_eof(&mut src).unwrap(), Some(b'b'));
assert_eq!(codec.decode_eof(&mut src).unwrap(), None);
assert_eq!(codec.eof.get(), 2);
let codec = Rc::new(BytesCodec);
let mut dst = BytePages::default();
codec.encode(Bytes::from_static(b"rc"), &mut dst).unwrap();
assert_eq!(dst.freeze(), "rc");
}
}