implement COMMAND

This commit is contained in:
Maxime Van Hees 2025-08-18 16:21:49 +02:00
parent afa1033cd6
commit a306544a34

View File

@ -40,6 +40,7 @@ pub enum Cmd {
Client(Vec<String>), Client(Vec<String>),
ClientSetName(String), ClientSetName(String),
ClientGetName, ClientGetName,
Command(Vec<String>),
// List commands // List commands
LPush(String, Vec<String>), LPush(String, Vec<String>),
RPush(String, Vec<String>), RPush(String, Vec<String>),
@ -345,6 +346,10 @@ impl Cmd {
Cmd::Client(vec![]) Cmd::Client(vec![])
} }
} }
"command" => {
let args = if cmd.len() > 1 { cmd[1..].to_vec() } else { vec![] };
Cmd::Command(args)
}
"lpush" => { "lpush" => {
if cmd.len() < 3 { if cmd.len() < 3 {
return Err(DBError(format!("wrong number of arguments for LPUSH command"))); return Err(DBError(format!("wrong number of arguments for LPUSH command")));
@ -540,6 +545,7 @@ impl Cmd {
Cmd::Client(_) => Ok(Protocol::SimpleString("OK".to_string())), Cmd::Client(_) => Ok(Protocol::SimpleString("OK".to_string())),
Cmd::ClientSetName(name) => client_setname_cmd(server, &name).await, Cmd::ClientSetName(name) => client_setname_cmd(server, &name).await,
Cmd::ClientGetName => client_getname_cmd(server).await, Cmd::ClientGetName => client_getname_cmd(server).await,
Cmd::Command(_) => Ok(Protocol::Array(vec![])),
// List commands // List commands
Cmd::LPush(key, elements) => lpush_cmd(server, &key, &elements).await, Cmd::LPush(key, elements) => lpush_cmd(server, &key, &elements).await,
Cmd::RPush(key, elements) => rpush_cmd(server, &key, &elements).await, Cmd::RPush(key, elements) => rpush_cmd(server, &key, &elements).await,
@ -862,17 +868,19 @@ async fn info_cmd(server: &Server, section: &Option<String>) -> Result<Protocol,
info_string.push_str(&format!("# Keyspace\n")); info_string.push_str(&format!("# Keyspace\n"));
info_string.push_str(&format!("db{}:keys=0,expires=0,avg_ttl=0\n", info.selected_db)); info_string.push_str(&format!("db{}:keys=0,expires=0,avg_ttl=0\n", info.selected_db));
match section { match section {
Some(s) => match s.as_str() { Some(s) => {
"replication" => Ok(Protocol::BulkString( let sl = s.to_lowercase();
"role:master\nmaster_replid:8371b4fb1155b71f4a04d3e1bc3e18c4a990aeea\nmaster_repl_offset:0\n".to_string() if sl == "replication" {
)), Ok(Protocol::BulkString(
_ => Err(DBError(format!("unsupported section {:?}", s))), "role:master\nmaster_replid:8371b4fb1155b71f4a04d3e1bc3e18c4a990aeea\nmaster_repl_offset:0\n".to_string()
}, ))
None => { } else {
Ok(Protocol::BulkString(info_string)) // Return general info for unknown sections (e.g., SERVER)
Ok(Protocol::BulkString(info_string))
}
} }
None => Ok(Protocol::BulkString(info_string)),
} }
} }