From fb991e09e1b453b9eccc385f50d44dabbfb37cc6 Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Wed, 21 Feb 2024 16:04:47 +0530 Subject: [PATCH] Fixed ds.env_bool --- fastn-ds/src/lib.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/fastn-ds/src/lib.rs b/fastn-ds/src/lib.rs index fa2413203c..02b1cbc845 100644 --- a/fastn-ds/src/lib.rs +++ b/fastn-ds/src/lib.rs @@ -234,13 +234,13 @@ impl DocumentStore { path.path.exists() } - pub async fn env_bool(&self, key: &str, default: bool) -> bool { - self.env(key) - .await - .inspect(|v| tracing::info!("env_bool {key} = {v}")) - .map(|x| x == "true") - .inspect_err(|e| tracing::error!("env_bool error {e:?}")) - .unwrap_or(default) + pub async fn env_bool(&self, key: &str, default: bool) -> Result { + match self.env(key).await { + Ok(t) if t.eq("true") => Ok(true), + Ok(t) if t.eq("false") => Ok(false), + Ok(value) => Err(BoolEnvironmentError::InvalidValue(value.to_string())), + Err(EnvironmentError::NotSet(_)) => Ok(default), + } } pub async fn env(&self, key: &str) -> Result { @@ -248,6 +248,12 @@ impl DocumentStore { } } +#[derive(thiserror::Error, PartialEq, Debug)] +pub enum BoolEnvironmentError { + #[error("Invalid value found for boolean: {0}")] + InvalidValue(String), +} + #[derive(thiserror::Error, PartialEq, Debug)] pub enum EnvironmentError { /// The environment variable is not set.