use env_cfg::{EnvConfig, EnvConfigError};
mod common;
#[derive(Debug, EnvConfig, PartialEq)]
#[env_cfg(no_prefix)]
struct DatabaseConfig {
host: String, port: u16, #[env_cfg(default = "myapp")]
database: String, }
#[derive(Debug, EnvConfig, PartialEq)]
#[env_cfg(no_prefix)]
struct RedisConfig {
#[env_cfg(env = "REDIS_URL")]
url: String, #[env_cfg(env = "REDIS_TIMEOUT", default = "5")]
timeout: u64, }
#[derive(Debug, EnvConfig, PartialEq)]
#[env_cfg(no_prefix)]
struct AppConfig {
#[env_cfg(nested)]
database: DatabaseConfig,
#[env_cfg(default = "info")]
log_level: String, }
#[derive(Debug, EnvConfig, PartialEq)]
#[env_cfg(no_prefix)]
struct MultiNestedConfig {
#[env_cfg(nested)]
database: DatabaseConfig,
#[env_cfg(nested)]
redis: RedisConfig,
app_name: String, }
#[test]
fn should_parse_nested_config() {
const ENV_VARS: &[(&str, &str)] = &[
("HOST", "localhost"),
("PORT", "5432"),
("DATABASE", "testdb"),
("LOG_LEVEL", "debug"),
];
let config = unsafe { common::with_env_vars(ENV_VARS, || AppConfig::from_env().unwrap()) };
assert_eq!(config.database.host, "localhost");
assert_eq!(config.database.port, 5432);
assert_eq!(config.database.database, "testdb");
assert_eq!(config.log_level, "debug");
}
#[test]
fn should_parse_nested_config_with_defaults() {
const ENV_VARS: &[(&str, &str)] = &[
("HOST", "localhost"),
("PORT", "5432"),
];
let config = unsafe { common::with_env_vars(ENV_VARS, || AppConfig::from_env().unwrap()) };
assert_eq!(config.database.host, "localhost");
assert_eq!(config.database.port, 5432);
assert_eq!(config.database.database, "myapp"); assert_eq!(config.log_level, "info"); }
#[test]
fn should_parse_multiple_nested_configs() {
const ENV_VARS: &[(&str, &str)] = &[
("HOST", "db.example.com"),
("PORT", "5432"),
("DATABASE", "prod"),
("REDIS_URL", "redis://localhost:6379"),
("REDIS_TIMEOUT", "10"),
("APP_NAME", "my-app"),
];
let config =
unsafe { common::with_env_vars(ENV_VARS, || MultiNestedConfig::from_env().unwrap()) };
assert_eq!(config.database.host, "db.example.com");
assert_eq!(config.database.port, 5432);
assert_eq!(config.database.database, "prod");
assert_eq!(config.redis.url, "redis://localhost:6379");
assert_eq!(config.redis.timeout, 10);
assert_eq!(config.app_name, "my-app");
}
#[test]
fn should_propagate_nested_config_errors() {
const ENV_VARS: &[(&str, &str)] = &[
("HOST", "localhost"),
("PORT", "not_a_number"), ("LOG_LEVEL", "debug"),
];
let result = unsafe { common::with_env_vars(ENV_VARS, AppConfig::from_env) };
if let Err(EnvConfigError::Parse(var, _)) = result {
assert!(var.contains("nested DatabaseConfig"));
} else {
panic!("Expected Parse error with nested context");
}
}
#[test]
fn should_fail_when_nested_required_vars_missing() {
const ENV_VARS: &[(&str, &str)] = &[
("PORT", "5432"),
("LOG_LEVEL", "debug"),
];
let result = unsafe { common::with_env_vars(ENV_VARS, AppConfig::from_env) };
if let Err(EnvConfigError::Parse(var, _)) = result {
assert!(var.contains("nested DatabaseConfig"));
} else {
panic!("Expected Parse error with nested context");
}
}
#[test]
fn should_parse_multiple_nested_with_defaults() {
const ENV_VARS: &[(&str, &str)] = &[
("HOST", "localhost"),
("PORT", "5432"),
("REDIS_URL", "redis://localhost:6379"),
("APP_NAME", "test-app"),
];
let config =
unsafe { common::with_env_vars(ENV_VARS, || MultiNestedConfig::from_env().unwrap()) };
assert_eq!(config.database.host, "localhost");
assert_eq!(config.database.port, 5432);
assert_eq!(config.database.database, "myapp"); assert_eq!(config.redis.url, "redis://localhost:6379");
assert_eq!(config.redis.timeout, 5); assert_eq!(config.app_name, "test-app");
}
#[test]
fn test_nested_with_parse_with_should_not_compile() {
}
#[test]
fn test_nested_with_default_should_not_compile() {
}