wireframe 0.4.0-alpha1

Simplify building servers and clients for custom binary protocols.
Documentation
//! RESP codec and adapter implementations.

use std::io;

use bytes::{Buf, Bytes, BytesMut};
use tokio_util::codec::{Decoder, Encoder};
use wireframe::codec::FrameCodec;

use super::{
    encode::{encode_frame, encoded_len},
    frame::RespFrame,
    parse::parse_frame,
};

/// RESP frame codec for use with Wireframe.
#[derive(Clone, Debug)]
pub struct RespFrameCodec {
    /// Upper bound applied before a frame is admitted to the transport.
    /// Keeping this limit in the codec makes decoder and encoder enforcement
    /// symmetrical, so neither direction can allocate an unbounded payload.
    max_frame_length: usize,
}

impl RespFrameCodec {
    /// Create a new RESP codec with the given maximum frame length.
    #[must_use]
    pub const fn new(max_frame_length: usize) -> Self { Self { max_frame_length } }
}

/// Tokio codec adapter for RESP frames.
#[derive(Clone, Debug)]
pub struct RespAdapter {
    /// Maximum encoded or decoded frame size delegated by the parent codec.
    /// The adapter checks it before advancing the input buffer or reserving
    /// output capacity, preserving the stream boundary on rejected frames.
    max_frame_length: usize,
}

impl RespAdapter {
    /// Build a Tokio adapter carrying the frame-size policy for one direction.
    const fn new(max_frame_length: usize) -> Self { Self { max_frame_length } }
}

impl Decoder for RespAdapter {
    type Item = RespFrame;
    type Error = io::Error;

    fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
        let Some((frame, consumed)) = parse_frame(src, self.max_frame_length)? else {
            return Ok(None);
        };
        if consumed > self.max_frame_length {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "frame too large",
            ));
        }
        src.advance(consumed);
        Ok(Some(frame))
    }
}

impl Encoder<RespFrame> for RespAdapter {
    type Error = io::Error;

    fn encode(&mut self, item: RespFrame, dst: &mut BytesMut) -> Result<(), Self::Error> {
        let len = encoded_len(&item)?;
        if len > self.max_frame_length {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "frame too large",
            ));
        }
        dst.reserve(len);
        encode_frame(&item, dst)
    }
}

impl FrameCodec for RespFrameCodec {
    type Frame = RespFrame;
    type Decoder = RespAdapter;
    type Encoder = RespAdapter;

    fn decoder(&self) -> Self::Decoder { RespAdapter::new(self.max_frame_length) }

    fn encoder(&self) -> Self::Encoder { RespAdapter::new(self.max_frame_length) }

    fn frame_payload(frame: &Self::Frame) -> &[u8] {
        match frame {
            RespFrame::BulkString(Some(payload)) => payload.as_slice(),
            _ => &[],
        }
    }

    fn wrap_payload(&self, payload: Bytes) -> Self::Frame {
        RespFrame::BulkString(Some(payload.to_vec()))
    }

    fn max_frame_length(&self) -> usize { self.max_frame_length }
}

#[cfg(test)]
mod tests {
    use bytes::BytesMut;
    use tokio_util::codec::Encoder;

    use super::RespAdapter;
    use crate::resp_codec_impl::frame::RespFrame;

    /// `:-99\r\n` is six bytes: it fits a six-byte limit exactly and is
    /// rejected by a five-byte one.
    #[test]
    fn negative_integer_frame_is_accepted_at_its_exact_size_limit() {
        let frame = RespFrame::Integer(-99);
        let mut dst = BytesMut::new();
        assert!(
            RespAdapter::new(6).encode(frame.clone(), &mut dst).is_ok(),
            "assertion failed: RespAdapter::new(6).encode(frame.clone(), &mut dst).is_ok()"
        );
        assert_eq!(
            dst.as_ref(),
            b":-99\r\n",
            "dst.as_ref() should equal b\":-99\\r\\n\""
        );

        let mut rejected = BytesMut::new();
        assert!(
            RespAdapter::new(5).encode(frame, &mut rejected).is_err(),
            "assertion failed: RespAdapter::new(5).encode(frame, &mut rejected).is_err()"
        );
        assert!(rejected.is_empty(), "assertion failed: rejected.is_empty()");
    }
}