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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use crate::commands::helpers::{CmdStr, RespMap};
use crate::commands::{Command, ResponseTypeError};
use alloc::string::String;
use alloc::vec::Vec;
use redis_protocol::resp2::types::Frame as Resp2Frame;
use redis_protocol::resp3::types::{Frame as Resp3Frame, RespVersion};
pub struct HelloCommand {}
impl Command<Resp3Frame> for HelloCommand {
type Response = HelloResponse;
fn encode(&self) -> Resp3Frame {
Resp3Frame::Hello {
version: RespVersion::RESP3,
auth: None,
}
}
fn eval_response(&self, frame: Resp3Frame) -> Result<Self::Response, ResponseTypeError> {
HelloResponse::try_from(frame)
}
}
impl Command<Resp2Frame> for HelloCommand {
type Response = HelloResponse;
fn encode(&self) -> Resp2Frame {
unimplemented!("Command requires RESP3");
}
fn eval_response(&self, _frame: Resp2Frame) -> Result<Self::Response, ResponseTypeError> {
unimplemented!("Command requires RESP3");
}
}
#[derive(Debug)]
pub struct HelloResponse {
pub server: String,
pub version: String,
pub protocol: i64,
pub id: i64,
pub mode: String,
pub role: String,
pub modules: Vec<Resp3Frame>,
}
impl TryFrom<Resp3Frame> for HelloResponse {
type Error = ResponseTypeError;
fn try_from(frame: Resp3Frame) -> Result<Self, Self::Error> {
let map = match frame {
Resp3Frame::Map { data, attributes: _ } => data,
_ => return Err(ResponseTypeError {}),
};
let map_cmd = RespMap::new(&map);
Ok(HelloResponse {
server: map_cmd.find_string("server").ok_or(ResponseTypeError {})?,
version: map_cmd.find_string("version").ok_or(ResponseTypeError {})?,
protocol: map_cmd.find_integer("proto").ok_or(ResponseTypeError {})?,
id: map_cmd.find_integer("id").ok_or(ResponseTypeError {})?,
mode: map_cmd.find_string("mode").ok_or(ResponseTypeError {})?,
role: map_cmd.find_string("role").ok_or(ResponseTypeError {})?,
modules: match map.get(&CmdStr::new("modules").to_blob()).ok_or(ResponseTypeError {})? {
Resp3Frame::Array { data, attributes: _ } => data.clone(),
_ => return Err(ResponseTypeError {}),
},
})
}
}