1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
use crate::resp_parser::*;
pub enum RespCommands {
PING,
ECHO(String),
GET(String),
SET(String),
Invalid,
}
impl RespCommands {
pub fn execute(self) -> Vec<u8> {
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<RespType> 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!(),
}
}
}
|