Module redis_protocol::resp3::decode::complete[][src]

Expand description

Decoding functions for complete frames. If a streamed frame is detected it will result in an error.

Implement a codec that only supports complete frames…

use redis_protocol::resp3::types::*;
use redis_protocol::types::{RedisProtocolError, RedisProtocolErrorKind};
use redis_protocol::resp3::decode::complete::*;
use redis_protocol::resp3::encode::complete::*;
use bytes::BytesMut;
use tokio_util::codec::{Decoder, Encoder};

pub struct RedisCodec {}

impl Encoder<Frame> for RedisCodec {
  type Error = RedisProtocolError;

  fn encode(&mut self, item: Frame, dst: &mut BytesMut) -> Result<(), Self::Error> {
    // in this example we only show support for encoding complete frames
    let _ = encode_bytes(dst, &item)?;
    Ok(())
  }
}

impl Decoder for RedisCodec {
  type Item = Frame;
  type Error = RedisProtocolError;

  fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
    if src.is_empty() {
      return Ok(None);
    }

    if let Some((frame, amt)) = decode(&src)? {
      // clear the buffer up to the amount decoded so the same bytes aren't repeatedly processed
      let _ = src.split_to(amt);

      Ok(Some(frame))
    }else{
      Ok(None)
    }
  }
}

Functions

Attempt to parse the contents of buf, returning the first valid frame and the number of bytes consumed.