Skip to main content

fallow_engine/baseline/
saved_scope.rs

1//! What a saved baseline file records about the run that saved it.
2
3use super::{BaselineData, DuplicationBaselineData, HealthBaselineData};
4
5impl BaselineData {
6    pub(crate) const FINDING_FIELDS: &'static [&'static str] = {
7        macro_rules! field_names {
8            ($($field:ident),* $(,)?) => {
9                &[$(stringify!($field)),*]
10            };
11        }
12        with_baseline_fields!(field_names)
13    };
14
15    /// Record the channels that narrowed the saving run.
16    #[must_use]
17    pub fn with_scope_reasons(mut self, reasons: fallow_output::BaselineScopeReasons) -> Self {
18        self.scope_reasons = reasons
19            .iter()
20            .map(|reason| reason.as_str().to_owned())
21            .collect();
22        self
23    }
24}
25
26impl DuplicationBaselineData {
27    /// The analysis mode recorded by the saving run.
28    #[must_use]
29    pub fn saved_mode(&self) -> fallow_output::BaselineMode {
30        fallow_output::BaselineMode::from_saved_names(&self.scope_reasons)
31    }
32
33    /// Record whether the saving run used production mode.
34    #[must_use]
35    pub fn with_production(mut self, production: bool) -> Self {
36        self.scope_reasons = production_reason(production);
37        self
38    }
39}
40
41impl HealthBaselineData {
42    /// The analysis mode recorded by the saving run.
43    #[must_use]
44    pub(crate) fn saved_mode(&self) -> fallow_output::BaselineMode {
45        fallow_output::BaselineMode::from_saved_names(&self.scope_reasons)
46    }
47
48    /// Record whether the saving run used production mode.
49    #[must_use]
50    pub(crate) fn with_production(mut self, production: bool) -> Self {
51        self.scope_reasons = production_reason(production);
52        self
53    }
54}
55
56/// Health and duplication baselines record only production mode.
57fn production_reason(production: bool) -> Vec<String> {
58    if production {
59        vec![fallow_output::ScopeReason::Production.as_str().to_owned()]
60    } else {
61        Vec::new()
62    }
63}
64
65/// The non-empty finding categories of a dead-code baseline file, by field
66/// name. Empty for a file that is not a JSON object.
67#[must_use]
68pub fn saved_finding_categories(json: &str) -> Vec<String> {
69    let Ok(value) = serde_json::from_str::<serde_json::Value>(json) else {
70        return Vec::new();
71    };
72    BaselineData::FINDING_FIELDS
73        .iter()
74        .copied()
75        .filter(|field| {
76            value
77                .get(field)
78                .and_then(serde_json::Value::as_array)
79                .is_some_and(|entries| !entries.is_empty())
80        })
81        .map(str::to_owned)
82        .collect()
83}
84
85/// The `scope_reasons` a baseline file recorded when it was saved. Empty for a
86/// whole-project save, a file saved before the file recorded its scope, or a
87/// file that is not JSON.
88#[must_use]
89pub fn saved_scope_reasons(json: &str) -> Vec<String> {
90    serde_json::from_str::<serde_json::Value>(json)
91        .ok()
92        .and_then(|value| {
93            value.get("scope_reasons")?.as_array().map(|reasons| {
94                reasons
95                    .iter()
96                    .filter_map(serde_json::Value::as_str)
97                    .map(str::to_owned)
98                    .collect()
99            })
100        })
101        .unwrap_or_default()
102}