@@ -147,13 +147,40 @@ pub struct IndexerConfig {
147147
148148#[ derive( Debug ,  Deserialize ) ]  
149149#[ cfg_attr( test,  derive( PartialEq ) ) ]  
150- pub  struct  DatabaseConfig  { 
151-     pub  postgres_url :  Url , 
152-     pub  postgres_host :  String , 
153-     pub  postgres_port :  i32 , 
154-     pub  postgres_user :  String , 
155-     pub  postgres_password :  String , 
156-     pub  postgress_db :  String , 
150+ #[ serde( untagged) ]  
151+ pub  enum  DatabaseConfig  { 
152+     PostgresUrl  { 
153+         postgres_url :  Url , 
154+     } , 
155+     PostgresVars  { 
156+         host :  String , 
157+         port :  Option < u16 > , 
158+         user :  String , 
159+         password :  Option < String > , 
160+         database :  String , 
161+     } , 
162+ } 
163+ impl  DatabaseConfig  { 
164+     pub  fn  get_formated_postgres_url ( & self )  -> Url  { 
165+         match  self  { 
166+             DatabaseConfig :: PostgresUrl  {  postgres_url }  => postgres_url. clone ( ) , 
167+             DatabaseConfig :: PostgresVars  { 
168+                 host, 
169+                 port, 
170+                 user, 
171+                 password, 
172+                 database, 
173+             }  => { 
174+                 let  postgres_url_str = format ! ( "postgres://{}@{}/{}" ,  user,  host,  database) ; 
175+                 let  mut  postgres_url = Url :: parse ( & postgres_url_str) . unwrap ( ) ; 
176+                 postgres_url
177+                     . set_password ( password. as_deref ( ) ) 
178+                     . expect ( "url is wrong" ) ; 
179+                 postgres_url. set_port ( * port) . expect ( "url is wrong" ) ; 
180+                 postgres_url
181+             } 
182+         } 
183+     } 
157184} 
158185
159186#[ derive( Debug ,  Deserialize ) ]  
@@ -291,6 +318,8 @@ mod tests {
291318
292319    use  crate :: { Config ,  ConfigPrefix } ; 
293320
321+     use  super :: DatabaseConfig ; 
322+ 
294323    #[ test]  
295324    fn  test_minimal_config ( )  { 
296325        Config :: parse ( 
@@ -403,4 +432,32 @@ mod tests {
403432            test_value
404433        ) ; 
405434    } 
435+     #[ test]  
436+     fn  test_url_format ( )  { 
437+         let  data = DatabaseConfig :: PostgresVars  { 
438+             host :  String :: from ( "postgres" ) , 
439+             port :  Some ( 1234 ) , 
440+             user :  String :: from ( "postgres" ) , 
441+             password :  Some ( String :: from ( "postgres" ) ) , 
442+             database :  String :: from ( "postgres" ) , 
443+         } ; 
444+         let  formated_data = data. get_formated_postgres_url ( ) ; 
445+         assert_eq ! ( 
446+             formated_data. as_str( ) , 
447+             "postgres://postgres:postgres@postgres:1234/postgres" 
448+         ) ; 
449+ 
450+         let  data = DatabaseConfig :: PostgresVars  { 
451+             host :  String :: from ( "postgres" ) , 
452+             port :  None , 
453+             user :  String :: from ( "postgres" ) , 
454+             password :  None , 
455+             database :  String :: from ( "postgres" ) , 
456+         } ; 
457+         let  formated_data = data. get_formated_postgres_url ( ) ; 
458+         assert_eq ! ( 
459+             formated_data. as_str( ) , 
460+             "postgres://postgres@postgres/postgres" 
461+         ) ; 
462+     } 
406463} 
0 commit comments