Skip to main content

stern4rust/settings/
package_config.rs

1// Copyright 2025 Umberto Gotti <umberto.gotti@umbertogotti.dev>
2// Licensed under the MIT License
3// SPDX-License-Identifier: MIT
4
5use serde::Deserialize;
6use std::path::Path;
7use std::path::PathBuf;
8
9// What one package in a workspace says about itself, in a `[package.<name>]`
10// section of the root `stern4rust.toml`.
11//
12// The keys here are the ones that shape which rules run and against what. The
13// two that are missing are missing on purpose: `baseline` is a set of
14// fingerprints for one run and `offence-threshold` is about how the report
15// ends, so neither is a property of a package. Leaving them off the struct is
16// what makes `deny_unknown_fields` reject them, with the message it already
17// gives a misspelled key -- no separate check, and nothing to keep in step.
18//
19// See [ADR-PerPackageConfiguration](../../docs/ADRs/ADR-PerPackageConfiguration.md).
20#[derive(Debug, Default, Deserialize, PartialEq, Eq)]
21#[serde(deny_unknown_fields, rename_all = "kebab-case")]
22pub struct PackageConfig {
23    #[serde(default)]
24    pub header_file: Option<PathBuf>,
25    #[serde(default)]
26    pub max_files_per_directory: Option<usize>,
27    #[serde(default)]
28    pub max_subfolders_per_directory: Option<usize>,
29    #[serde(default)]
30    pub rules: Vec<String>,
31    #[serde(default)]
32    pub skip: Vec<String>,
33    #[serde(default)]
34    pub exclude: Vec<String>,
35}
36
37impl PackageConfig {
38    // Resolved against the directory the config file sits in, which for a root
39    // file is the workspace root -- so a section writes `docs/header.txt` and
40    // not the `../docs/header.txt` that a file beside a member manifest needed.
41    pub fn header_file_from(&self, directory: &Path) -> Option<PathBuf> {
42        self.header_file.as_ref().map(|path| directory.join(path))
43    }
44}