use std::fmt;
use std::str::FromStr;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub enum Profile {
#[default]
Local,
Dev,
Test,
Staging,
Prod,
}
impl Profile {
pub fn detect() -> Self {
std::env::var("APP_PROFILE")
.or_else(|_| std::env::var("KLAUTHED_PROFILE"))
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or_default()
}
pub fn requires_vault(&self) -> bool {
matches!(self, Profile::Staging | Profile::Prod)
}
pub fn allows_file_secrets(&self) -> bool {
!self.requires_vault()
}
pub fn as_str(&self) -> &'static str {
match self {
Profile::Local => "local",
Profile::Dev => "dev",
Profile::Test => "test",
Profile::Staging => "staging",
Profile::Prod => "prod",
}
}
}
impl fmt::Display for Profile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for Profile {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"local" => Ok(Profile::Local),
"dev" => Ok(Profile::Dev),
"test" => Ok(Profile::Test),
"staging" => Ok(Profile::Staging),
"prod" | "production" => Ok(Profile::Prod),
_ => Err(()),
}
}
}