Skip to content

Commit

Permalink
Fixed ds.env_bool
Browse files Browse the repository at this point in the history
  • Loading branch information
Arpita-Jaiswal committed Feb 21, 2024
1 parent badbcc2 commit fb991e0
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions fastn-ds/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,20 +234,26 @@ 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<bool, BoolEnvironmentError> {
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<String, EnvironmentError> {
std::env::var(key).map_err(|_| EnvironmentError::NotSet(key.to_string()))
}
}

#[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.
Expand Down

0 comments on commit fb991e0

Please sign in to comment.