cfgmatic-paths 2.2.0

Cross-platform configuration path discovery following XDG and platform conventions
Documentation
//! Configuration tier definitions.

/// Configuration tier represents the priority level of a configuration source.
///
/// Tiers are ordered by priority: User > Local > System.
/// When merging configurations, User tier overrides Local,
/// and Local overrides System.
///
/// The enum's discriminant values directly represent priority levels,
/// allowing use via `u8::from(tier)` or `tier as u8`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum ConfigTier {
    /// System-wide configuration.
    /// Lowest priority, typically in `/etc/`.
    /// Examples: `/etc/app/`, `/etc/xdg/app/`
    System = 1,

    /// Local/machine-specific configuration.
    /// Medium priority, typically in `/usr/local/etc` or similar.
    /// Examples: `/usr/local/etc/app/`, `/opt/app/etc/`
    Local = 2,

    /// User-specific configuration.
    /// Highest priority, typically in user's home directory.
    /// Examples: `~/.config/app/`, `~/.apprc`
    #[default]
    User = 3,
}

impl From<ConfigTier> for u8 {
    fn from(tier: ConfigTier) -> Self {
        tier as Self
    }
}

impl ConfigTier {
    /// Returns true if this tier is user-level.
    #[must_use]
    pub const fn is_user(self) -> bool {
        matches!(self, Self::User)
    }

    /// Returns true if this tier is local-level.
    #[must_use]
    pub const fn is_local(self) -> bool {
        matches!(self, Self::Local)
    }

    /// Returns true if this tier is system-level.
    #[must_use]
    pub const fn is_system(self) -> bool {
        matches!(self, Self::System)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tier_ordering() {
        assert!(ConfigTier::User > ConfigTier::Local);
        assert!(ConfigTier::Local > ConfigTier::System);
        assert!(ConfigTier::User > ConfigTier::System);
    }

    #[test]
    fn test_priority_values() {
        assert_eq!(u8::from(ConfigTier::User), 3);
        assert_eq!(u8::from(ConfigTier::Local), 2);
        assert_eq!(u8::from(ConfigTier::System), 1);
    }
}