Skip to main content

codex_config/
config_layer_source.rs

1use codex_utils_absolute_path::AbsolutePathBuf;
2use serde_json::Value as JsonValue;
3
4/// Provenance for one layer in the effective Codex configuration.
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum ConfigLayerSource {
7    /// Managed preferences delivered by MDM.
8    Mdm { domain: String, key: String },
9    /// Host-wide configuration loaded from a file.
10    System { file: AbsolutePathBuf },
11    /// Configuration delivered by an enterprise cloud bundle.
12    EnterpriseManaged { id: String, name: String },
13    /// User configuration, optionally augmented by a selected profile.
14    User {
15        file: AbsolutePathBuf,
16        profile: Option<String>,
17    },
18    /// Configuration loaded from a project's `.codex` directory.
19    Project { dot_codex_folder: AbsolutePathBuf },
20    /// Overrides supplied for the current session.
21    SessionFlags,
22    /// Legacy managed configuration loaded from a file.
23    LegacyManagedConfigTomlFromFile { file: AbsolutePathBuf },
24    /// Legacy managed configuration delivered by MDM.
25    LegacyManagedConfigTomlFromMdm,
26}
27
28impl ConfigLayerSource {
29    /// A setting from a layer with a higher precedence overrides a setting
30    /// from a layer with a lower precedence.
31    pub fn precedence(&self) -> i16 {
32        match self {
33            ConfigLayerSource::Mdm { .. } => 0,
34            ConfigLayerSource::System { .. } => 10,
35            ConfigLayerSource::EnterpriseManaged { .. } => 15,
36            ConfigLayerSource::User { profile, .. } => {
37                if profile.is_some() {
38                    21
39                } else {
40                    20
41                }
42            }
43            ConfigLayerSource::Project { .. } => 25,
44            ConfigLayerSource::SessionFlags => 30,
45            ConfigLayerSource::LegacyManagedConfigTomlFromFile { .. } => 40,
46            ConfigLayerSource::LegacyManagedConfigTomlFromMdm => 50,
47        }
48    }
49}
50
51/// Compares [`ConfigLayerSource`] by precedence, so `A < B` means settings
52/// from layer `A` will be overridden by settings from layer `B`.
53impl PartialOrd for ConfigLayerSource {
54    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
55        Some(self.precedence().cmp(&other.precedence()))
56    }
57}
58
59/// Identity and version information for a configuration layer.
60#[derive(Debug, Clone, PartialEq)]
61pub struct ConfigLayerMetadata {
62    pub name: ConfigLayerSource,
63    pub version: String,
64}
65
66/// A materialized configuration layer and its provenance.
67#[derive(Debug, Clone, PartialEq)]
68pub struct ConfigLayer {
69    pub name: ConfigLayerSource,
70    pub version: String,
71    pub config: JsonValue,
72    pub disabled_reason: Option<String>,
73}
74
75pub fn format_config_layer_source(source: &ConfigLayerSource, config_toml_file: &str) -> String {
76    match source {
77        ConfigLayerSource::Mdm { domain, key } => {
78            format!("MDM ({domain}:{key})")
79        }
80        ConfigLayerSource::System { file } => {
81            format!("system ({})", file.as_path().display())
82        }
83        ConfigLayerSource::EnterpriseManaged { id, name } => {
84            format!("enterprise-managed ({name}, {id})")
85        }
86        ConfigLayerSource::User { file, .. } => {
87            format!("user ({})", file.as_path().display())
88        }
89        ConfigLayerSource::Project { dot_codex_folder } => {
90            format!(
91                "project ({}/{config_toml_file})",
92                dot_codex_folder.as_path().display()
93            )
94        }
95        ConfigLayerSource::SessionFlags => "session-flags".to_string(),
96        ConfigLayerSource::LegacyManagedConfigTomlFromFile { file } => {
97            format!("legacy managed_config.toml ({})", file.as_path().display())
98        }
99        ConfigLayerSource::LegacyManagedConfigTomlFromMdm => {
100            "legacy managed_config.toml (MDM)".to_string()
101        }
102    }
103}