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 super::Text;
use psl::StringFromEnvVar;
use std::fmt;
/// A value that can optionally be fetched from an environment
/// variable.
#[derive(Debug, Clone, Copy)]
pub enum Env<'a> {
/// Represents `env("VAR")`, where `var` is the tuple value. The
/// value is fetched from an env var of the same name.
FromVar(Text<&'a str>),
/// Value directly written to the file, not using an env var.
Value(Text<&'a str>),
}
impl<'a> Env<'a> {
/// Represents `env("VAR")`, where `var` is the tuple value. The
/// value is fetched from an env var of the same name.
pub fn variable(var: &'a str) -> Self {
Self::FromVar(Text(var))
}
/// Value directly written to the file, not using an env var.
pub fn value(val: &'a str) -> Self {
Self::Value(Text(val))
}
}
impl<'a> From<&'a StringFromEnvVar> for Env<'a> {
fn from(other: &'a StringFromEnvVar) -> Self {
match (other.as_env_var(), other.as_literal()) {
(Some(var), _) => Self::variable(var),
(_, Some(val)) => Self::value(val),
_ => unreachable!(),
}
}
}
impl<'a> fmt::Display for Env<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Env::FromVar(var) => {
write!(f, "env({var})")
}
Env::Value(val) => val.fmt(f),
}
}
}