feat: add setting value to env as default

This commit is contained in:
Dmitriy Pleshevskiy 2020-01-06 23:12:06 +03:00
parent 57598a876d
commit 3b95ed6f3e
6 changed files with 40 additions and 15 deletions

View file

@ -54,7 +54,7 @@ cargo test
* [x] Add namespace for variables * [x] Add namespace for variables
* [x] Custom env name * [x] Custom env name
* [x] Support feature config and other meta directives * [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 * [ ] Concat env variables to one variable

View file

@ -10,7 +10,7 @@ use rocket::config::{Config, Environment};
config! { config! {
ROCKET { ROCKET {
HOST: String => "localhost".to_string(), HOST: String => "localhost".to_string(),
PORT: u16 => 8000, PORT: u16 => 9000,
BASE_URL: String => "/".to_string(), BASE_URL: String => "/".to_string(),
} }
} }
@ -24,13 +24,7 @@ fn index() -> &'static str {
fn main() { fn main() {
cfg::init(); cfg::init();
let config = Config::build(Environment::Staging) rocket::ignite()
.address(cfg::ROCKET::HOST())
.port(cfg::ROCKET::PORT())
.finalize()
.unwrap();
rocket::custom(config)
.mount(&cfg::ROCKET::BASE_URL(), routes![index]) .mount(&cfg::ROCKET::BASE_URL(), routes![index])
.launch(); .launch();
} }

View file

@ -1,6 +1,6 @@
[package] [package]
name = "itconfig" name = "itconfig"
version = "0.5.2" version = "0.6.0"
authors = ["Dmitriy Pleshevskiy <dmitriy@ideascup.me>"] authors = ["Dmitriy Pleshevskiy <dmitriy@ideascup.me>"]
description = "Easy build a configs from environment variables and use it in globally." description = "Easy build a configs from environment variables and use it in globally."
categories = ["config", "web-programming"] categories = ["config", "web-programming"]

View file

@ -44,7 +44,7 @@ fn main () {
* [x] Add namespace for variables * [x] Add namespace for variables
* [x] Custom env name * [x] Custom env name
* [x] Support feature config and other meta directives * [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 * [ ] Concat env variables to one variable

View file

@ -557,15 +557,28 @@ macro_rules! __itconfig_variable {
#[macro_export(local_inner_macro)] #[macro_export(local_inner_macro)]
macro_rules! env_or { macro_rules! env_or {
($env_name:expr) => { ($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 std::env;
use itconfig::EnvValue; use itconfig::EnvValue;
env::var($env_name) env::var($env_name)
.map(|val| EnvValue::from(val).into()) .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);
};
} }

View file

@ -240,4 +240,22 @@ fn stranger_meta_data() {
#[cfg(feature = "postgres")] #[cfg(feature = "postgres")]
assert_eq!(cfg::DATABASE_URL(), "95"); assert_eq!(cfg::DATABASE_URL(), "95");
env::remove_var("MY_CUSTOM_NAME"); env::remove_var("MY_CUSTOM_NAME");
} }
#[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()));
}