From 18b7911c656b531fc5d7fe15245e765951f3e65e Mon Sep 17 00:00:00 2001 From: omagdy Date: Wed, 16 Jul 2025 06:42:23 +0300 Subject: feat: Finished ECHO command --- src/resp_commands.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/resp_commands.rs (limited to 'src/resp_commands.rs') diff --git a/src/resp_commands.rs b/src/resp_commands.rs new file mode 100644 index 0000000..82c0079 --- /dev/null +++ b/src/resp_commands.rs @@ -0,0 +1,59 @@ +use crate::resp_parser::*; + +pub enum RespCommands { + PING, + ECHO(String), + GET(String), + SET(String), + Invalid, +} + +impl RespCommands { + pub fn execute(self) -> Vec { + match self { + RespCommands::PING => b"+PONG\r\n".to_vec(), + RespCommands::ECHO(echo_string) => echo_string.into_bytes(), + RespCommands::GET(_) => todo!(), + RespCommands::SET(_) => todo!(), + RespCommands::Invalid => todo!(), + } + } +} + +impl From for RespCommands { + fn from(value: RespType) -> Self { + match value { + RespType::Array(vec) if vec.len() > 1 => match (&vec[0], &vec[1]) { + (RespType::BulkString(command), RespType::BulkString(argument)) => { + if let Ok(command) = str::from_utf8(&command) { + match command { + "PING" => Self::PING, + "ECHO" => Self::ECHO(format!( + "+{}\r\n", + String::from_utf8(argument.clone()).unwrap() + )), + _ => Self::Invalid, + } + } else { + Self::Invalid + } + } + _ => todo!(), + }, + RespType::Array(vec) => match &vec[0] { + RespType::BulkString(command) => { + if let Ok(command) = str::from_utf8(&command) { + match command { + "PING" => Self::PING, + _ => Self::Invalid, + } + } else { + Self::Invalid + } + } + _ => Self::Invalid, + }, + _ => todo!(), + } + } +} -- cgit v1.2.3