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    pub fn path(&self) -> Result<PathBuf> {
20        match self {
21            ConfigLevel::System => Ok(PathBuf::from("/etc/ferrous-forge/config.toml")),
22            ConfigLevel::User => {
23                let config_dir = dirs::config_dir()
24                    .ok_or_else(|| Error::config("Could not find config directory"))?;
25                Ok(config_dir.join("ferrous-forge").join("config.toml"))
26            }
27            ConfigLevel::Project => Ok(PathBuf::from(".ferrous-forge/config.toml")),
28        }
29    }
30
31    /// Display name for this level
32    pub fn display_name(&self) -> &'static str {
33        match self {
34            ConfigLevel::System => "System",
35            ConfigLevel::User => "User",
36            ConfigLevel::Project => "Project",
37        }
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn test_config_level_ordering() {
47        assert!(ConfigLevel::System < ConfigLevel::User);
48        assert!(ConfigLevel::User < ConfigLevel::Project);
49    }
50}