Skip to main content

ferrous_forge/config/hierarchy/
levels.rs

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