diff --git a/README.md b/README.md index 534ddb4..71a1ad5 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ cargo test * [x] Add namespace for variables * [x] Custom env name * [x] Support feature config and other meta directives -* [ ] Add default value to env if env is not found +* [x] Add default value to env if env is not found * [ ] Concat env variables to one variable diff --git a/examples/rocket/src/main.rs b/examples/rocket/src/main.rs index 684d5cb..7770874 100644 --- a/examples/rocket/src/main.rs +++ b/examples/rocket/src/main.rs @@ -10,7 +10,7 @@ use rocket::config::{Config, Environment}; config! { ROCKET { HOST: String => "localhost".to_string(), - PORT: u16 => 8000, + PORT: u16 => 9000, BASE_URL: String => "/".to_string(), } } @@ -24,13 +24,7 @@ fn index() -> &'static str { fn main() { cfg::init(); - let config = Config::build(Environment::Staging) - .address(cfg::ROCKET::HOST()) - .port(cfg::ROCKET::PORT()) - .finalize() - .unwrap(); - - rocket::custom(config) + rocket::ignite() .mount(&cfg::ROCKET::BASE_URL(), routes![index]) .launch(); } \ No newline at end of file diff --git a/itconfig/Cargo.toml b/itconfig/Cargo.toml index 8c4176c..68cf56e 100644 --- a/itconfig/Cargo.toml +++ b/itconfig/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "itconfig" -version = "0.5.2" +version = "0.6.0" authors = ["Dmitriy Pleshevskiy "] description = "Easy build a configs from environment variables and use it in globally." categories = ["config", "web-programming"] diff --git a/itconfig/README.md b/itconfig/README.md index b77ac4e..a7c6cc7 100644 --- a/itconfig/README.md +++ b/itconfig/README.md @@ -44,7 +44,7 @@ fn main () { * [x] Add namespace for variables * [x] Custom env name * [x] Support feature config and other meta directives -* [ ] Add default value to env if env is not found +* [x] Add default value to env if env is not found * [ ] Concat env variables to one variable diff --git a/itconfig/src/lib.rs b/itconfig/src/lib.rs index 9f83715..7825335 100644 --- a/itconfig/src/lib.rs +++ b/itconfig/src/lib.rs @@ -557,15 +557,28 @@ macro_rules! __itconfig_variable { #[macro_export(local_inner_macro)] macro_rules! env_or { ($env_name:expr) => { - env_or!($env_name, panic!(format!(r#"Cannot read "{}" environment variable"#, $env_name))); + env_or!($env_name, format!(r#"Cannot read "{}" environment variable"#, $env_name), panic); }; - ($env_name:expr, $default:expr) => {{ + ($env_name:expr, $default:expr) => { + env_or!($env_name, $default, default); + }; + + ($env_name:expr, $default:expr, $token:tt) => {{ use std::env; use itconfig::EnvValue; env::var($env_name) .map(|val| EnvValue::from(val).into()) - .unwrap_or_else(|_| $default) + .unwrap_or_else(|_| env_or!(@$token $env_name, $default)) }}; + + (@default $env_name:expr, $default:expr) => {{ + env::set_var($env_name, $default.to_string()); + $default + }}; + + (@panic $env_name:expr, $default:expr) => { + panic!($default); + }; } diff --git a/itconfig_tests/tests/config_macro.rs b/itconfig_tests/tests/config_macro.rs index c3881a0..00c16df 100644 --- a/itconfig_tests/tests/config_macro.rs +++ b/itconfig_tests/tests/config_macro.rs @@ -240,4 +240,22 @@ fn stranger_meta_data() { #[cfg(feature = "postgres")] assert_eq!(cfg::DATABASE_URL(), "95"); env::remove_var("MY_CUSTOM_NAME"); -} \ No newline at end of file +} + +#[test] +fn setting_default_env_variable() { + config! { + DEFAULT_ENV_STRING: String => "localhost".to_string(), + DEFAULT_ENV_BOOLEAN: bool => true, + DEFAULT_ENV_UINT: u32 => 40, + DEFAULT_ENV_FLOAT: f64 => 40.9, + } + + cfg::init(); + + assert_eq!(env::var("DEFAULT_ENV_STRING"), Ok("localhost".to_string())); + assert_eq!(env::var("DEFAULT_ENV_BOOLEAN"), Ok("true".to_string())); + assert_eq!(env::var("DEFAULT_ENV_UINT"), Ok("40".to_string())); + assert_eq!(env::var("DEFAULT_ENV_FLOAT"), Ok("40.9".to_string())); +} +