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
use bytes::Bytes;
mod array;
mod bulk_string;
mod error;
mod integer;
mod simple_string;
pub use array::{Array, ArrayBuilder, EMPTY_ARRAY, NULL_ARRAY};
pub use bulk_string::BulkString;
pub use error::Error;
pub use integer::Integer;
pub use simple_string::SimpleString;
#[derive(Debug, Clone)]
pub enum RespError {
InvalidFirstChar,
InvalidLength,
InvalidLengthSeparator,
InvalidValue,
InvalidTerminate,
LengthsNotMatch,
}
impl std::fmt::Display for RespError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
RespError::InvalidFirstChar => {
write!(f, "Invalid first char.")
}
RespError::InvalidLength => {
write!(f, "Invalid length.")
}
RespError::InvalidLengthSeparator => {
write!(f, "Invalid length separator.")
}
RespError::InvalidValue => {
write!(f, "Invalid value.")
}
RespError::LengthsNotMatch => {
write!(f, "Lengths do not match.")
}
RespError::InvalidTerminate => {
write!(f, "Invalid terminate.")
}
}
}
}
impl std::error::Error for RespError {}
#[derive(Debug, Clone)]
pub enum RespType {
SimpleString(SimpleString),
Error(Error),
Integer(Integer),
BulkString(BulkString),
Array(Array),
}
impl RespType {
fn len(&self) -> usize {
match self {
RespType::SimpleString(simple_string) => simple_string.len(),
RespType::Error(error) => error.len(),
RespType::Integer(integer) => integer.len(),
RespType::BulkString(bulk_string) => bulk_string.len(),
RespType::Array(array) => array.len(),
}
}
fn bytes(&self) -> Bytes {
match self {
RespType::SimpleString(simple_string) => simple_string.bytes(),
RespType::Error(error) => error.bytes(),
RespType::Integer(integer) => integer.bytes(),
RespType::BulkString(bulk_string) => bulk_string.bytes(),
RespType::Array(array) => array.bytes(),
}
}
}