Skip to content

Commit

Permalink
feat: implement info cmd and print error for unrecognized cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
kindywu committed May 8, 2024
1 parent dc7c34d commit 49facc4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
33 changes: 33 additions & 0 deletions src/cmd/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::{Backend, CommandError, CommandExecutor, RespArray, RespFrame};

use super::validate_command;

#[derive(Debug)]
pub struct Info();

impl CommandExecutor for Info {
fn execute(self, _: &Backend) -> RespFrame {
"Ok".into()
}
}

impl Info {
pub fn new() -> Self {
Self()
}
}

impl Default for Info {
fn default() -> Self {
Self::new()
}
}

impl TryFrom<RespArray> for Info {
type Error = CommandError;
fn try_from(value: RespArray) -> Result<Self, Self::Error> {
validate_command(&value, &["info"], 0, super::ArgsCheckRule::Equal)?;

Ok(Info::new())
}
}
7 changes: 5 additions & 2 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod echo;
mod hmget;
mod hset;
pub mod info;
mod sadd;
mod sismember;
mod unrecognized;
Expand All @@ -13,7 +14,7 @@ use thiserror::Error;
use crate::{Backend, RespArray, RespError, RespFrame, SimpleString};

use self::{
echo::Echo, hmget::HmGet, hset::HSet, sadd::SAdd, sismember::SisMember,
echo::Echo, hmget::HmGet, hset::HSet, info::Info, sadd::SAdd, sismember::SisMember,
unrecognized::Unrecognized,
};

Expand Down Expand Up @@ -42,6 +43,7 @@ pub trait CommandExecutor {
#[derive(Debug)]
pub enum Command {
Echo(Echo),
Info(Info),
SAdd(SAdd),
SisMember(SisMember),
HmGet(HmGet),
Expand Down Expand Up @@ -76,7 +78,8 @@ impl TryFrom<RespArray> for Command {
b"hmget" => Ok(HmGet::try_from(v)?.into()),
b"sadd" => Ok(SAdd::try_from(v)?.into()),
b"sismember" => Ok(SisMember::try_from(v)?.into()),
_ => Ok(Unrecognized.into()),
b"info" => Ok(Info::try_from(v)?.into()),
_ => Ok(Unrecognized::new(cmd.clone()).into()),
},
_ => Err(CommandError::InvalidCommand("Command is null".to_string())),
},
Expand Down
17 changes: 14 additions & 3 deletions src/cmd/unrecognized.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
use crate::{Backend, CommandExecutor, RespFrame};
use crate::{Backend, BulkString, CommandExecutor, RespFrame, SimpleError};

#[derive(Debug)]
pub struct Unrecognized;
pub struct Unrecognized(pub(crate) BulkString);

impl CommandExecutor for Unrecognized {
fn execute(self, _: &Backend) -> RespFrame {
"OK".into()
SimpleError::new(format!(
"ERR unknown command '{}', with args beginning with:",
self.0
))
.into()
}
}

impl Unrecognized {
pub fn new(cmd: BulkString) -> Self {
Self(cmd)
}
}
13 changes: 12 additions & 1 deletion src/resp/bulk_string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::Deref;
use std::{fmt, ops::Deref};

use bytes::{Buf, BytesMut};

Expand Down Expand Up @@ -31,6 +31,17 @@ use super::{parse_length, CRLF_LEN};
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd)]
pub struct BulkString(pub(crate) Vec<u8>);

impl fmt::Display for BulkString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// 使用 from_utf8 检查 Vec<u8> 是否是有效的 UTF-8 序列
let str_slice = std::str::from_utf8(&self.0);
match str_slice {
Ok(s) => write!(f, "{}", s),
Err(_) => write!(f, "Invalid UTF-8 sequence"),
}
}
}

//
const NULL_BULK_STRING: &[u8; 5] = b"$-1\r\n";

Expand Down

0 comments on commit 49facc4

Please sign in to comment.