From 846479e6bcf8238879546534b09e141b9bb668f8 Mon Sep 17 00:00:00 2001 From: omagdy Date: Sun, 20 Jul 2025 16:29:34 +0300 Subject: feat: Added parsing of simple command line arguments and CONFIG GET command implementation --- src/resp_commands.rs | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) (limited to 'src/resp_commands.rs') diff --git a/src/resp_commands.rs b/src/resp_commands.rs index 4d01507..37273f8 100644 --- a/src/resp_commands.rs +++ b/src/resp_commands.rs @@ -1,6 +1,6 @@ use crate::{resp_parser::*, shared_cache::*}; -use std::collections::HashMap; -use std::time::{Duration, SystemTime, UNIX_EPOCH}; +use crate::{Config, SharedConfig}; +use std::time::{SystemTime, UNIX_EPOCH}; #[derive(Debug, Clone)] pub enum SetCondition { @@ -118,11 +118,12 @@ pub enum RedisCommands { ECHO(String), GET(String), SET(SetCommand), + CONFIG_GET(String), Invalid, } impl RedisCommands { - pub fn execute(self, cache: SharedCache) -> Vec { + pub fn execute(self, cache: SharedCache, config: SharedConfig) -> Vec { match self { RedisCommands::PING => resp!("PONG"), RedisCommands::ECHO(echo_string) => resp!(echo_string), @@ -193,6 +194,29 @@ impl RedisCommands { None => return resp!(null), } } + RedisCommands::CONFIG_GET(s) => { + use RespType as RT; + let config = config.clone(); + if let Some(conf) = config.as_ref() { + let dir = conf.dir.clone().unwrap(); + let dbfilename = conf.dbfilename.clone().unwrap(); + match s.as_str() { + "dir" => RT::Array(vec![ + RT::BulkString(s.as_bytes().to_vec()), + RT::BulkString(dir.as_bytes().to_vec()), + ]) + .to_resp_bytes(), + "dbfilename" => RT::Array(vec![ + RT::BulkString(s.as_bytes().to_vec()), + RT::BulkString(dbfilename.as_bytes().to_vec()), + ]) + .to_resp_bytes(), + _ => unreachable!(), + } + } else { + unreachable!() + } + } RedisCommands::Invalid => todo!(), } } @@ -347,6 +371,18 @@ impl From for RedisCommands { } } } + "CONFIG" => { + let Some(sub_command) = args.next() else { + return Self::Invalid; + }; + let Some(key) = args.next() else { + return Self::Invalid; + }; + if &sub_command.to_uppercase() == &"GET" { + return Self::CONFIG_GET(key); + } + Self::Invalid + } _ => Self::Invalid, } } -- cgit v1.2.3