Skip to main content

alint_core/
config.rs

1use std::collections::HashMap;
2use std::path::PathBuf;
3
4use serde::Deserialize;
5
6use crate::facts::FactSpec;
7use crate::level::Level;
8
9/// Parsed form of a `.alint.yml` file.
10#[derive(Debug, Clone, Deserialize, Default)]
11#[serde(deny_unknown_fields)]
12pub struct Config {
13    pub version: u32,
14    /// Other config files this one inherits from. Entries resolved
15    /// left-to-right; later entries override earlier ones; the
16    /// current file's own definitions override everything it extends.
17    ///
18    /// Each entry is either a bare string (local path, `https://`
19    /// URL with SRI, or `alint://bundled/...`) or a mapping with
20    /// `url:` and optional `only:` / `except:` filters.
21    #[serde(default)]
22    pub extends: Vec<ExtendsEntry>,
23    #[serde(default)]
24    pub ignore: Vec<String>,
25    #[serde(default = "default_respect_gitignore")]
26    pub respect_gitignore: bool,
27    /// Free-form string variables referenced from rule messages and
28    /// `when` expressions as `{{vars.<name>}}` and `vars.<name>`.
29    #[serde(default)]
30    pub vars: HashMap<String, String>,
31    /// Repository properties evaluated once per run and referenced from
32    /// `when` clauses as `facts.<id>`.
33    #[serde(default)]
34    pub facts: Vec<FactSpec>,
35    #[serde(default)]
36    pub rules: Vec<RuleSpec>,
37    /// Maximum file size, in bytes, that content-editing fixes
38    /// will read and rewrite. Files over this limit are reported
39    /// as `Skipped` in the fix report and a one-line warning is
40    /// printed to stderr. Defaults to 1 MiB; set explicitly to
41    /// `null` to disable the cap entirely.
42    ///
43    /// Path-only fixes (`file_create`, `file_remove`,
44    /// `file_rename`) ignore the cap — they don't read content.
45    #[serde(default = "default_fix_size_limit")]
46    pub fix_size_limit: Option<u64>,
47    /// Opt in to discovery of `.alint.yml` / `.alint.yaml` files
48    /// in subdirectories. When `true`, the loader walks the
49    /// repository tree (from the root config's directory,
50    /// respecting `.gitignore` and `ignore:`) and finds any
51    /// nested config files; each nested rule's path-like fields
52    /// (`paths`, `select`, `primary`) are prefixed with the
53    /// directory that nested config lives in, so the rule
54    /// auto-scopes to that subtree. Default `false`.
55    ///
56    /// Only the user's top-level config may set this — nested
57    /// configs themselves cannot spawn further nested discovery.
58    #[serde(default)]
59    pub nested_configs: bool,
60}
61
62// Returning `Option<u64>` (rather than bare `u64`) keeps the
63// YAML-facing type consistent with `Config.fix_size_limit`:
64// users set `null` in YAML to mean "no limit". The Option is
65// load-bearing at the field level, so clippy's warning on the
66// default fn is noise here.
67#[allow(clippy::unnecessary_wraps)]
68fn default_fix_size_limit() -> Option<u64> {
69    Some(1 << 20)
70}
71
72fn default_respect_gitignore() -> bool {
73    true
74}
75
76impl Config {
77    pub const CURRENT_VERSION: u32 = 1;
78}
79
80/// A single `extends:` entry. Accepts either a bare string (the
81/// classic form — a local path, `https://` URL with SRI, or
82/// `alint://bundled/<name>@<rev>`) or a mapping that adds
83/// `only:` / `except:` filters on the inherited rule set.
84///
85/// ```yaml
86/// extends:
87///   - alint://bundled/oss-baseline@v1             # classic form
88///   - url: alint://bundled/rust@v1                # filtered form
89///     except: [rust-no-target-dir]                # drop by id
90///   - url: ./team-defaults.yml
91///     only: [team-copyright-header]               # keep by id
92/// ```
93///
94/// Filters resolve against the *fully-resolved* rule set of the
95/// entry (i.e. anything it transitively extends). `only:` and
96/// `except:` are mutually exclusive on a single entry; listing an
97/// unknown rule id is a config error so typos surface at load
98/// time.
99#[derive(Debug, Clone, Deserialize)]
100#[serde(untagged)]
101pub enum ExtendsEntry {
102    Url(String),
103    Filtered {
104        url: String,
105        #[serde(default)]
106        only: Option<Vec<String>>,
107        #[serde(default)]
108        except: Option<Vec<String>>,
109    },
110}
111
112impl ExtendsEntry {
113    /// The URL / path of the extended config. Uniform across both
114    /// enum variants.
115    pub fn url(&self) -> &str {
116        match self {
117            Self::Url(s) | Self::Filtered { url: s, .. } => s,
118        }
119    }
120
121    /// Rule ids to keep (drop everything else). `None` when no
122    /// `only:` filter is specified.
123    pub fn only(&self) -> Option<&[String]> {
124        match self {
125            Self::Filtered { only: Some(v), .. } => Some(v),
126            _ => None,
127        }
128    }
129
130    /// Rule ids to drop. `None` when no `except:` filter is
131    /// specified.
132    pub fn except(&self) -> Option<&[String]> {
133        match self {
134            Self::Filtered {
135                except: Some(v), ..
136            } => Some(v),
137            _ => None,
138        }
139    }
140}
141
142/// YAML shape for a rule's `paths:` field — a single glob, an array (with
143/// optional `!pattern` negations), or an explicit `{include, exclude}` pair.
144/// For the include/exclude form, each field accepts either a single string
145/// or a list of strings.
146#[derive(Debug, Clone, Deserialize)]
147#[serde(untagged)]
148pub enum PathsSpec {
149    Single(String),
150    Many(Vec<String>),
151    IncludeExclude {
152        #[serde(default, deserialize_with = "string_or_vec")]
153        include: Vec<String>,
154        #[serde(default, deserialize_with = "string_or_vec")]
155        exclude: Vec<String>,
156    },
157}
158
159fn string_or_vec<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
160where
161    D: serde::Deserializer<'de>,
162{
163    #[derive(Deserialize)]
164    #[serde(untagged)]
165    enum OneOrMany {
166        One(String),
167        Many(Vec<String>),
168    }
169    match OneOrMany::deserialize(deserializer)? {
170        OneOrMany::One(s) => Ok(vec![s]),
171        OneOrMany::Many(v) => Ok(v),
172    }
173}
174
175/// YAML-level description of a rule before it is instantiated into a `Box<dyn Rule>`
176/// by a [`RuleBuilder`](crate::registry::RuleBuilder).
177#[derive(Debug, Clone, Deserialize)]
178pub struct RuleSpec {
179    pub id: String,
180    pub kind: String,
181    pub level: Level,
182    #[serde(default)]
183    pub paths: Option<PathsSpec>,
184    #[serde(default)]
185    pub message: Option<String>,
186    #[serde(default)]
187    pub policy_url: Option<String>,
188    #[serde(default)]
189    pub when: Option<String>,
190    /// Optional mechanical-fix strategy. Rules whose builders understand
191    /// the chosen op attach a [`Fixer`](crate::Fixer) to the built rule;
192    /// rules whose kind is incompatible with the op return a config error
193    /// at build time.
194    #[serde(default)]
195    pub fix: Option<FixSpec>,
196    /// Restrict the rule to files / directories tracked in git's index.
197    /// When `true`, the rule's `paths`-matched entries are intersected
198    /// with the set of git-tracked files; entries that exist in the
199    /// walked tree but aren't in `git ls-files` output are skipped.
200    /// Only meaningful for rule kinds that opt in (currently the
201    /// existence family — `file_exists`, `file_absent`, `dir_exists`,
202    /// `dir_absent`); rule kinds that don't support it surface a clean
203    /// config error when this is `true` so silent mis-configuration
204    /// doesn't slip through.
205    ///
206    /// Default `false`. Has no effect outside a git repo.
207    #[serde(default)]
208    pub git_tracked_only: bool,
209    /// The entire YAML mapping, retained so each rule builder can deserialize
210    /// its kind-specific fields without every option being represented here.
211    #[serde(flatten)]
212    pub extra: serde_yaml_ng::Mapping,
213}
214
215/// The `fix:` block on a rule. Exactly one op key must be present —
216/// alint errors at load time when the op and rule kind are incompatible.
217#[derive(Debug, Clone, Deserialize)]
218#[serde(untagged)]
219pub enum FixSpec {
220    FileCreate {
221        file_create: FileCreateFixSpec,
222    },
223    FileRemove {
224        file_remove: FileRemoveFixSpec,
225    },
226    FilePrepend {
227        file_prepend: FilePrependFixSpec,
228    },
229    FileAppend {
230        file_append: FileAppendFixSpec,
231    },
232    FileRename {
233        file_rename: FileRenameFixSpec,
234    },
235    FileTrimTrailingWhitespace {
236        file_trim_trailing_whitespace: FileTrimTrailingWhitespaceFixSpec,
237    },
238    FileAppendFinalNewline {
239        file_append_final_newline: FileAppendFinalNewlineFixSpec,
240    },
241    FileNormalizeLineEndings {
242        file_normalize_line_endings: FileNormalizeLineEndingsFixSpec,
243    },
244    FileStripBidi {
245        file_strip_bidi: FileStripBidiFixSpec,
246    },
247    FileStripZeroWidth {
248        file_strip_zero_width: FileStripZeroWidthFixSpec,
249    },
250    FileStripBom {
251        file_strip_bom: FileStripBomFixSpec,
252    },
253    FileCollapseBlankLines {
254        file_collapse_blank_lines: FileCollapseBlankLinesFixSpec,
255    },
256}
257
258impl FixSpec {
259    /// The op name as it appears in YAML — used in config-error messages.
260    pub fn op_name(&self) -> &'static str {
261        match self {
262            Self::FileCreate { .. } => "file_create",
263            Self::FileRemove { .. } => "file_remove",
264            Self::FilePrepend { .. } => "file_prepend",
265            Self::FileAppend { .. } => "file_append",
266            Self::FileRename { .. } => "file_rename",
267            Self::FileTrimTrailingWhitespace { .. } => "file_trim_trailing_whitespace",
268            Self::FileAppendFinalNewline { .. } => "file_append_final_newline",
269            Self::FileNormalizeLineEndings { .. } => "file_normalize_line_endings",
270            Self::FileStripBidi { .. } => "file_strip_bidi",
271            Self::FileStripZeroWidth { .. } => "file_strip_zero_width",
272            Self::FileStripBom { .. } => "file_strip_bom",
273            Self::FileCollapseBlankLines { .. } => "file_collapse_blank_lines",
274        }
275    }
276}
277
278#[derive(Debug, Clone, Deserialize)]
279#[serde(deny_unknown_fields)]
280pub struct FileCreateFixSpec {
281    /// Content to write. Required — there is no implicit empty default;
282    /// for an empty file, pass `content: ""` explicitly.
283    pub content: String,
284    /// Path to create, relative to the repo root. When omitted, the
285    /// rule builder substitutes the first literal entry from the rule's
286    /// `paths:` list.
287    #[serde(default)]
288    pub path: Option<PathBuf>,
289    /// Whether to create intermediate directories. Defaults to true.
290    #[serde(default = "default_create_parents")]
291    pub create_parents: bool,
292}
293
294fn default_create_parents() -> bool {
295    true
296}
297
298#[derive(Debug, Clone, Deserialize, Default)]
299#[serde(deny_unknown_fields)]
300pub struct FileRemoveFixSpec {}
301
302#[derive(Debug, Clone, Deserialize)]
303#[serde(deny_unknown_fields)]
304pub struct FilePrependFixSpec {
305    /// Bytes to insert at the beginning of each violating file. A
306    /// trailing newline in `content` is the caller's responsibility.
307    pub content: String,
308}
309
310#[derive(Debug, Clone, Deserialize)]
311#[serde(deny_unknown_fields)]
312pub struct FileAppendFixSpec {
313    /// Bytes to append to each violating file. A leading newline in
314    /// `content` is the caller's responsibility.
315    pub content: String,
316}
317
318/// Empty marker: `file_rename` takes no parameters. The target name
319/// is derived from the parent rule (e.g. `filename_case` converts the
320/// stem to its configured case; the extension is preserved).
321#[derive(Debug, Clone, Deserialize, Default)]
322#[serde(deny_unknown_fields)]
323pub struct FileRenameFixSpec {}
324
325/// Empty marker. Behavior: read file (subject to `fix_size_limit`),
326/// strip trailing space/tab on every line, write back.
327#[derive(Debug, Clone, Deserialize, Default)]
328#[serde(deny_unknown_fields)]
329pub struct FileTrimTrailingWhitespaceFixSpec {}
330
331/// Empty marker. Behavior: if the file has content and does not
332/// end with `\n`, append one.
333#[derive(Debug, Clone, Deserialize, Default)]
334#[serde(deny_unknown_fields)]
335pub struct FileAppendFinalNewlineFixSpec {}
336
337/// Empty marker. Behavior: rewrite the file with every line ending
338/// replaced by the parent rule's configured target (`lf` or `crlf`).
339#[derive(Debug, Clone, Deserialize, Default)]
340#[serde(deny_unknown_fields)]
341pub struct FileNormalizeLineEndingsFixSpec {}
342
343/// Empty marker. Behavior: remove every Unicode bidi control
344/// character (U+202A–202E, U+2066–2069) from the file's content.
345#[derive(Debug, Clone, Deserialize, Default)]
346#[serde(deny_unknown_fields)]
347pub struct FileStripBidiFixSpec {}
348
349/// Empty marker. Behavior: remove every zero-width character
350/// (U+200B / U+200C / U+200D / U+FEFF) from the file's content,
351/// *except* a leading BOM (U+FEFF at position 0) — that's the
352/// responsibility of the `no_bom` rule.
353#[derive(Debug, Clone, Deserialize, Default)]
354#[serde(deny_unknown_fields)]
355pub struct FileStripZeroWidthFixSpec {}
356
357/// Empty marker. Behavior: remove a leading UTF-8/UTF-16/UTF-32
358/// BOM byte sequence if present; otherwise a no-op.
359#[derive(Debug, Clone, Deserialize, Default)]
360#[serde(deny_unknown_fields)]
361pub struct FileStripBomFixSpec {}
362
363/// Empty marker. Behavior: collapse runs of blank lines longer than
364/// the parent rule's `max` down to exactly `max` blank lines.
365#[derive(Debug, Clone, Deserialize, Default)]
366#[serde(deny_unknown_fields)]
367pub struct FileCollapseBlankLinesFixSpec {}
368
369impl RuleSpec {
370    /// Deserialize the full spec (common + kind-specific fields) into a typed
371    /// options struct. Common fields are reconstructed into the mapping so
372    /// the target struct can `#[derive(Deserialize)]` against the whole shape
373    /// when convenient.
374    pub fn deserialize_options<T>(&self) -> crate::error::Result<T>
375    where
376        T: serde::de::DeserializeOwned,
377    {
378        Ok(serde_yaml_ng::from_value(serde_yaml_ng::Value::Mapping(
379            self.extra.clone(),
380        ))?)
381    }
382}
383
384/// Rule specification for nested rules (e.g. the `require:` block of
385/// `for_each_dir`). Unlike [`RuleSpec`], `id` and `level` are synthesized
386/// from the parent rule — users just supply the `kind` plus kind-specific
387/// options, optionally with a `message` / `policy_url` / `when`.
388#[derive(Debug, Clone, Deserialize)]
389pub struct NestedRuleSpec {
390    pub kind: String,
391    #[serde(default)]
392    pub paths: Option<PathsSpec>,
393    #[serde(default)]
394    pub message: Option<String>,
395    #[serde(default)]
396    pub policy_url: Option<String>,
397    #[serde(default)]
398    pub when: Option<String>,
399    #[serde(flatten)]
400    pub extra: serde_yaml_ng::Mapping,
401}
402
403impl NestedRuleSpec {
404    /// Synthesize a full [`RuleSpec`] for a single iteration, applying
405    /// path-template substitution (using the iterated entry's tokens) to
406    /// every string field. The resulting spec has `id =
407    /// "{parent_id}.require[{idx}]"` and inherits `level` from the parent.
408    pub fn instantiate(
409        &self,
410        parent_id: &str,
411        idx: usize,
412        level: Level,
413        tokens: &crate::template::PathTokens,
414    ) -> RuleSpec {
415        RuleSpec {
416            id: format!("{parent_id}.require[{idx}]"),
417            kind: self.kind.clone(),
418            level,
419            paths: self
420                .paths
421                .as_ref()
422                .map(|p| crate::template::render_paths_spec(p, tokens)),
423            message: self
424                .message
425                .as_deref()
426                .map(|m| crate::template::render_path(m, tokens)),
427            policy_url: self.policy_url.clone(),
428            when: self.when.clone(),
429            fix: None,
430            // Nested rules don't currently expose
431            // `git_tracked_only` from their parent's spec — the
432            // option is meaningful on top-level rules only for
433            // now. If/when `for_each_dir`'s nested rules need it,
434            // plumb it through here.
435            git_tracked_only: false,
436            extra: crate::template::render_mapping(self.extra.clone(), tokens),
437        }
438    }
439}