@@ -5,12 +5,7 @@ use std::io::{BufRead, BufReader};
55use std:: path:: PathBuf ;
66
77/// try to load a password from the various pgpass file locations
8- pub fn load_password (
9- host : & str ,
10- port : u16 ,
11- username : & str ,
12- database : Option < & str > ,
13- ) -> Option < String > {
8+ pub fn load_password ( host : & str , port : u16 , username : & str , database : & str ) -> Option < String > {
149 let custom_file = var_os ( "PGPASSFILE" ) ;
1510 if let Some ( file) = custom_file {
1611 if let Some ( password) =
@@ -39,7 +34,7 @@ fn load_password_from_file(
3934 host : & str ,
4035 port : u16 ,
4136 username : & str ,
42- database : Option < & str > ,
37+ database : & str ,
4338) -> Option < String > {
4439 let file = File :: open ( & path)
4540 . map_err ( |e| {
@@ -88,7 +83,7 @@ fn load_password_from_reader(
8883 host : & str ,
8984 port : u16 ,
9085 username : & str ,
91- database : Option < & str > ,
86+ database : & str ,
9287) -> Option < String > {
9388 let mut line = String :: new ( ) ;
9489
@@ -129,7 +124,7 @@ fn load_password_from_line(
129124 host : & str ,
130125 port : u16 ,
131126 username : & str ,
132- database : Option < & str > ,
127+ database : & str ,
133128) -> Option < String > {
134129 let whole_line = line;
135130
@@ -140,7 +135,7 @@ fn load_password_from_line(
140135 _ => {
141136 matches_next_field ( whole_line, & mut line, host) ?;
142137 matches_next_field ( whole_line, & mut line, & port. to_string ( ) ) ?;
143- matches_next_field ( whole_line, & mut line, database. unwrap_or_default ( ) ) ?;
138+ matches_next_field ( whole_line, & mut line, database) ?;
144139 matches_next_field ( whole_line, & mut line, username) ?;
145140 Some ( line. to_owned ( ) )
146141 }
@@ -268,41 +263,24 @@ mod tests {
268263 "localhost" ,
269264 5432 ,
270265 "foo" ,
271- Some ( "bar" )
266+ "bar" ,
272267 ) ,
273268 Some ( "baz" . to_owned( ) )
274269 ) ;
275270 // wildcard
276271 assert_eq ! (
277- load_password_from_line( "*:5432:bar:foo:baz" , "localhost" , 5432 , "foo" , Some ( "bar" ) ) ,
278- Some ( "baz" . to_owned( ) )
279- ) ;
280- // accept wildcard with missing db
281- assert_eq ! (
282- load_password_from_line( "localhost:5432:*:foo:baz" , "localhost" , 5432 , "foo" , None ) ,
272+ load_password_from_line( "*:5432:bar:foo:baz" , "localhost" , 5432 , "foo" , "bar" ) ,
283273 Some ( "baz" . to_owned( ) )
284274 ) ;
285275
286276 // doesn't match
287277 assert_eq ! (
288- load_password_from_line(
289- "thishost:5432:bar:foo:baz" ,
290- "thathost" ,
291- 5432 ,
292- "foo" ,
293- Some ( "bar" )
294- ) ,
278+ load_password_from_line( "thishost:5432:bar:foo:baz" , "thathost" , 5432 , "foo" , "bar" , ) ,
295279 None
296280 ) ;
297281 // malformed entry
298282 assert_eq ! (
299- load_password_from_line(
300- "localhost:5432:bar:foo" ,
301- "localhost" ,
302- 5432 ,
303- "foo" ,
304- Some ( "bar" )
305- ) ,
283+ load_password_from_line( "localhost:5432:bar:foo" , "localhost" , 5432 , "foo" , "bar" , ) ,
306284 None
307285 ) ;
308286 }
@@ -323,28 +301,23 @@ mod tests {
323301
324302 // normal
325303 assert_eq ! (
326- load_password_from_reader( & mut & file[ ..] , "localhost" , 5432 , "foo" , Some ( "bar" ) ) ,
304+ load_password_from_reader( & mut & file[ ..] , "localhost" , 5432 , "foo" , "bar" ) ,
327305 Some ( "baz" . to_owned( ) )
328306 ) ;
329307 // wildcard
330308 assert_eq ! (
331- load_password_from_reader( & mut & file[ ..] , "localhost" , 5432 , "foo" , Some ( "foobar" ) ) ,
332- Some ( "baz" . to_owned( ) )
333- ) ;
334- // accept wildcard with missing db
335- assert_eq ! (
336- load_password_from_reader( & mut & file[ ..] , "localhost" , 5432 , "foo" , None ) ,
309+ load_password_from_reader( & mut & file[ ..] , "localhost" , 5432 , "foo" , "foobar" ) ,
337310 Some ( "baz" . to_owned( ) )
338311 ) ;
339312
340313 // doesn't match
341314 assert_eq ! (
342- load_password_from_reader( & mut & file[ ..] , "thathost" , 5432 , "foo" , Some ( "foobar" ) ) ,
315+ load_password_from_reader( & mut & file[ ..] , "thathost" , 5432 , "foo" , "foobar" ) ,
343316 None
344317 ) ;
345318 // malformed entry
346319 assert_eq ! (
347- load_password_from_reader( & mut & file[ ..] , "thathost" , 5432 , "foo" , Some ( "foobar" ) ) ,
320+ load_password_from_reader( & mut & file[ ..] , "thathost" , 5432 , "foo" , "foobar" ) ,
348321 None
349322 ) ;
350323 }
0 commit comments