use std::path::Path;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Format {
Env,
Ini,
Toml,
Json,
Yaml,
}
impl Format {
#[must_use]
pub fn from_path(path: &Path) -> Option<Self> {
let name = path.file_name()?.to_string_lossy();
if name == ".env" || name.starts_with(".env.") || name.ends_with(".env") {
return Some(Self::Env);
}
match path.extension()?.to_string_lossy().as_ref() {
"ini" => Some(Self::Ini),
"toml" => Some(Self::Toml),
"json" => Some(Self::Json),
"yml" | "yaml" => Some(Self::Yaml),
_ => None,
}
}
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::Env => "env",
Self::Ini => "ini",
Self::Toml => "toml",
Self::Json => "json",
Self::Yaml => "yaml",
}
}
#[must_use]
pub fn candidates(stem: &str) -> Vec<String> {
vec![
".env".to_string(),
format!("{stem}.ini"),
format!("{stem}.toml"),
format!("{stem}.json"),
format!("{stem}.yml"),
format!("{stem}.yaml"),
]
}
}
impl std::fmt::Display for Format {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}