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
#![doc(html_root_url="https://sorccu.github.io/r2d2-redis/doc/v0.2.0")]
#![warn(missing_docs)]
extern crate r2d2;
extern crate redis;
use std::error;
use std::error::Error as _StdError;
use std::fmt;
#[derive(Debug)]
pub enum Error {
Other(redis::RedisError),
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self.cause() {
Some(cause) => write!(fmt, "{}: {}", self.description(), cause),
None => write!(fmt, "{}", self.description()),
}
}
}
impl error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::Other(ref err) => err.description()
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
Error::Other(ref err) => err.cause()
}
}
}
#[derive(Debug)]
pub struct RedisConnectionManager {
connection_info: redis::ConnectionInfo
}
impl RedisConnectionManager {
pub fn new<T: redis::IntoConnectionInfo>(params: T)
-> Result<RedisConnectionManager, redis::RedisError> {
Ok(RedisConnectionManager {
connection_info: try!(params.into_connection_info()),
})
}
}
impl r2d2::ManageConnection for RedisConnectionManager {
type Connection = redis::Connection;
type Error = Error;
fn connect(&self) -> Result<redis::Connection, Error> {
match redis::Client::open(self.connection_info.clone()) {
Ok(client) => {
client.get_connection().map_err(Error::Other)
},
Err(err) => Err(Error::Other(err))
}
}
fn is_valid(&self, conn: &mut redis::Connection) -> Result<(), Error> {
redis::cmd("PING").query(conn).map_err(Error::Other)
}
fn has_broken(&self, _conn: &mut redis::Connection) -> bool {
false
}
}