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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::fmt;
mod ascii;
pub use ascii::{parse_ascii_metadump_response, parse_ascii_response, parse_ascii_stats_response};
#[derive(Clone, Debug, PartialEq)]
pub struct Value {
pub key: Vec<u8>,
pub cas: Option<u64>,
pub flags: u32,
pub data: Vec<u8>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Status {
Stored,
NotStored,
Deleted,
Touched,
Exists,
NotFound,
Error(ErrorKind),
}
#[derive(Clone, Debug, PartialEq)]
pub enum ErrorKind {
Generic(String),
NonexistentCommand,
Protocol(Option<String>),
Client(String),
Server(String),
}
#[derive(Clone, Debug, PartialEq)]
pub enum Response {
Status(Status),
Data(Option<Vec<Value>>),
IncrDecr(u64),
}
#[derive(Clone, Debug, PartialEq)]
pub enum MetadumpResponse {
Busy(String),
BadClass(String),
Entry(KeyMetadata),
End,
}
#[derive(Clone, Debug, PartialEq)]
pub enum StatsResponse {
Entry(String, String),
End,
}
#[derive(Clone, Debug, PartialEq)]
pub struct KeyMetadata {
pub key: Vec<u8>,
pub expiration: i64,
pub last_accessed: u64,
pub cas: u64,
pub fetched: bool,
pub class_id: u32,
pub size: u32,
}
impl fmt::Display for Status {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Stored => write!(f, "stored"),
Self::NotStored => write!(f, "not stored"),
Self::Deleted => write!(f, "deleted"),
Self::Touched => write!(f, "touched"),
Self::Exists => write!(f, "exists"),
Self::NotFound => write!(f, "not found"),
Self::Error(ek) => write!(f, "error: {}", ek),
}
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Generic(s) => write!(f, "generic: {}", s),
Self::NonexistentCommand => write!(f, "command does not exist"),
Self::Protocol(s) => match s {
Some(s) => write!(f, "protocol: {}", s),
None => write!(f, "protocol"),
},
Self::Client(s) => write!(f, "client: {}", s),
Self::Server(s) => write!(f, "server: {}", s),
}
}
}
impl From<MetadumpResponse> for Status {
fn from(resp: MetadumpResponse) -> Self {
match resp {
MetadumpResponse::BadClass(s) => {
Status::Error(ErrorKind::Generic(format!("BADCLASS {}", s)))
}
MetadumpResponse::Busy(s) => Status::Error(ErrorKind::Generic(format!("BUSY {}", s))),
_ => unreachable!("Metadump Entry/End states should never be used as a Status!"),
}
}
}