@@ -93,7 +93,7 @@ pub enum Host {
9393///
9494/// ## Keys
9595///
96- /// * `user` - The username to authenticate with. Required .
96+ /// * `user` - The username to authenticate with. Defaults to the user executing this process .
9797/// * `password` - The password to authenticate with.
9898/// * `dbname` - The name of the database to connect to. Defaults to the username.
9999/// * `options` - Command line options used to configure the server.
@@ -190,7 +190,7 @@ pub enum Host {
190190/// ```
191191#[ derive( Clone , PartialEq , Eq ) ]
192192pub struct Config {
193- pub ( crate ) user : Option < String > ,
193+ user : String ,
194194 pub ( crate ) password : Option < Vec < u8 > > ,
195195 pub ( crate ) dbname : Option < String > ,
196196 pub ( crate ) options : Option < String > ,
@@ -219,7 +219,7 @@ impl Config {
219219 /// Creates a new configuration.
220220 pub fn new ( ) -> Config {
221221 Config {
222- user : None ,
222+ user : whoami :: username ( ) ,
223223 password : None ,
224224 dbname : None ,
225225 options : None ,
@@ -245,16 +245,17 @@ impl Config {
245245
246246 /// Sets the user to authenticate with.
247247 ///
248- /// Required .
248+ /// If the user is not set, then this defaults to the user executing this process .
249249 pub fn user ( & mut self , user : & str ) -> & mut Config {
250- self . user = Some ( user. to_string ( ) ) ;
250+ self . user = user. to_string ( ) ;
251251 self
252252 }
253253
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
258259 }
259260
260261 /// Sets the password to authenticate with.
@@ -1124,7 +1125,7 @@ mod tests {
11241125 fn test_simple_parsing ( ) {
11251126 let s = "user=pass_user dbname=postgres host=host1,host2 hostaddr=127.0.0.1,127.0.0.2 port=26257" ;
11261127 let config = s. parse :: < Config > ( ) . unwrap ( ) ;
1127- assert_eq ! ( Some ( "pass_user" ) , config. get_user( ) ) ;
1128+ assert_eq ! ( "pass_user" , config. get_user( ) ) ;
11281129 assert_eq ! ( Some ( "postgres" ) , config. get_dbname( ) ) ;
11291130 assert_eq ! (
11301131 [
0 commit comments