Skip to content

Commit

Permalink
cmd: Add reply frame
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Jun 11, 2024
1 parent 2f720c3 commit 34d62e7
Show file tree
Hide file tree
Showing 21 changed files with 293 additions and 268 deletions.
71 changes: 2 additions & 69 deletions src/cmd/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
// Use of this source is governed by GNU Affero General Public License
// that can be found in the LICENSE file.

use std::io::{Cursor, Write};
use std::io::Cursor;
use std::num::TryFromIntError;
use std::string::FromUtf8Error;

use atoi::atoi;
use bytes::{Buf, BufMut, Bytes, BytesMut};

use crate::cmd::frame_consts::FrameConst;
use bytes::{Buf, Bytes};

#[derive(Debug, Clone)]
pub enum Frame {
Expand All @@ -19,11 +17,8 @@ pub enum Frame {
Integer(i64),
Null,
Simple(String),
Const(FrameConst),
}

// TODO(Shaohua): Add ReplyFrame

#[derive(Debug)]
pub enum ParseFrameError {
ArrayExpected,
Expand All @@ -33,68 +28,6 @@ pub enum ParseFrameError {
}

impl Frame {
pub fn into_bytes(self) -> Bytes {
match self {
Self::Array(arr) => {
let mut bytes = BytesMut::new();
bytes.put_u8(b'*');
let len = arr.len();
#[allow(clippy::cast_possible_wrap)]
Self::write_i64(&mut bytes, len as i64);

for frame in arr {
bytes.put(frame.into_bytes());
}

bytes.freeze()
}
Self::Bulk(val) => {
let len = val.len();
let mut bytes = BytesMut::new();
bytes.put_u8(b'$');
#[allow(clippy::cast_possible_wrap)]
Self::write_i64(&mut bytes, len as i64);
bytes.put(val);
bytes.put_slice(b"\r\n");
bytes.freeze()
}
Self::Error(err) => {
let mut bytes = BytesMut::new();
bytes.put_u8(b'-');
bytes.put(Bytes::from(err));
bytes.put_slice(b"\r\n");
bytes.freeze()
}
Self::Integer(num) => {
let mut bytes = BytesMut::new();
bytes.put_u8(b':');
Self::write_i64(&mut bytes, num);
bytes.freeze()
}
Self::Null => Bytes::from("$-1\r\n"),
Self::Simple(s) => {
let mut bytes = BytesMut::new();
bytes.put_u8(b'+');
bytes.put(Bytes::from(s));
bytes.put_slice(b"\r\n");
bytes.freeze()
}
Self::Const(frame_const) => {
frame_const.to_real_frame().into_bytes()
}
}
}

fn write_i64(bytes: &mut BytesMut, val: i64) {
// NOTE(Shaohua): Replace String format with stack array.
let mut buf = [0u8; 32];
let mut cursor = Cursor::new(&mut buf[..]);
write!(&mut cursor, "{val}").unwrap();
let pos = usize::try_from(cursor.position()).unwrap();
bytes.put(&cursor.get_ref()[0..pos]);
bytes.put_slice(b"\r\n");
}

pub fn push_bulk(&mut self, bytes: Bytes) -> Result<(), ParseFrameError> {
if let Self::Array(vec) = self {
vec.push(Self::Bulk(bytes));
Expand Down
127 changes: 0 additions & 127 deletions src/cmd/frame_consts.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod frame;
pub mod list;
mod parse;
pub mod string;
pub mod frame_consts;
pub mod reply_frame;

#[derive(Debug, Clone)]
pub enum Command {
Expand Down
Loading

0 comments on commit 34d62e7

Please sign in to comment.