fallow_engine/resolved_run/
pick.rs1use std::path::Path;
8
9use fallow_config::{
10 PRODUCTION_DEAD_CODE_ENV, PRODUCTION_DUPES_ENV, PRODUCTION_ENV, PRODUCTION_HEALTH_ENV,
11 ProductionAnalysis, ProductionConfig,
12};
13use fallow_types::sourced::{Given, Scope, Source, Sourced};
14
15const PRODUCTION_DEFAULT: &str = "production-off";
17const IN_MEMORY_CONFIG: &str = "config";
20
21fn pick<T>(
24 fallback: Sourced<T>,
25 candidates: impl IntoIterator<Item = Option<Sourced<T>>>,
26) -> Sourced<T> {
27 candidates
28 .into_iter()
29 .flatten()
30 .reduce(|best, candidate| {
31 if candidate.rank() > best.rank() {
32 candidate
33 } else {
34 best
35 }
36 })
37 .unwrap_or(fallback)
38}
39
40#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
42pub struct PerAnalysis<T> {
43 pub dead_code: T,
45 pub health: T,
47 pub dupes: T,
49}
50
51impl<T> PerAnalysis<T> {
52 pub const fn get(&self, analysis: ProductionAnalysis) -> &T {
54 match analysis {
55 ProductionAnalysis::DeadCode => &self.dead_code,
56 ProductionAnalysis::Health => &self.health,
57 ProductionAnalysis::Dupes => &self.dupes,
58 }
59 }
60
61 fn from_fn(mut f: impl FnMut(ProductionAnalysis) -> T) -> Self {
62 Self {
63 dead_code: f(ProductionAnalysis::DeadCode),
64 health: f(ProductionAnalysis::Health),
65 dupes: f(ProductionAnalysis::Dupes),
66 }
67 }
68}
69
70#[derive(Debug, Clone, Copy, Default)]
73pub struct ProductionInputs {
74 pub global_flag: Option<Given<bool>>,
76 pub own_flag: PerAnalysis<Option<Given<bool>>>,
79 pub global_env: Option<bool>,
81 pub own_env: PerAnalysis<Option<bool>>,
83}
84
85pub fn decide_production(
94 inputs: &ProductionInputs,
95 config: &ProductionConfig,
96 file: Option<&Path>,
97) -> PerAnalysis<Sourced<bool>> {
98 let given = |value: Option<Given<bool>>, scope| {
99 value.map(|given| {
100 let source = Source::Given {
101 origin: given.origin,
102 };
103 Sourced::new(given.value, source, scope)
104 })
105 };
106 let environment = |value: Option<bool>, name, scope| {
107 value.map(|value| Sourced::new(value, Source::Environment { name }, scope))
108 };
109 PerAnalysis::from_fn(|analysis| {
110 pick(
111 production_fallback(config, analysis),
112 [
113 given(*inputs.own_flag.get(analysis), Scope::Analysis),
114 given(inputs.global_flag, Scope::Global),
115 environment(
116 *inputs.own_env.get(analysis),
117 production_env_name(analysis),
118 Scope::Analysis,
119 ),
120 environment(inputs.global_env, PRODUCTION_ENV, Scope::Global),
121 file.map(|file| production_config_value(config, file, analysis)),
122 ],
123 )
124 })
125}
126
127const fn production_env_name(analysis: ProductionAnalysis) -> &'static str {
128 match analysis {
129 ProductionAnalysis::DeadCode => PRODUCTION_DEAD_CODE_ENV,
130 ProductionAnalysis::Health => PRODUCTION_HEALTH_ENV,
131 ProductionAnalysis::Dupes => PRODUCTION_DUPES_ENV,
132 }
133}
134
135const fn production_config_key(analysis: ProductionAnalysis) -> &'static str {
136 match analysis {
137 ProductionAnalysis::DeadCode => "production.deadCode",
138 ProductionAnalysis::Health => "production.health",
139 ProductionAnalysis::Dupes => "production.dupes",
140 }
141}
142
143fn production_config_value(
146 config: &ProductionConfig,
147 file: &Path,
148 analysis: ProductionAnalysis,
149) -> Sourced<bool> {
150 let (key, scope) = match config {
151 ProductionConfig::Global(_) => ("production", Scope::Global),
152 ProductionConfig::PerAnalysis(_) => (production_config_key(analysis), Scope::Analysis),
153 };
154 Sourced::new(
155 config.for_analysis(analysis),
156 Source::ConfigFile {
157 file: file.to_path_buf(),
158 key,
159 },
160 scope,
161 )
162}
163
164fn production_fallback(config: &ProductionConfig, analysis: ProductionAnalysis) -> Sourced<bool> {
165 let source = if *config == ProductionConfig::default() {
166 Source::Default {
167 rule: PRODUCTION_DEFAULT,
168 }
169 } else {
170 Source::Derived {
171 from: IN_MEMORY_CONFIG,
172 }
173 };
174 Sourced::new(config.for_analysis(analysis), source, Scope::Global)
175}
176
177#[cfg(test)]
178#[path = "pick_tests.rs"]
179mod tests;