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
use std::io;
use std::fmt;
use std::error;
use std::string;
#[derive(Debug)]
pub enum MemcacheError {
Io(io::Error),
FromUtf8(string::FromUtf8Error),
Error,
ClientError(String),
ServerError(String),
}
impl fmt::Display for MemcacheError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
MemcacheError::Io(ref err) => err.fmt(f),
MemcacheError::FromUtf8(ref err) => err.fmt(f),
MemcacheError::Error => write!(f, "Error"),
MemcacheError::ClientError(ref s) => s.fmt(f),
MemcacheError::ServerError(ref s) => s.fmt(f),
}
}
}
impl error::Error for MemcacheError {
fn description(&self) -> &str {
match *self {
MemcacheError::Io(ref err) => err.description(),
MemcacheError::FromUtf8(ref err) => err.description(),
MemcacheError::Error => "Error",
MemcacheError::ClientError(ref s) => s.as_str(),
MemcacheError::ServerError(ref s) => s.as_str(),
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
MemcacheError::Io(ref err) => err.cause(),
MemcacheError::FromUtf8(ref err) => err.cause(),
MemcacheError::Error => None,
MemcacheError::ClientError(_) => None,
MemcacheError::ServerError(_) => None,
}
}
}
impl From<io::Error> for MemcacheError {
fn from(err: io::Error) -> MemcacheError {
MemcacheError::Io(err)
}
}
impl From<string::FromUtf8Error> for MemcacheError {
fn from(err: string::FromUtf8Error) -> MemcacheError {
MemcacheError::FromUtf8(err)
}
}
impl From<String> for MemcacheError {
fn from(s: String) -> MemcacheError {
if s == "ERROR\r\n" {
return MemcacheError::Error;
} else if s.starts_with("CLIENT_ERROR") {
return MemcacheError::ClientError(s);
} else if s.starts_with("SERVER_ERROR") {
return MemcacheError::ServerError(s);
} else {
panic!("{} if not a memcached error!", s);
}
}
}
pub fn is_memcache_error(s: &str) -> bool {
return s == "ERROR\r\n" || s.starts_with("CIENT_ERROR") || s.starts_with("SERVER_ERROR");
}