use std::time::Duration;
use dynamic_config::dynamic_config;
use serde::Deserialize;
#[dynamic_config(files = ["dynamic-config/examples/limits.json"], key = "limits", env = "APP_")]
#[derive(Debug, Deserialize)]
struct Limits {
#[serde(with = "dynamic_config::duration")]
request_timeout: Duration,
#[serde(default, with = "dynamic_config::duration::option")]
shutdown_grace: Option<Duration>,
#[serde(with = "dynamic_config::bytes")]
max_body: u64,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let limits = Limits::load()?;
println!("request_timeout = {:?}", limits.request_timeout);
println!("shutdown_grace = {:?}", limits.shutdown_grace);
println!("max_body = {} bytes", limits.max_body);
println!("\nThe same values through the environment, since a variable is");
println!("text and these adapters read text:");
std::env::set_var("APP_LIMITS_REQUEST_TIMEOUT", "1h30m");
std::env::set_var("APP_LIMITS_MAX_BODY", "1GB");
let limits = Limits::load()?;
println!(" request_timeout = {:?}", limits.request_timeout);
println!(" max_body = {} bytes", limits.max_body);
println!("\nBinary and decimal units are not the same thing:");
println!(" 1KiB = {} bytes", dynamic_config::bytes::parse("1KiB")?);
println!(" 1KB = {} bytes", dynamic_config::bytes::parse("1KB")?);
println!("\nAnd nonsense is an error listing what was expected:");
match dynamic_config::duration::parse("30 fortnights") {
Ok(parsed) => println!(" unexpectedly parsed {parsed:?}"),
Err(error) => println!(" {error}"),
}
std::env::remove_var("APP_LIMITS_REQUEST_TIMEOUT");
std::env::remove_var("APP_LIMITS_MAX_BODY");
Ok(())
}