@@ -249,13 +249,23 @@ impl FullConfig {
249
249
Ok ( config)
250
250
}
251
251
252
+ /// Return the default configuration for all environments and marks the
253
+ /// active environment (from `CONFIG_ENV`) as active. Overrides the defaults
254
+ /// with values from the `ROCKET_{PARAM}` environment variables. Doesn't
255
+ /// read any other sources.
256
+ pub fn env_default ( ) -> Result < FullConfig > {
257
+ let mut config = Self :: active_default_with_path ( None ) ?;
258
+ config. override_from_env ( ) ?;
259
+ Ok ( config)
260
+ }
261
+
252
262
/// Return the default configuration for all environments and marks the
253
263
/// active environment (from `CONFIG_ENV`) as active. This doesn't read
254
264
/// `filename`, nor any other config values from any source; it simply uses
255
265
/// `filename` to set up the config path property in the returned `Config`.
256
- pub fn active_default < ' a , P : Into < Option < & ' a Path > > > ( filename : P ) -> Result < FullConfig > {
266
+ fn active_default_with_path ( path : Option < & Path > ) -> Result < FullConfig > {
257
267
let mut defaults = HashMap :: new ( ) ;
258
- if let Some ( path) = filename . into ( ) {
268
+ if let Some ( path) = path {
259
269
defaults. insert ( Development , Config :: default_from ( Development , & path) ?) ;
260
270
defaults. insert ( Staging , Config :: default_from ( Staging , & path) ?) ;
261
271
defaults. insert ( Production , Config :: default_from ( Production , & path) ?) ;
@@ -405,7 +415,7 @@ impl FullConfig {
405
415
} ;
406
416
407
417
// Create a config with the defaults; set the env to the active one.
408
- let mut config = FullConfig :: active_default ( filename. as_ref ( ) ) ?;
418
+ let mut config = FullConfig :: active_default_with_path ( Some ( filename. as_ref ( ) ) ) ?;
409
419
410
420
// Store all of the global overrides, if any, for later use.
411
421
let mut global = None ;
@@ -483,10 +493,8 @@ mod test {
483
493
) ;
484
494
}
485
495
486
- fn active_default ( ) -> Result < FullConfig > {
487
- let mut config = FullConfig :: active_default ( None ) ?;
488
- config. override_from_env ( ) ?;
489
- Ok ( config)
496
+ fn env_default ( ) -> Result < FullConfig > {
497
+ FullConfig :: env_default ( )
490
498
}
491
499
492
500
fn default_config ( env : Environment ) -> ConfigBuilder {
@@ -501,25 +509,25 @@ mod test {
501
509
// First, without an environment. Should get development defaults on
502
510
// debug builds and productions defaults on non-debug builds.
503
511
env:: remove_var ( CONFIG_ENV ) ;
504
- #[ cfg( debug_assertions) ] check_config ! ( active_default ( ) , default_config( Development ) ) ;
505
- #[ cfg( not( debug_assertions) ) ] check_config ! ( active_default ( ) , default_config( Production ) ) ;
512
+ #[ cfg( debug_assertions) ] check_config ! ( env_default ( ) , default_config( Development ) ) ;
513
+ #[ cfg( not( debug_assertions) ) ] check_config ! ( env_default ( ) , default_config( Production ) ) ;
506
514
507
515
// Now with an explicit dev environment.
508
516
for env in & [ "development" , "dev" ] {
509
517
env:: set_var ( CONFIG_ENV , env) ;
510
- check_config ! ( active_default ( ) , default_config( Development ) ) ;
518
+ check_config ! ( env_default ( ) , default_config( Development ) ) ;
511
519
}
512
520
513
521
// Now staging.
514
522
for env in & [ "stage" , "staging" ] {
515
523
env:: set_var ( CONFIG_ENV , env) ;
516
- check_config ! ( active_default ( ) , default_config( Staging ) ) ;
524
+ check_config ! ( env_default ( ) , default_config( Staging ) ) ;
517
525
}
518
526
519
527
// Finally, production.
520
528
for env in & [ "prod" , "production" ] {
521
529
env:: set_var ( CONFIG_ENV , env) ;
522
- check_config ! ( active_default ( ) , default_config( Production ) ) ;
530
+ check_config ! ( env_default ( ) , default_config( Production ) ) ;
523
531
}
524
532
}
525
533
@@ -531,7 +539,7 @@ mod test {
531
539
for env in & [ "" , "p" , "pr" , "pro" , "prodo" , " prod" , "dev " , "!dev!" , "🚀 " ] {
532
540
env:: set_var ( CONFIG_ENV , env) ;
533
541
let err = ConfigError :: BadEnv ( env. to_string ( ) ) ;
534
- assert ! ( active_default ( ) . err( ) . map_or( false , |e| e == err) ) ;
542
+ assert ! ( env_default ( ) . err( ) . map_or( false , |e| e == err) ) ;
535
543
}
536
544
537
545
// Test that a bunch of invalid environment names give the right error.
@@ -1094,12 +1102,12 @@ mod test {
1094
1102
// Check that it overrides the active config.
1095
1103
for env in & Environment :: ALL {
1096
1104
env:: set_var ( CONFIG_ENV , env. to_string ( ) ) ;
1097
- let rconfig = active_default ( ) . unwrap ( ) ;
1105
+ let rconfig = env_default ( ) . unwrap ( ) ;
1098
1106
check_value ! ( & * key. to_lowercase( ) , val, rconfig. active( ) ) ;
1099
1107
}
1100
1108
1101
1109
// And non-active configs.
1102
- let rconfig = active_default ( ) . unwrap ( ) ;
1110
+ let rconfig = env_default ( ) . unwrap ( ) ;
1103
1111
for env in & Environment :: ALL {
1104
1112
check_value ! ( & * key. to_lowercase( ) , val, rconfig. get( * env) ) ;
1105
1113
}
0 commit comments