3
3
#![ warn( missing_docs) ]
4
4
pub extern crate r2d2;
5
5
pub extern crate postgres;
6
+ extern crate postgres_shared;
6
7
7
- use std:: error;
8
- use std:: error:: Error as _StdError;
9
- use std:: fmt;
8
+ use postgres:: { Connection , Error , Result } ;
10
9
use postgres:: params:: { ConnectParams , IntoConnectParams } ;
11
10
use postgres:: tls:: TlsHandshake ;
12
11
@@ -21,37 +20,6 @@ pub enum TlsMode {
21
20
Require ( Box < TlsHandshake +Sync +Send > ) ,
22
21
}
23
22
24
- /// A unified enum of errors returned by postgres::Connection
25
- #[ derive( Debug ) ]
26
- pub enum Error {
27
- /// A postgres::error::ConnectError
28
- Connect ( postgres:: error:: ConnectError ) ,
29
- /// An postgres::error::Error
30
- Other ( postgres:: error:: Error ) ,
31
- }
32
-
33
- impl fmt:: Display for Error {
34
- fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
35
- write ! ( fmt, "{}: {}" , self . description( ) , self . cause( ) . unwrap( ) )
36
- }
37
- }
38
-
39
- impl error:: Error for Error {
40
- fn description ( & self ) ->& str {
41
- match * self {
42
- Error :: Connect ( _) =>"Error opening a connection" ,
43
- Error :: Other ( _) =>"Error communicating with server" ,
44
- }
45
- }
46
-
47
- fn cause ( & self ) ->Option < & error:: Error > {
48
- match * self {
49
- Error :: Connect ( ref err) =>Some ( erras & error:: Error ) ,
50
- Error :: Other ( ref err) =>Some ( erras & error:: Error ) ,
51
- }
52
- }
53
- }
54
-
55
23
/// An `r2d2::ManageConnection` for `postgres::Connection`s.
56
24
///
57
25
/// ## Example
@@ -92,13 +60,11 @@ impl PostgresConnectionManager {
92
60
/// types.
93
61
pub fn new < T > ( params : T ,
94
62
ssl_mode : TlsMode )
95
- ->Result < PostgresConnectionManager , postgres :: error :: ConnectError >
63
+ ->Result < PostgresConnectionManager >
96
64
where T : IntoConnectParams
97
65
{
98
- let params =match params. into_connect_params ( ) {
99
- Ok ( params) => params,
100
- Err ( err) =>return Err ( postgres:: error:: ConnectError :: ConnectParams ( err) ) ,
101
- } ;
66
+ // fixme we shouldn't be using this private constructor :(
67
+ let params = params. into_connect_params ( ) . map_err ( postgres_shared:: error:: connect) ?;
102
68
103
69
Ok ( PostgresConnectionManager {
104
70
params : params,
@@ -108,23 +74,23 @@ impl PostgresConnectionManager {
108
74
}
109
75
110
76
impl r2d2:: ManageConnection for PostgresConnectionManager {
111
- type Connection =postgres :: Connection ;
77
+ type Connection =Connection ;
112
78
type Error =Error ;
113
79
114
- fn connect ( & self ) ->Result < postgres:: Connection , Error > {
80
+ fn connect ( & self ) ->Result < postgres:: Connection > {
115
81
let mode =match self . ssl_mode {
116
82
TlsMode :: None => postgres:: TlsMode :: None ,
117
83
TlsMode :: Prefer ( ref n) => postgres:: TlsMode :: Prefer ( & * * n) ,
118
84
TlsMode :: Require ( ref n) => postgres:: TlsMode :: Require ( & * * n) ,
119
85
} ;
120
- postgres:: Connection :: connect ( self . params . clone ( ) , mode) . map_err ( Error :: Connect )
86
+ postgres:: Connection :: connect ( self . params . clone ( ) , mode)
121
87
}
122
88
123
- fn is_valid ( & self , conn : & mut postgres :: Connection ) ->Result < ( ) , Error > {
124
- conn. batch_execute ( "" ) . map_err ( Error :: Other )
89
+ fn is_valid ( & self , conn : & mut Connection ) ->Result < ( ) > {
90
+ conn. batch_execute ( "" )
125
91
}
126
92
127
- fn has_broken ( & self , conn : & mut postgres :: Connection ) ->bool {
93
+ fn has_broken ( & self , conn : & mut Connection ) ->bool {
128
94
conn. is_desynchronized ( )
129
95
}
130
96
}