-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from upupnoah/main
feat: support hmget sadd sismember
- Loading branch information
Showing
25 changed files
with
847 additions
and
386 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/target | ||
.DS_Store | ||
/.vscode | ||
*.dump.rdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use enum_dispatch::enum_dispatch; | ||
use thiserror::Error; | ||
|
||
use crate::{RespArray, RespError, RespFrame}; | ||
|
||
use super::{ | ||
echo::Echo, | ||
hmap::{HGet, HGetAll, HMGet, HSet}, | ||
map::{Get, Set}, | ||
set::{SAdd, SIsMember}, | ||
unrecognized::Unrecognized, | ||
}; | ||
|
||
#[enum_dispatch(CommandExecutor)] | ||
#[derive(Debug)] | ||
pub enum Command { | ||
Get(Get), | ||
Set(Set), | ||
HGet(HGet), | ||
HSet(HSet), | ||
HGetAll(HGetAll), | ||
Echo(Echo), | ||
HMGet(HMGet), | ||
SAdd(SAdd), | ||
SIsMember(SIsMember), // S 表示 Set | ||
// unrecognized command | ||
Unrecognized(Unrecognized), | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub enum CommandError { | ||
#[error("Invalid command: {0}")] | ||
InvalidCommand(String), | ||
#[error("Invalid argument: {0}")] | ||
InvalidArgument(String), | ||
#[error("{0}")] | ||
RespError(#[from] RespError), | ||
#[error("Utf8 error: {0}")] | ||
Utf8Error(#[from] std::string::FromUtf8Error), | ||
} | ||
|
||
impl TryFrom<RespArray> for Command { | ||
type Error = CommandError; | ||
fn try_from(v: RespArray) -> Result<Self, Self::Error> { | ||
match v.first() { | ||
Some(RespFrame::BulkString(ref cmd)) => match cmd.as_ref() { | ||
b"get" => Ok(Get::try_from(v)?.into()), | ||
b"set" => Ok(Set::try_from(v)?.into()), | ||
b"hget" => Ok(HGet::try_from(v)?.into()), | ||
b"hset" => Ok(HSet::try_from(v)?.into()), | ||
b"hgetall" => Ok(HGetAll::try_from(v)?.into()), | ||
b"echo" => Ok(Echo::try_from(v)?.into()), | ||
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()), | ||
}, | ||
_ => Err(CommandError::InvalidCommand( | ||
"Command must have a BulkString as the first argument".to_string(), | ||
)), | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<RespFrame> for Command { | ||
type Error = CommandError; | ||
fn try_from(v: RespFrame) -> Result<Self, Self::Error> { | ||
match v { | ||
RespFrame::Array(array) => array.try_into(), | ||
_ => Err(CommandError::InvalidCommand( | ||
"Command must be an Array".to_string(), | ||
)), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use crate::{Backend, RespArray, RespFrame}; | ||
|
||
use super::{extract_args, validate_command, CommandError, CommandExecutor}; | ||
|
||
// echo: https://redis.io/docs/latest/commands/echo/ | ||
|
||
#[derive(Debug)] | ||
pub struct Echo { | ||
message: String, | ||
} | ||
|
||
impl CommandExecutor for Echo { | ||
fn execute(self, _backend: &Backend) -> RespFrame { | ||
RespFrame::BulkString(self.message.into()) | ||
} | ||
} | ||
|
||
impl TryFrom<RespArray> for Echo { | ||
type Error = CommandError; | ||
fn try_from(value: RespArray) -> Result<Self, Self::Error> { | ||
validate_command(&value, &["echo"], 1)?; // validate get | ||
|
||
let mut args = extract_args(value, 1)?.into_iter(); | ||
match args.next() { | ||
Some(RespFrame::BulkString(message)) => Ok(Echo { | ||
message: String::from_utf8(message.0)?, | ||
}), | ||
_ => Err(CommandError::InvalidArgument("Invalid message".to_string())), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{BulkString, RespDecode}; | ||
|
||
use super::*; | ||
use anyhow::Result; | ||
use bytes::BytesMut; | ||
|
||
#[test] | ||
fn test_echo_from_resp_array() -> Result<()> { | ||
let mut buf = BytesMut::new(); | ||
buf.extend_from_slice(b"*2\r\n$4\r\necho\r\n$5\r\nhello\r\n"); | ||
|
||
let frame = RespArray::decode(&mut buf)?; | ||
|
||
let result: Echo = frame.try_into()?; | ||
assert_eq!(result.message, "hello"); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_echo_command() -> Result<()> { | ||
// let backend = Backend::new(); | ||
// let cmd = Echo { | ||
// message: "hello world".to_string(), | ||
// }; | ||
// let result = cmd.execute(&backend); | ||
// assert_eq!(result, RespFrame::BulkString(b"hello world".into())); | ||
|
||
// Ok(()) | ||
|
||
let command = Echo::try_from(RespArray::new([ | ||
BulkString::new("echo").into(), | ||
BulkString::new("hello").into(), | ||
]))?; | ||
assert_eq!(command.message, "hello"); | ||
|
||
let backend = Backend::new(); | ||
let result = command.execute(&backend); | ||
assert_eq!(result, RespFrame::BulkString(b"hello".into())); | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.