Skip to content

Commit

Permalink
accept true/yes/1 for bool options
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobhellermann committed Nov 21, 2023
1 parent 2f3f277 commit 9ac6629
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,28 @@ pub type Result<T, E = anyhow::Error> = std::result::Result<T, E>;
mod server;
mod wasm_bindgen;

fn bool_option(name: &str, default: bool) -> Result<bool, anyhow::Error> {
match std::env::var(name) {
Ok(value) if ["true", "1"].contains(&value.as_str()) => Ok(true),
Ok(value) if ["false", "0"].contains(&value.as_str()) => Ok(false),
Ok(value) => Err(anyhow!("unexpected option {name}={value}, expected true,1 or false,0")),
Err(_) => Ok(default),
}
}
fn option(name: &str, default: &str) -> String {
std::env::var(name).unwrap_or(default.to_owned())
}

fn main() -> Result<(), anyhow::Error> {
let filter = EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("info,tower_http=debug,walrus=error"));
tracing_subscriber::fmt::fmt().without_time().with_env_filter(filter).init();

let title = std::env::var("CARGO_PKG_NAME").unwrap_or_else(|_| "".to_string());
let address =
std::env::var("WASM_SERVER_RUNNER_ADDRESS").unwrap_or_else(|_| "127.0.0.1".to_string());
let directory =
std::env::var("WASM_SERVER_RUNNER_DIRECTORY").unwrap_or_else(|_| String::from("."));
let https =
std::env::var("WASM_SERVER_RUNNER_HTTPS").unwrap_or_else(|_| String::from("0")) == "1";
let no_module =
std::env::var("WASM_SERVER_RUNNER_NO_MODULE").unwrap_or_else(|_| String::from("0")) == "1";
let address = option("WASM_SERVER_RUNNER_ADDRESS", "127.0.0.1");
let directory = option("WASM_SERVER_RUNNER_DIRECTORY", ".");
let https = bool_option("WASM_SERVER_RUNNER_HTTPS", false)?;
let no_module = bool_option("WASM_SERVER_RUNNER_NO_MODULE", false)?;

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

Expand Down

0 comments on commit 9ac6629

Please sign in to comment.