Skip to main content

cfgmatic_paths/core/
config_tier.rs

1//! Configuration tier definitions.
2
3/// Configuration tier represents the priority level of a configuration source.
4///
5/// Tiers are ordered by priority: User > Local > System.
6/// When merging configurations, User tier overrides Local,
7/// and Local overrides System.
8///
9/// The enum's discriminant values directly represent priority levels,
10/// allowing use via `u8::from(tier)` or `tier as u8`.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
12pub enum ConfigTier {
13    /// System-wide configuration.
14    /// Lowest priority, typically in `/etc/`.
15    /// Examples: `/etc/app/`, `/etc/xdg/app/`
16    System = 1,
17
18    /// Local/machine-specific configuration.
19    /// Medium priority, typically in `/usr/local/etc` or similar.
20    /// Examples: `/usr/local/etc/app/`, `/opt/app/etc/`
21    Local = 2,
22
23    /// User-specific configuration.
24    /// Highest priority, typically in user's home directory.
25    /// Examples: `~/.config/app/`, `~/.apprc`
26    #[default]
27    User = 3,
28}
29
30impl From<ConfigTier> for u8 {
31    fn from(tier: ConfigTier) -> Self {
32        tier as Self
33    }
34}
35
36impl ConfigTier {
37    /// Returns true if this tier is user-level.
38    #[must_use]
39    pub const fn is_user(self) -> bool {
40        matches!(self, Self::User)
41    }
42
43    /// Returns true if this tier is local-level.
44    #[must_use]
45    pub const fn is_local(self) -> bool {
46        matches!(self, Self::Local)
47    }
48
49    /// Returns true if this tier is system-level.
50    #[must_use]
51    pub const fn is_system(self) -> bool {
52        matches!(self, Self::System)
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_tier_ordering() {
62        assert!(ConfigTier::User > ConfigTier::Local);
63        assert!(ConfigTier::Local > ConfigTier::System);
64        assert!(ConfigTier::User > ConfigTier::System);
65    }
66
67    #[test]
68    fn test_priority_values() {
69        assert_eq!(u8::from(ConfigTier::User), 3);
70        assert_eq!(u8::from(ConfigTier::Local), 2);
71        assert_eq!(u8::from(ConfigTier::System), 1);
72    }
73}