@@ -5,12 +5,7 @@ use std::io::{BufRead, BufReader};
5
5
use std:: path:: PathBuf ;
6
6
7
7
/// 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 > {
14
9
let custom_file = var_os ( "PGPASSFILE" ) ;
15
10
if let Some ( file) = custom_file {
16
11
if let Some ( password) =
@@ -39,7 +34,7 @@ fn load_password_from_file(
39
34
host : & str ,
40
35
port : u16 ,
41
36
username : & str ,
42
- database : Option < & str > ,
37
+ database : & str ,
43
38
) -> Option < String > {
44
39
let file = File :: open ( & path)
45
40
. map_err ( |e| {
@@ -88,7 +83,7 @@ fn load_password_from_reader(
88
83
host : & str ,
89
84
port : u16 ,
90
85
username : & str ,
91
- database : Option < & str > ,
86
+ database : & str ,
92
87
) -> Option < String > {
93
88
let mut line = String :: new ( ) ;
94
89
@@ -129,7 +124,7 @@ fn load_password_from_line(
129
124
host : & str ,
130
125
port : u16 ,
131
126
username : & str ,
132
- database : Option < & str > ,
127
+ database : & str ,
133
128
) -> Option < String > {
134
129
let whole_line = line;
135
130
@@ -140,7 +135,7 @@ fn load_password_from_line(
140
135
_ => {
141
136
matches_next_field ( whole_line, & mut line, host) ?;
142
137
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) ?;
144
139
matches_next_field ( whole_line, & mut line, username) ?;
145
140
Some ( line. to_owned ( ) )
146
141
}
@@ -268,41 +263,24 @@ mod tests {
268
263
"localhost" ,
269
264
5432 ,
270
265
"foo" ,
271
- Some ( "bar" )
266
+ "bar" ,
272
267
) ,
273
268
Some ( "baz" . to_owned( ) )
274
269
) ;
275
270
// wildcard
276
271
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" ) ,
283
273
Some ( "baz" . to_owned( ) )
284
274
) ;
285
275
286
276
// doesn't match
287
277
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" , ) ,
295
279
None
296
280
) ;
297
281
// malformed entry
298
282
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" , ) ,
306
284
None
307
285
) ;
308
286
}
@@ -323,28 +301,23 @@ mod tests {
323
301
324
302
// normal
325
303
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" ) ,
327
305
Some ( "baz" . to_owned( ) )
328
306
) ;
329
307
// wildcard
330
308
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" ) ,
337
310
Some ( "baz" . to_owned( ) )
338
311
) ;
339
312
340
313
// doesn't match
341
314
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" ) ,
343
316
None
344
317
) ;
345
318
// malformed entry
346
319
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" ) ,
348
321
None
349
322
) ;
350
323
}
0 commit comments