sqlpage 0.45.0

Build data user interfaces entirely in SQL. A web server that takes .sql files and formats the query result using pre-made configurable professional-looking components.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use std::borrow::Cow;

use anyhow::Context;

/// Returns the value of an environment variable.
pub(super) async fn environment_variable(name: Cow<'_, str>) -> anyhow::Result<Option<Cow<'_, str>>> {
    match std::env::var(&*name) {
        Ok(value) => Ok(Some(Cow::Owned(value))),
        Err(std::env::VarError::NotPresent) if name.contains(['=', '\0']) => anyhow::bail!(
            "Invalid environment variable name: {name:?}. Environment variable names cannot contain an equals sign or a null character."
        ),
        Err(std::env::VarError::NotPresent) => Ok(None),
        Err(err) => {
            Err(err).with_context(|| format!("unable to read the environment variable {name:?}"))
        }
    }
}