1use std::fmt;
2mod ascii;
3pub use ascii::{parse_ascii_metadump_response, parse_ascii_response, parse_ascii_stats_response};
4
5#[derive(Clone, Debug, PartialEq)]
7pub struct Value {
8 pub key: Vec<u8>,
10 pub cas: Option<u64>,
12 pub flags: u32,
16 pub data: Vec<u8>,
18}
19
20#[derive(Clone, Debug, PartialEq)]
22pub enum Status {
23 Stored,
25 NotStored,
27 Deleted,
29 Touched,
31 Exists,
33 NotFound,
35 Error(ErrorKind),
37}
38
39#[derive(Clone, Debug, PartialEq)]
41pub enum ErrorKind {
42 Generic(String),
44 NonexistentCommand,
46 Protocol(Option<String>),
48 Client(String),
50 Server(String),
52}
53
54#[derive(Clone, Debug, PartialEq)]
56pub enum Response {
57 Status(Status),
59 Data(Option<Vec<Value>>),
61 IncrDecr(u64),
63}
64
65#[derive(Clone, Debug, PartialEq)]
67pub enum MetadumpResponse {
68 Busy(String),
70 BadClass(String),
72 Entry(KeyMetadata),
74 End,
76}
77
78#[derive(Clone, Debug, PartialEq)]
80pub enum StatsResponse {
81 Entry(String, String),
83 End,
85}
86
87#[derive(Clone, Debug, PartialEq)]
89pub struct KeyMetadata {
90 pub key: Vec<u8>,
92 pub expiration: i64,
94 pub last_accessed: u64,
96 pub cas: u64,
98 pub fetched: bool,
100 pub class_id: u32,
102 pub size: u32,
104}
105
106impl fmt::Display for Status {
107 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
108 match self {
109 Self::Stored => write!(f, "stored"),
110 Self::NotStored => write!(f, "not stored"),
111 Self::Deleted => write!(f, "deleted"),
112 Self::Touched => write!(f, "touched"),
113 Self::Exists => write!(f, "exists"),
114 Self::NotFound => write!(f, "not found"),
115 Self::Error(ek) => write!(f, "error: {}", ek),
116 }
117 }
118}
119
120impl fmt::Display for ErrorKind {
121 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
122 match self {
123 Self::Generic(s) => write!(f, "generic: {}", s),
124 Self::NonexistentCommand => write!(f, "command does not exist"),
125 Self::Protocol(s) => match s {
126 Some(s) => write!(f, "protocol: {}", s),
127 None => write!(f, "protocol"),
128 },
129 Self::Client(s) => write!(f, "client: {}", s),
130 Self::Server(s) => write!(f, "server: {}", s),
131 }
132 }
133}
134
135impl From<MetadumpResponse> for Status {
136 fn from(resp: MetadumpResponse) -> Self {
137 match resp {
138 MetadumpResponse::BadClass(s) => {
139 Status::Error(ErrorKind::Generic(format!("BADCLASS {}", s)))
140 }
141 MetadumpResponse::Busy(s) => Status::Error(ErrorKind::Generic(format!("BUSY {}", s))),
142 _ => unreachable!("Metadump Entry/End states should never be used as a Status!"),
143 }
144 }
145}