Skip to content

Commit 0ce4cba

Browse files
accept true/yes/1 for bool options
1 parent 2f3f277 commit 0ce4cba

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/main.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,28 @@ pub type Result<T, E = anyhow::Error> = std::result::Result<T, E>;
1111
mod server;
1212
mod wasm_bindgen;
1313

14+
fn bool_option(name: &str, default: bool) -> Result<bool, anyhow::Error> {
15+
match std::env::var(name) {
16+
Ok(value) if ["true", "1", "yes"].contains(&value.as_str()) => Ok(true),
17+
Ok(value) if ["false", "0", "no"].contains(&value.as_str()) => Ok(false),
18+
Ok(value) => Err(anyhow!("unexpected option {name}={value}, expected true,1 or false,0")),
19+
Err(_) => Ok(default),
20+
}
21+
}
22+
fn option(name: &str, default: &str) -> String {
23+
std::env::var(name).unwrap_or(default.to_owned())
24+
}
25+
1426
fn main() -> Result<(), anyhow::Error> {
1527
let filter = EnvFilter::try_from_default_env()
1628
.unwrap_or_else(|_| EnvFilter::new("info,tower_http=debug,walrus=error"));
1729
tracing_subscriber::fmt::fmt().without_time().with_env_filter(filter).init();
1830

1931
let title = std::env::var("CARGO_PKG_NAME").unwrap_or_else(|_| "".to_string());
20-
let address =
21-
std::env::var("WASM_SERVER_RUNNER_ADDRESS").unwrap_or_else(|_| "127.0.0.1".to_string());
22-
let directory =
23-
std::env::var("WASM_SERVER_RUNNER_DIRECTORY").unwrap_or_else(|_| String::from("."));
24-
let https =
25-
std::env::var("WASM_SERVER_RUNNER_HTTPS").unwrap_or_else(|_| String::from("0")) == "1";
26-
let no_module =
27-
std::env::var("WASM_SERVER_RUNNER_NO_MODULE").unwrap_or_else(|_| String::from("0")) == "1";
32+
let address = option("WASM_SERVER_RUNNER_ADDRESS", "127.0.0.1");
33+
let directory = option("WASM_SERVER_RUNNER_DIRECTORY", ".");
34+
let https = bool_option("WASM_SERVER_RUNNER_HTTPS", false)?;
35+
let no_module = bool_option("WASM_SERVER_RUNNER_NO_MODULE", false)?;
2836

2937
let options = Options { title, address, directory, https, no_module };
3038

0 commit comments

Comments
 (0)