@@ -3,6 +3,11 @@ use crate::Error;
33use std:: convert:: TryFrom ;
44use std:: path:: PathBuf ;
55use std:: str:: FromStr ;
6+ #[ cfg( any(
7+ feature = "postgres" ,
8+ feature = "tokio-postgres" ,
9+ feature = "tiberius-config"
10+ ) ) ]
611use std:: { borrow:: Cow , collections:: HashMap } ;
712use url:: Url ;
813
@@ -35,7 +40,8 @@ impl Config {
3540 db_user : None ,
3641 db_pass : None ,
3742 db_name : None ,
38- use_tls : None ,
43+ #[ cfg( any( feature = "postgres" , feature = "tokio-postgres" ) ) ]
44+ use_tls : false ,
3945 #[ cfg( feature = "tiberius-config" ) ]
4046 trust_cert : false ,
4147 } ,
@@ -141,7 +147,8 @@ impl Config {
141147 self . main . db_port . as_deref ( )
142148 }
143149
144- pub fn use_tls ( & self ) -> Option < bool > {
150+ #[ cfg( any( feature = "postgres" , feature = "tokio-postgres" ) ) ]
151+ pub fn use_tls ( & self ) -> bool {
145152 self . main . use_tls
146153 }
147154
@@ -189,6 +196,16 @@ impl Config {
189196 } ,
190197 }
191198 }
199+
200+ #[ cfg( any( feature = "postgres" , feature = "tokio-postgres" ) ) ]
201+ pub fn set_use_tls ( self , use_tls : bool ) -> Config {
202+ Config {
203+ main : Main {
204+ use_tls,
205+ ..self . main
206+ } ,
207+ }
208+ }
192209}
193210
194211impl TryFrom < Url > for Config {
@@ -209,6 +226,11 @@ impl TryFrom<Url> for Config {
209226 }
210227 } ;
211228
229+ #[ cfg( any(
230+ feature = "postgres" ,
231+ feature = "tokio-postgres" ,
232+ feature = "tiberius-config"
233+ ) ) ]
212234 let query_params = url
213235 . query_pairs ( )
214236 . collect :: < HashMap < Cow < ' _ , str > , Cow < ' _ , str > > > ( ) ;
@@ -228,12 +250,10 @@ impl TryFrom<Url> for Config {
228250 }
229251 }
230252
231- let use_tls = match query_params
232- . get ( "sslmode" )
233- . unwrap_or ( & Cow :: Borrowed ( "disable" ) )
234- {
235- & Cow :: Borrowed ( "disable" ) => false ,
236- & Cow :: Borrowed ( "require" ) => true ,
253+ #[ cfg( any( feature = "postgres" , feature = "tokio-postgres" ) ) ]
254+ let use_tls = match query_params. get ( "sslmode" ) {
255+ Some ( & Cow :: Borrowed ( "require" ) ) => true ,
256+ Some ( & Cow :: Borrowed ( "disable" ) ) | None => false ,
237257 _ => {
238258 return Err ( Error :: new (
239259 Kind :: ConfigError ( "Invalid sslmode value, please use disable/require" . into ( ) ) ,
@@ -257,7 +277,8 @@ impl TryFrom<Url> for Config {
257277 db_user : Some ( url. username ( ) . to_string ( ) ) ,
258278 db_pass : url. password ( ) . map ( |r| r. to_string ( ) ) ,
259279 db_name : Some ( url. path ( ) . trim_start_matches ( '/' ) . to_string ( ) ) ,
260- use_tls : Some ( use_tls) ,
280+ #[ cfg( any( feature = "postgres" , feature = "tokio-postgres" ) ) ]
281+ use_tls,
261282 #[ cfg( feature = "tiberius-config" ) ]
262283 trust_cert,
263284 } ,
@@ -290,7 +311,9 @@ struct Main {
290311 db_user : Option < String > ,
291312 db_pass : Option < String > ,
292313 db_name : Option < String > ,
293- use_tls : Option < bool > ,
314+ #[ cfg( feature = "postgres" ) ]
315+ #[ serde( default ) ]
316+ use_tls : bool ,
294317 #[ cfg( feature = "tiberius-config" ) ]
295318 #[ serde( default ) ]
296319 trust_cert : bool ,
@@ -474,18 +497,40 @@ mod tests {
474497 ) ;
475498 }
476499
500+ #[ cfg( any( feature = "postgres" , feature = "tokio-postgres" ) ) ]
477501 #[ test]
478502 fn builds_from_sslmode_str ( ) {
479- let config =
503+ use crate :: config:: ConfigDbType ;
504+
505+ let config_disable =
480506 Config :: from_str ( "postgres://root:1234@localhost:5432/refinery?sslmode=disable" )
481507 . unwrap ( ) ;
482- assert ! ( config . use_tls( ) . is_some ( ) ) ;
483- assert ! ( !config . use_tls ( ) . unwrap ( ) ) ;
484- let config =
508+ assert ! ( !config_disable . use_tls( ) ) ;
509+
510+ let config_require =
485511 Config :: from_str ( "postgres://root:1234@localhost:5432/refinery?sslmode=require" )
486512 . unwrap ( ) ;
487- assert ! ( config. use_tls( ) . is_some( ) ) ;
488- assert ! ( config. use_tls( ) . unwrap( ) ) ;
513+ assert ! ( config_require. use_tls( ) ) ;
514+
515+ // Verify that manually created config matches parsed URL config
516+ let manual_config_disable = Config :: new ( ConfigDbType :: Postgres )
517+ . set_db_user ( "root" )
518+ . set_db_pass ( "1234" )
519+ . set_db_host ( "localhost" )
520+ . set_db_port ( "5432" )
521+ . set_db_name ( "refinery" )
522+ . set_use_tls ( false ) ;
523+ assert_eq ! ( config_disable. use_tls( ) , manual_config_disable. use_tls( ) ) ;
524+
525+ let manual_config_require = Config :: new ( ConfigDbType :: Postgres )
526+ . set_db_user ( "root" )
527+ . set_db_pass ( "1234" )
528+ . set_db_host ( "localhost" )
529+ . set_db_port ( "5432" )
530+ . set_db_name ( "refinery" )
531+ . set_use_tls ( true ) ;
532+ assert_eq ! ( config_require. use_tls( ) , manual_config_require. use_tls( ) ) ;
533+
489534 let config =
490535 Config :: from_str ( "postgres://root:1234@localhost:5432/refinery?sslmode=invalidvalue" ) ;
491536 assert ! ( config. is_err( ) ) ;
0 commit comments