Skip to main content

codeint_config/
dependencies.rs

1use std::collections::BTreeSet;
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6use crate::config_error::{ConfigFileKind, ConfigurationFileError, KeyPath, KeySegment};
7
8#[cfg(test)]
9mod tests;
10
11#[derive(Debug, Clone, Default, Deserialize, Serialize, JsonSchema)]
12#[serde(deny_unknown_fields)]
13pub(crate) struct DependenciesFile {
14    #[serde(default, skip_serializing_if = "Vec::is_empty")]
15    #[schemars(
16        description = "Canonical external package names excluded from unused, unlisted, and dev-in-production dependency Findings."
17    )]
18    ignore: Vec<String>,
19}
20
21impl DependenciesFile {
22    pub(crate) fn is_unset(&self) -> bool {
23        self.ignore.is_empty()
24    }
25}
26
27#[derive(Debug, Clone, Default, PartialEq, Eq)]
28pub struct DependenciesConfig {
29    ignore: BTreeSet<String>,
30}
31
32impl DependenciesConfig {
33    pub(crate) fn from_file(
34        file: DependenciesFile,
35    ) -> Result<DependenciesConfig, ConfigurationFileError> {
36        let ignore_key = KeyPath::default()
37            .child(KeySegment::Key("dependencies".to_owned()))
38            .child(KeySegment::Key("ignore".to_owned()));
39        let mut ignore = BTreeSet::new();
40        for (index, entry) in file.ignore.into_iter().enumerate() {
41            let trimmed = entry.trim();
42            if trimmed.is_empty() {
43                return Err(ConfigurationFileError::EmptyDependencyIgnore {
44                    file: ConfigFileKind::Json,
45                    key: ignore_key.child(KeySegment::Index(index)),
46                });
47            }
48            ignore.insert(trimmed.to_owned());
49        }
50        Ok(DependenciesConfig { ignore })
51    }
52
53    pub fn ignored(&self) -> &BTreeSet<String> {
54        &self.ignore
55    }
56
57    pub fn is_ignored(&self, dependency: &str) -> bool {
58        self.ignore.contains(dependency)
59    }
60}