File size: 1,362 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::{error::Error, str::FromStr};

use anyhow::{Context, Result, anyhow};

/// Reads an environment variable.
pub fn read_env<T>(name: &str, default: T) -> Result<T>
where
    T: FromStr,
    <T as FromStr>::Err: Error + Send + Sync + 'static,
{
    let config = std::env::var(name).ok();
    match config.as_deref() {
        None | Some("") => Ok(default),
        Some(config) => config
            .parse()
            .with_context(|| anyhow!("Invalid value for {}", name)),
    }
}

/// Reads a boolean-like environment variable, where any value but "0", "no",
/// or "false" is considered true.
pub fn read_env_bool(name: &str) -> bool {
    let config = std::env::var(name).ok();
    !matches!(
        config.as_deref(),
        None | Some("") | Some("0") | Some("no") | Some("false")
    )
}

/// Reads a comma-separated environment variable as a vector.
pub fn read_env_list<T>(name: &str, default: Vec<T>) -> Result<Vec<T>>
where
    T: FromStr,
    <T as FromStr>::Err: Error + Send + Sync + 'static,
{
    let config = std::env::var(name).ok();
    match config.as_deref() {
        None | Some("") => Ok(default),
        Some(config) => config
            .split(',')
            .map(|s| {
                s.parse()
                    .with_context(|| anyhow!("Invalid value for {}", name))
            })
            .collect(),
    }
}