Skip to main content

ferrous_forge/config/hierarchy/
levels.rs

1//! Configuration level definitions
2
3use crate::{Error, Result};
4use std::path::PathBuf;
5
6/// Configuration level in the hierarchy
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
8pub enum ConfigLevel {
9    /// System-wide configuration
10    System,
11    /// User-specific configuration
12    User,
13    /// Project-specific configuration
14    Project,
15}
16
17impl ConfigLevel {
18    /// Get the configuration file path for this level
19    ///
20    /// # Errors
21    ///
22    /// Returns an error if the user config directory cannot be determined.
23    pub fn path(&self) -> Result<PathBuf> {
24        match self {
25            ConfigLevel::System => Ok(PathBuf::from("/etc/ferrous-forge/config.toml")),
26            ConfigLevel::User => {
27                let config_dir = dirs::config_dir()
28                    .ok_or_else(|| Error::config("Could not find config directory"))?;
29                Ok(config_dir.join("ferrous-forge").join("config.toml"))
30            }
31            ConfigLevel::Project => Ok(PathBuf::from(".ferrous-forge/config.toml")),
32        }
33    }
34
35    /// Display name for this level
36    pub fn display_name(&self) -> &'static str {
37        match self {
38            ConfigLevel::System => "System",
39            ConfigLevel::User => "User",
40            ConfigLevel::Project => "Project",
41        }
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_config_level_ordering() {
51        assert!(ConfigLevel::System < ConfigLevel::User);
52        assert!(ConfigLevel::User < ConfigLevel::Project);
53    }
54}