This repository was archived by the owner on Mar 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmod.rs
More file actions
119 lines (108 loc) · 3.18 KB
/
Copy pathmod.rs
File metadata and controls
119 lines (108 loc) · 3.18 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
use prost::bytes::BufMut;
use super::{NIL, OK, PONG};
use crate::{command::command_error::RedisCommandError, storage::models::RedisString};
pub enum RedisResponseType {
SimpleString(RedisString),
BulkString(RedisString),
Integer(i64),
Nil,
}
pub struct RedisResponse {
responses: RedisResponseInner,
}
enum RedisResponseInner {
Single(RedisResponseType),
Array(Vec<RedisResponseType>),
Error(RedisCommandError),
Okay,
Pong,
Quit,
}
impl RedisResponseType {
// move out of the enum
fn to_vec(&self) -> Vec<u8> {
use RedisResponseType::*;
match self {
SimpleString(s) | BulkString(s) => s.clone(),
Integer(num) => num.to_string().as_bytes().to_vec(),
Nil => NIL.to_vec(),
}
}
/// Move out of self and return bytes analogous to `format!("{}{}{}", symbol, data, CRLF)`
pub fn get_formatted(self) -> Vec<u8> {
use RedisResponseType::*;
let symbol = match &self {
SimpleString(_) => b'+',
BulkString(_) => b'$',
Integer(_) => b':',
Nil => return self.to_vec(),
};
let mut bytes = self.to_vec();
let mut reply =
Vec::<u8>::with_capacity(bytes.len() + 3 /* 3 more bytes for symbol and /r/n */);
reply.push(symbol);
if symbol == b'$' {
reply.put_slice(bytes.len().to_string().as_bytes());
reply.put_slice(b"\r\n");
}
//eprintln!("{:?}", bytes);
reply.append(&mut bytes);
reply.put_slice(b"\r\n");
reply
}
}
impl RedisResponse {
pub fn okay() -> Self {
Self {
responses: RedisResponseInner::Okay,
}
}
pub fn pong() -> Self {
Self {
responses: RedisResponseInner::Pong,
}
}
pub fn quit() -> Self {
Self {
responses: RedisResponseInner::Quit,
}
}
pub fn is_quit(&self) -> bool {
matches!(self.responses, RedisResponseInner::Quit)
}
pub fn single(response: RedisResponseType) -> Self {
Self {
responses: RedisResponseInner::Single(response),
}
}
pub fn array(responses: Vec<RedisResponseType>) -> Self {
Self {
responses: RedisResponseInner::Array(responses),
}
}
pub fn error(error: RedisCommandError) -> Self {
Self {
responses: RedisResponseInner::Error(error),
}
}
pub fn reply(self) -> Vec<u8> {
use RedisResponseInner::*;
match self.responses {
Okay | Quit => OK.to_vec(),
Error(e) => e.to_vec(),
Pong => PONG.to_vec(),
Single(single) => single.get_formatted(),
Array(responses) => {
let mut reply = Vec::<u8>::with_capacity(512);
reply.push(b'*');
reply.put_slice(&responses.len().to_string().as_bytes().to_vec());
reply.put_slice(b"\r\n");
for response in responses {
let mut response = response.get_formatted();
reply.append(&mut response);
}
reply
}
}
}
}