use std::io;
use bytes::{Bytes, BytesMut};
use tokio_util::codec::{Decoder, Encoder};
use crate::codec::FrameCodec;
#[derive(Clone, Debug)]
pub struct BadFrame {
pub correlation_id: u64,
pub payload: Vec<u8>,
}
#[derive(Clone, Debug)]
pub struct BadCodec;
#[derive(Clone, Debug)]
pub struct BadDecoder;
#[derive(Clone, Debug)]
pub struct BadEncoder;
impl Decoder for BadDecoder {
type Item = BadFrame;
type Error = io::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
if src.is_empty() {
Ok(None)
} else {
Err(io::Error::new(io::ErrorKind::InvalidData, "bad test frame"))
}
}
}
impl Encoder<BadFrame> for BadEncoder {
type Error = io::Error;
fn encode(&mut self, _item: BadFrame, _dst: &mut BytesMut) -> Result<(), Self::Error> { Ok(()) }
}
impl FrameCodec for BadCodec {
type Frame = BadFrame;
type Decoder = BadDecoder;
type Encoder = BadEncoder;
fn decoder(&self) -> Self::Decoder { BadDecoder }
fn encoder(&self) -> Self::Encoder { BadEncoder }
fn frame_payload(frame: &Self::Frame) -> &[u8] { frame.payload.as_slice() }
fn wrap_payload(&self, payload: Bytes) -> Self::Frame {
BadFrame {
correlation_id: 0,
payload: payload.to_vec(),
}
}
fn correlation_id(frame: &Self::Frame) -> Option<u64> { Some(frame.correlation_id) }
fn max_frame_length(&self) -> usize { 64 }
}