use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Scope {
User,
Global,
Portable,
}
impl fmt::Display for Scope {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::User => "user",
Self::Global => "global",
Self::Portable => "portable",
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Kind {
Data,
Config,
Cache,
Logs,
State,
Runtime,
Temp,
Documents,
}
impl Kind {
pub const ALL: [Self; 8] = [
Self::Data,
Self::Config,
Self::Cache,
Self::Logs,
Self::State,
Self::Runtime,
Self::Temp,
Self::Documents,
];
#[must_use]
pub const fn env_name(self) -> &'static str {
match self {
Self::Data => "DATA",
Self::Config => "CONFIG",
Self::Cache => "CACHE",
Self::Logs => "LOGS",
Self::State => "STATE",
Self::Runtime => "RUNTIME",
Self::Temp => "TEMP",
Self::Documents => "DOCUMENTS",
}
}
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Data => "data",
Self::Config => "config",
Self::Cache => "cache",
Self::Logs => "logs",
Self::State => "state",
Self::Runtime => "runtime",
Self::Temp => "temp",
Self::Documents => "documents",
}
}
}
impl fmt::Display for Kind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_kind_all_is_complete_and_named() {
assert_eq!(Kind::ALL.len(), 8);
for k in Kind::ALL {
assert_eq!(k.to_string(), k.name());
}
}
#[test]
fn test_env_names_are_unique_uppercase() {
let names: Vec<_> = Kind::ALL.iter().map(|k| k.env_name()).collect();
for (i, n) in names.iter().enumerate() {
assert_eq!(n.to_ascii_uppercase(), *n);
assert!(!names[i + 1..].contains(n));
}
}
#[test]
fn test_scope_display_names() {
assert_eq!(Scope::User.to_string(), "user");
assert_eq!(Scope::Global.to_string(), "global");
assert_eq!(Scope::Portable.to_string(), "portable");
}
}