Skip to content

Commit

Permalink
refactor: split RespFrame and RespError into frame.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
luffy2025 committed Dec 2, 2024
1 parent 96ff4b1 commit 9460f28
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 57 deletions.
64 changes: 64 additions & 0 deletions src/resp/frame.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::resp::f64::RespF64;
use crate::resp::map::RespMap;
use crate::resp::set::RespSet;
use crate::resp::simple_error::SimpleError;
use crate::{BulkString, RespArray, RespDecode, RespNull, SimpleString};
use bytes::BytesMut;
use enum_dispatch::enum_dispatch;
use thiserror::Error;

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord)]
#[enum_dispatch(RespEncode)]
pub enum RespFrame {
SimpleString(SimpleString),
Error(SimpleError),
Integer(i64),
BulkString(BulkString),
Array(RespArray),
Null(RespNull),
Boolean(bool),
Double(RespF64),
Map(RespMap),
Set(RespSet),
}

#[derive(Error, Debug, PartialEq, Eq)]
pub enum RespError {
#[error("Invalid frame: {0}")]
InvalidFrame(String),
#[error("Invalid frame type: {0}")]
InvalidFrameType(String),
#[error("Invalid frame length: {0}")]
InvalidFrameLength(isize),
#[error("Frame is not compete")]
NotComplete,
#[error("Frame is empty")]
Empty,
#[error("ParseIntError: {0}")]
ParseIntError(#[from] std::num::ParseIntError),
#[error("ParseFloatError: {0}")]
ParseFloatError(#[from] std::num::ParseFloatError),
}

impl RespDecode for RespFrame {
const PREFIX: &'static u8 = &b'_';
fn decode(buf: &mut BytesMut) -> Result<Self, RespError> {
let mut iter = buf.iter().peekable();
match iter.peek() {
Some(&SimpleString::PREFIX) => Ok(SimpleString::decode(buf)?.into()),
Some(&SimpleError::PREFIX) => Ok(SimpleError::decode(buf)?.into()),
Some(&i64::PREFIX) => Ok(i64::decode(buf)?.into()),
Some(&BulkString::PREFIX) => Ok(BulkString::decode(buf)?.into()),
Some(&RespArray::PREFIX) => Ok(RespArray::decode(buf)?.into()),
Some(&RespSet::PREFIX) => Ok(RespSet::decode(buf)?.into()),
Some(&RespMap::PREFIX) => Ok(RespMap::decode(buf)?.into()),
Some(&RespNull::PREFIX) => Ok(RespNull::decode(buf)?.into()),
Some(&bool::PREFIX) => Ok(bool::decode(buf)?.into()),
Some(&RespF64::PREFIX) => Ok(RespF64::decode(buf)?.into()),
Some(e) => Err(RespError::InvalidFrame(
format!("Invalid prefix: {}", e.to_ascii_lowercase()).to_string(),
)),
None => Err(RespError::Empty),
}
}
}
60 changes: 3 additions & 57 deletions src/resp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
pub use crate::resp::array::RespArray;
pub use crate::resp::bulk_string::BulkString;
use crate::resp::f64::RespF64;
pub use crate::resp::frame::RespError;
pub use crate::resp::frame::RespFrame;
use crate::resp::map::RespMap;
pub use crate::resp::null::RespNull;
use crate::resp::set::RespSet;
use crate::resp::simple_error::SimpleError;
pub use crate::resp::simple_string::SimpleString;
use bytes::BytesMut;
use enum_dispatch::enum_dispatch;
use thiserror::Error;

mod array;
mod bool;
mod bulk_string;
mod f64;
mod frame;
mod i64;
mod map;
mod null;
Expand All @@ -31,62 +33,6 @@ pub trait RespDecode: Sized {
fn decode(buf: &mut BytesMut) -> Result<Self, RespError>;
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord)]
#[enum_dispatch(RespEncode)]
pub enum RespFrame {
SimpleString(SimpleString),
Error(SimpleError),
Integer(i64),
BulkString(BulkString),
Array(RespArray),
Null(RespNull),
Boolean(bool),
Double(RespF64),
Map(RespMap),
Set(RespSet),
}

#[derive(Error, Debug, PartialEq, Eq)]
pub enum RespError {
#[error("Invalid frame: {0}")]
InvalidFrame(String),
#[error("Invalid frame type: {0}")]
InvalidFrameType(String),
#[error("Invalid frame length: {0}")]
InvalidFrameLength(isize),
#[error("Frame is not compete")]
NotComplete,
#[error("Frame is empty")]
Empty,
#[error("ParseIntError: {0}")]
ParseIntError(#[from] std::num::ParseIntError),
#[error("ParseFloatError: {0}")]
ParseFloatError(#[from] std::num::ParseFloatError),
}

impl RespDecode for RespFrame {
const PREFIX: &'static u8 = &b'_';
fn decode(buf: &mut BytesMut) -> Result<Self, RespError> {
let mut iter = buf.iter().peekable();
match iter.peek() {
Some(&SimpleString::PREFIX) => Ok(SimpleString::decode(buf)?.into()),
Some(&SimpleError::PREFIX) => Ok(SimpleError::decode(buf)?.into()),
Some(&i64::PREFIX) => Ok(i64::decode(buf)?.into()),
Some(&BulkString::PREFIX) => Ok(BulkString::decode(buf)?.into()),
Some(&RespArray::PREFIX) => Ok(RespArray::decode(buf)?.into()),
Some(&RespSet::PREFIX) => Ok(RespSet::decode(buf)?.into()),
Some(&RespMap::PREFIX) => Ok(RespMap::decode(buf)?.into()),
Some(&RespNull::PREFIX) => Ok(RespNull::decode(buf)?.into()),
Some(&bool::PREFIX) => Ok(bool::decode(buf)?.into()),
Some(&RespF64::PREFIX) => Ok(RespF64::decode(buf)?.into()),
Some(e) => Err(RespError::InvalidFrame(
format!("Invalid prefix: {}", e.to_ascii_lowercase()).to_string(),
)),
None => Err(RespError::Empty),
}
}
}

pub fn extract_end_and_length(
buf: &mut BytesMut,
prefix: &[u8],
Expand Down

0 comments on commit 9460f28

Please sign in to comment.