fallow_engine/baseline/
saved_scope.rs1use 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 #[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 #[must_use]
29 pub fn saved_mode(&self) -> fallow_output::BaselineMode {
30 fallow_output::BaselineMode::from_saved_names(&self.scope_reasons)
31 }
32
33 #[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 #[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 #[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
56fn 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#[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#[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}