Skip to main content

rumdl_lib/config/
source_tracking.rs

1use crate::types::LineLength;
2use indexmap::IndexMap;
3use std::collections::BTreeMap;
4use std::collections::HashMap;
5use std::marker::PhantomData;
6
7use super::flavor::{ConfigLoaded, MarkdownFlavor};
8
9/// Configuration source with clear precedence hierarchy.
10///
11/// Precedence order (lower values override higher values):
12/// - Default (0): Built-in defaults
13/// - UserConfig (1): User-level ~/.config/rumdl/rumdl.toml
14/// - PyprojectToml (2): Project-level pyproject.toml
15/// - ProjectConfig (3): Project-level .rumdl.toml (most specific)
16/// - Cli (4): Command-line flags (highest priority)
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum ConfigSource {
19    /// Built-in default configuration
20    Default,
21    /// User-level configuration from ~/.config/rumdl/rumdl.toml
22    UserConfig,
23    /// Project-level configuration from pyproject.toml
24    PyprojectToml,
25    /// Project-level configuration from .rumdl.toml or rumdl.toml
26    ProjectConfig,
27    /// Command-line flags (highest precedence)
28    Cli,
29}
30
31#[derive(Debug, Clone)]
32pub struct ConfigOverride<T> {
33    pub value: T,
34    pub source: ConfigSource,
35    pub file: Option<String>,
36    pub line: Option<usize>,
37}
38
39#[derive(Debug, Clone)]
40pub struct SourcedValue<T> {
41    pub value: T,
42    pub source: ConfigSource,
43    pub overrides: Vec<ConfigOverride<T>>,
44}
45
46impl<T: Clone> SourcedValue<T> {
47    pub fn new(value: T, source: ConfigSource) -> Self {
48        Self {
49            value: value.clone(),
50            source,
51            overrides: vec![ConfigOverride {
52                value,
53                source,
54                file: None,
55                line: None,
56            }],
57        }
58    }
59
60    /// Merges a new override into this SourcedValue based on source precedence.
61    /// If the new source has higher or equal precedence, the value and source are updated,
62    /// and the new override is added to the history.
63    pub fn merge_override(
64        &mut self,
65        new_value: T,
66        new_source: ConfigSource,
67        new_file: Option<String>,
68        new_line: Option<usize>,
69    ) {
70        // Helper function to get precedence, defined locally or globally
71        fn source_precedence(src: ConfigSource) -> u8 {
72            match src {
73                ConfigSource::Default => 0,
74                ConfigSource::UserConfig => 1,
75                ConfigSource::PyprojectToml => 2,
76                ConfigSource::ProjectConfig => 3,
77                ConfigSource::Cli => 4,
78            }
79        }
80
81        if source_precedence(new_source) >= source_precedence(self.source) {
82            self.value = new_value.clone();
83            self.source = new_source;
84            self.overrides.push(ConfigOverride {
85                value: new_value,
86                source: new_source,
87                file: new_file,
88                line: new_line,
89            });
90        }
91    }
92
93    pub fn push_override(&mut self, value: T, source: ConfigSource, file: Option<String>, line: Option<usize>) {
94        // This is essentially merge_override without the precedence check
95        // We might consolidate these later, but keep separate for now during refactor
96        self.value = value.clone();
97        self.source = source;
98        self.overrides.push(ConfigOverride {
99            value,
100            source,
101            file,
102            line,
103        });
104    }
105}
106
107impl<T: Clone + Eq + std::hash::Hash> SourcedValue<Vec<T>> {
108    /// Merges a new value using union semantics (for arrays like `disable`)
109    /// Values from both sources are combined, with deduplication
110    pub fn merge_union(
111        &mut self,
112        new_value: Vec<T>,
113        new_source: ConfigSource,
114        new_file: Option<String>,
115        new_line: Option<usize>,
116    ) {
117        fn source_precedence(src: ConfigSource) -> u8 {
118            match src {
119                ConfigSource::Default => 0,
120                ConfigSource::UserConfig => 1,
121                ConfigSource::PyprojectToml => 2,
122                ConfigSource::ProjectConfig => 3,
123                ConfigSource::Cli => 4,
124            }
125        }
126
127        if source_precedence(new_source) >= source_precedence(self.source) {
128            // Union: combine values from both sources with deduplication
129            let mut combined = self.value.clone();
130            for item in new_value.iter() {
131                if !combined.contains(item) {
132                    combined.push(item.clone());
133                }
134            }
135
136            self.value = combined;
137            self.source = new_source;
138            self.overrides.push(ConfigOverride {
139                value: new_value,
140                source: new_source,
141                file: new_file,
142                line: new_line,
143            });
144        }
145    }
146}
147
148#[derive(Debug, Clone)]
149pub struct SourcedGlobalConfig {
150    pub enable: SourcedValue<Vec<String>>,
151    pub disable: SourcedValue<Vec<String>>,
152    pub exclude: SourcedValue<Vec<String>>,
153    pub include: SourcedValue<Vec<String>>,
154    pub respect_gitignore: SourcedValue<bool>,
155    pub line_length: SourcedValue<LineLength>,
156    pub output_format: Option<SourcedValue<String>>,
157    pub fixable: SourcedValue<Vec<String>>,
158    pub unfixable: SourcedValue<Vec<String>>,
159    pub flavor: SourcedValue<MarkdownFlavor>,
160    pub force_exclude: SourcedValue<bool>,
161    pub cache_dir: Option<SourcedValue<String>>,
162    pub cache: SourcedValue<bool>,
163    pub extend_enable: SourcedValue<Vec<String>>,
164    pub extend_disable: SourcedValue<Vec<String>>,
165}
166
167impl Default for SourcedGlobalConfig {
168    fn default() -> Self {
169        SourcedGlobalConfig {
170            enable: SourcedValue::new(Vec::new(), ConfigSource::Default),
171            disable: SourcedValue::new(Vec::new(), ConfigSource::Default),
172            exclude: SourcedValue::new(Vec::new(), ConfigSource::Default),
173            include: SourcedValue::new(Vec::new(), ConfigSource::Default),
174            respect_gitignore: SourcedValue::new(true, ConfigSource::Default),
175            line_length: SourcedValue::new(LineLength::default(), ConfigSource::Default),
176            output_format: None,
177            fixable: SourcedValue::new(Vec::new(), ConfigSource::Default),
178            unfixable: SourcedValue::new(Vec::new(), ConfigSource::Default),
179            flavor: SourcedValue::new(MarkdownFlavor::default(), ConfigSource::Default),
180            force_exclude: SourcedValue::new(false, ConfigSource::Default),
181            cache_dir: None,
182            cache: SourcedValue::new(true, ConfigSource::Default),
183            extend_enable: SourcedValue::new(Vec::new(), ConfigSource::Default),
184            extend_disable: SourcedValue::new(Vec::new(), ConfigSource::Default),
185        }
186    }
187}
188
189#[derive(Debug, Default, Clone)]
190pub struct SourcedRuleConfig {
191    pub severity: Option<SourcedValue<crate::rule::Severity>>,
192    pub values: BTreeMap<String, SourcedValue<toml::Value>>,
193}
194
195/// Represents configuration loaded from a single source file, with provenance.
196/// Used as an intermediate step before merging into the final SourcedConfig.
197#[derive(Debug, Clone)]
198pub struct SourcedConfigFragment {
199    pub global: SourcedGlobalConfig,
200    pub per_file_ignores: SourcedValue<HashMap<String, Vec<String>>>,
201    pub per_file_flavor: SourcedValue<IndexMap<String, MarkdownFlavor>>,
202    pub code_block_tools: SourcedValue<crate::code_block_tools::CodeBlockToolsConfig>,
203    pub rules: BTreeMap<String, SourcedRuleConfig>,
204    pub unknown_keys: Vec<(String, String, Option<String>)>, // (section, key, file_path)
205                                                             // Note: loaded_files is tracked globally in SourcedConfig.
206}
207
208impl Default for SourcedConfigFragment {
209    fn default() -> Self {
210        Self {
211            global: SourcedGlobalConfig::default(),
212            per_file_ignores: SourcedValue::new(HashMap::new(), ConfigSource::Default),
213            per_file_flavor: SourcedValue::new(IndexMap::new(), ConfigSource::Default),
214            code_block_tools: SourcedValue::new(
215                crate::code_block_tools::CodeBlockToolsConfig::default(),
216                ConfigSource::Default,
217            ),
218            rules: BTreeMap::new(),
219            unknown_keys: Vec::new(),
220        }
221    }
222}
223
224/// Represents a config validation warning or error
225#[derive(Debug, Clone)]
226pub struct ConfigValidationWarning {
227    pub message: String,
228    pub rule: Option<String>,
229    pub key: Option<String>,
230}
231
232/// Configuration with provenance tracking for values.
233///
234/// The `State` type parameter encodes the validation state:
235/// - `ConfigLoaded`: Config has been loaded but not validated
236/// - `ConfigValidated`: Config has been validated and can be converted to `Config`
237///
238/// # Typestate Pattern
239///
240/// This uses the typestate pattern to ensure validation happens before conversion:
241///
242/// ```ignore
243/// let loaded: SourcedConfig<ConfigLoaded> = SourcedConfig::load_with_discovery(...)?;
244/// let validated: SourcedConfig<ConfigValidated> = loaded.validate(&registry)?;
245/// let config: Config = validated.into();  // Only works on ConfigValidated!
246/// ```
247///
248/// Attempting to convert a `ConfigLoaded` config directly to `Config` is a compile error.
249#[derive(Debug, Clone)]
250pub struct SourcedConfig<State = ConfigLoaded> {
251    pub global: SourcedGlobalConfig,
252    pub per_file_ignores: SourcedValue<HashMap<String, Vec<String>>>,
253    pub per_file_flavor: SourcedValue<IndexMap<String, MarkdownFlavor>>,
254    pub code_block_tools: SourcedValue<crate::code_block_tools::CodeBlockToolsConfig>,
255    pub rules: BTreeMap<String, SourcedRuleConfig>,
256    pub loaded_files: Vec<String>,
257    pub unknown_keys: Vec<(String, String, Option<String>)>, // (section, key, file_path)
258    /// Project root directory (parent of config file), used for resolving relative paths
259    pub project_root: Option<std::path::PathBuf>,
260    /// Validation warnings (populated after validate() is called)
261    pub validation_warnings: Vec<ConfigValidationWarning>,
262    /// Phantom data for the state type parameter
263    pub(super) _state: PhantomData<State>,
264}
265
266impl Default for SourcedConfig<ConfigLoaded> {
267    fn default() -> Self {
268        Self {
269            global: SourcedGlobalConfig::default(),
270            per_file_ignores: SourcedValue::new(HashMap::new(), ConfigSource::Default),
271            per_file_flavor: SourcedValue::new(IndexMap::new(), ConfigSource::Default),
272            code_block_tools: SourcedValue::new(
273                crate::code_block_tools::CodeBlockToolsConfig::default(),
274                ConfigSource::Default,
275            ),
276            rules: BTreeMap::new(),
277            loaded_files: Vec::new(),
278            unknown_keys: Vec::new(),
279            project_root: None,
280            validation_warnings: Vec::new(),
281            _state: PhantomData,
282        }
283    }
284}