stern4rust/settings/package_sections.rs
1// Copyright 2025 Umberto Gotti <umberto.gotti@umbertogotti.dev>
2// Licensed under the MIT License
3// SPDX-License-Identifier: MIT
4
5use anyhow::Result;
6use std::collections::BTreeMap;
7use std::path::Path;
8
9use crate::settings::config_file::ConfigFile;
10use crate::settings::package_config::PackageConfig;
11
12// The `[package.<name>]` sections of the root `stern4rust.toml`, and the two
13// questions asked of them: which section applies to the package about to be
14// walked, and does every section name a package this run actually scans.
15//
16// A type rather than a pair of functions on the runner, because the second
17// question only means anything beside the first: a section is either the rule
18// set for a member or a name that matches nothing, and nothing else.
19//
20// See [ADR-PerPackageConfiguration](../../docs/ADRs/ADR-PerPackageConfiguration.md).
21pub struct PackageSections {
22 sections: BTreeMap<String, PackageConfig>,
23}
24
25impl PackageSections {
26 pub fn new(sections: BTreeMap<String, PackageConfig>) -> Self {
27 Self { sections }
28 }
29
30 // No file and a file with no sections are the same answer here. They differ
31 // only for `ConfigFile::load`, which has to tell a missing file from an
32 // unreadable one.
33 pub fn load(directory: &Path) -> Result<Self> {
34 Ok(Self::new(
35 ConfigFile::load(directory)?
36 .map(|file| file.packages)
37 .unwrap_or_default(),
38 ))
39 }
40
41 pub fn of(&self, name: &str) -> Option<&PackageConfig> {
42 self.sections.get(name)
43 }
44
45 // Every rule any section stands down on.
46 //
47 // The report answers for the run as a whole, and a rule that did not apply
48 // to one package did not apply to the run. Reporting it as applied would be
49 // the overstatement this tool exists to refuse: a stand-down is only
50 // acceptable while the report names it. Until the report speaks per package,
51 // this is what keeps it honest -- it understates, naming a rule as skipped
52 // even where most packages applied it.
53 pub fn skipped_anywhere(&self) -> Vec<String> {
54 let mut skipped: Vec<String> = self
55 .sections
56 .values()
57 .flat_map(|section| section.skip.iter().cloned())
58 .collect();
59 skipped.sort();
60 skipped.dedup();
61 skipped
62 }
63
64 pub fn is_empty(&self) -> bool {
65 self.sections.is_empty()
66 }
67
68 // A section naming no package in the *workspace* is an error, for the reason
69 // a misspelled `--rule` name is: it reads as a rule set being applied, and
70 // `deny_unknown_fields` cannot catch it because the section name is data
71 // rather than a key.
72 //
73 // The workspace, not the scan. Scoping a run to one package is an ordinary
74 // thing to do, and the sections for the others are not typos -- checking
75 // against the scan made `--package node` an error in any repository whose
76 // root config had sections, which is every repository this exists for.
77 pub fn validate(&self, workspace: &[&str]) -> Result<()> {
78 let unknown: Vec<&str> = self
79 .sections
80 .keys()
81 .map(String::as_str)
82 .filter(|name| !workspace.contains(name))
83 .collect();
84 if unknown.is_empty() {
85 return Ok(());
86 }
87 Err(anyhow::anyhow!(
88 "{} configures package(s) that are not in this workspace: {} -- it holds: {}",
89 ConfigFile::NAME,
90 unknown.join(", "),
91 workspace.join(", ")
92 ))
93 }
94}