Skip to main content

codeint_config/
hazards.rs

1use std::collections::BTreeSet;
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6use crate::config_error::{ConfigFileKind, ConfigurationFileError, KeyPath, KeySegment};
7
8#[derive(Debug, Clone, Default, PartialEq, Eq)]
9pub struct HazardsConfig {
10    pub disabled_rules: Vec<String>,
11}
12
13impl HazardsConfig {
14    pub fn is_disabled(&self, rule: &str) -> bool {
15        self.disabled_rules.iter().any(|disabled| disabled == rule)
16    }
17
18    pub fn disabled_rules_set(&self) -> BTreeSet<String> {
19        self.disabled_rules.iter().cloned().collect()
20    }
21
22    pub(crate) fn from_file(
23        file: HazardsConfigFile,
24    ) -> Result<HazardsConfig, ConfigurationFileError> {
25        let base_key = KeyPath::nested("hazards", "disabled_rules");
26        if let Some((index, _)) = file
27            .disabled_rules
28            .iter()
29            .enumerate()
30            .find(|(_, entry)| entry.trim().is_empty())
31        {
32            return Err(ConfigurationFileError::EmptyHazardRuleName {
33                file: ConfigFileKind::Json,
34                key: base_key.child(KeySegment::Index(index)),
35            });
36        }
37        Ok(HazardsConfig {
38            disabled_rules: file.disabled_rules,
39        })
40    }
41}
42
43#[derive(Debug, Clone, Default, Deserialize, Serialize, JsonSchema)]
44#[serde(deny_unknown_fields)]
45pub(crate) struct HazardsConfigFile {
46    #[serde(default, skip_serializing_if = "Vec::is_empty")]
47    #[schemars(
48        description = "Names of Hazard line rules turned off across the repository. A turned-off rule adds no Hazard line and no Finding."
49    )]
50    pub(crate) disabled_rules: Vec<String>,
51}
52
53pub(crate) fn is_hazards_unset(file: &HazardsConfigFile) -> bool {
54    file.disabled_rules.is_empty()
55}