@@ -93,7 +93,7 @@ pub enum Host {
93
93
///
94
94
/// ## Keys
95
95
///
96
- /// * `user` - The username to authenticate with. Required .
96
+ /// * `user` - The username to authenticate with. Defaults to the user executing this process .
97
97
/// * `password` - The password to authenticate with.
98
98
/// * `dbname` - The name of the database to connect to. Defaults to the username.
99
99
/// * `options` - Command line options used to configure the server.
@@ -190,7 +190,7 @@ pub enum Host {
190
190
/// ```
191
191
#[ derive( Clone , PartialEq , Eq ) ]
192
192
pub struct Config {
193
- pub ( crate ) user : Option < String > ,
193
+ user : String ,
194
194
pub ( crate ) password : Option < Vec < u8 > > ,
195
195
pub ( crate ) dbname : Option < String > ,
196
196
pub ( crate ) options : Option < String > ,
@@ -219,7 +219,7 @@ impl Config {
219
219
/// Creates a new configuration.
220
220
pub fn new ( ) -> Config {
221
221
Config {
222
- user : None ,
222
+ user : whoami :: username ( ) ,
223
223
password : None ,
224
224
dbname : None ,
225
225
options : None ,
@@ -245,16 +245,17 @@ impl Config {
245
245
246
246
/// Sets the user to authenticate with.
247
247
///
248
- /// Required .
248
+ /// If the user is not set, then this defaults to the user executing this process .
249
249
pub fn user ( & mut self , user : & str ) -> & mut Config {
250
- self . user = Some ( user. to_string ( ) ) ;
250
+ self . user = user. to_string ( ) ;
251
251
self
252
252
}
253
253
254
- /// Gets the user to authenticate with, if one has been configured with
255
- /// the `user` method.
256
- pub fn get_user ( & self ) -> Option < & str > {
257
- self . user . as_deref ( )
254
+ /// Gets the user to authenticate with.
255
+ /// If no user has been configured with the [`user`](Config::user) method,
256
+ /// then this defaults to the user executing this process.
257
+ pub fn get_user ( & self ) -> & str {
258
+ & self . user
258
259
}
259
260
260
261
/// Sets the password to authenticate with.
@@ -1124,7 +1125,7 @@ mod tests {
1124
1125
fn test_simple_parsing ( ) {
1125
1126
let s = "user=pass_user dbname=postgres host=host1,host2 hostaddr=127.0.0.1,127.0.0.2 port=26257" ;
1126
1127
let config = s. parse :: < Config > ( ) . unwrap ( ) ;
1127
- assert_eq ! ( Some ( "pass_user" ) , config. get_user( ) ) ;
1128
+ assert_eq ! ( "pass_user" , config. get_user( ) ) ;
1128
1129
assert_eq ! ( Some ( "postgres" ) , config. get_dbname( ) ) ;
1129
1130
assert_eq ! (
1130
1131
[
0 commit comments