1
1
use crate :: error:: Error ;
2
2
use crate :: postgres:: PgConnectOptions ;
3
3
use percent_encoding:: percent_decode_str;
4
+ use std:: net:: IpAddr ;
4
5
use std:: str:: FromStr ;
5
6
use url:: Url ;
6
7
@@ -65,21 +66,22 @@ impl FromStr for PgConnectOptions {
65
66
}
66
67
}
67
68
68
- "application_name" => {
69
- options = options. application_name ( & * value) ;
69
+ "hostaddr" => {
70
+ value. parse :: < IpAddr > ( ) . map_err ( Error :: config) ?;
71
+ options = options. host ( & * value)
70
72
}
71
73
72
- "port" => {
73
- options = options. port ( value. parse ( ) . map_err ( Error :: config) ?) ;
74
- }
74
+ "port" => options = options. port ( value. parse ( ) . map_err ( Error :: config) ?) ,
75
75
76
76
"dbname" => options = options. database ( & * value) ,
77
77
78
78
"user" => options = options. username ( & * value) ,
79
79
80
80
"password" => options = options. password ( & * value) ,
81
81
82
- _ => { }
82
+ "application_name" => options = options. application_name ( & * value) ,
83
+
84
+ _ => log:: warn!( "ignoring unrecognized connect parameter: {}={}" , key, value) ,
83
85
}
84
86
}
85
87
@@ -104,6 +106,51 @@ fn it_parses_host_correctly_from_parameter() {
104
106
assert_eq ! ( "google.database.com" , & opts. host) ;
105
107
}
106
108
109
+ #[ test]
110
+ fn it_parses_hostaddr_correctly_from_parameter ( ) {
111
+ let uri = "postgres:///?hostaddr=8.8.8.8" ;
112
+ let opts = PgConnectOptions :: from_str ( uri) . unwrap ( ) ;
113
+
114
+ assert_eq ! ( None , opts. socket) ;
115
+ assert_eq ! ( "8.8.8.8" , & opts. host) ;
116
+ }
117
+
118
+ #[ test]
119
+ fn it_parses_port_correctly_from_parameter ( ) {
120
+ let uri = "postgres:///?port=1234" ;
121
+ let opts = PgConnectOptions :: from_str ( uri) . unwrap ( ) ;
122
+
123
+ assert_eq ! ( None , opts. socket) ;
124
+ assert_eq ! ( 1234 , opts. port) ;
125
+ }
126
+
127
+ #[ test]
128
+ fn it_parses_dbname_correctly_from_parameter ( ) {
129
+ let uri = "postgres:///?dbname=some_db" ;
130
+ let opts = PgConnectOptions :: from_str ( uri) . unwrap ( ) ;
131
+
132
+ assert_eq ! ( None , opts. socket) ;
133
+ assert_eq ! ( Some ( "some_db" ) , opts. database. as_deref( ) ) ;
134
+ }
135
+
136
+ #[ test]
137
+ fn it_parses_user_correctly_from_parameter ( ) {
138
+ let uri = "postgres:///?user=some_user" ;
139
+ let opts = PgConnectOptions :: from_str ( uri) . unwrap ( ) ;
140
+
141
+ assert_eq ! ( None , opts. socket) ;
142
+ assert_eq ! ( "some_user" , opts. username) ;
143
+ }
144
+
145
+ #[ test]
146
+ fn it_parses_password_correctly_from_parameter ( ) {
147
+ let uri = "postgres:///?password=some_pass" ;
148
+ let opts = PgConnectOptions :: from_str ( uri) . unwrap ( ) ;
149
+
150
+ assert_eq ! ( None , opts. socket) ;
151
+ assert_eq ! ( Some ( "some_pass" ) , opts. password. as_deref( ) ) ;
152
+ }
153
+
107
154
#[ test]
108
155
fn it_parses_application_name_correctly_from_parameter ( ) {
109
156
let uri = "postgres:///?application_name=some_name" ;
0 commit comments