Skip to main content

fallow_config/
production_env.rs

1//! The `FALLOW_PRODUCTION*` environment variables. The resolver of the engine
2//! reads them from the `EnvSnapshot` of a run and decides the production mode
3//! of each analysis. [`ProductionEnv`] parses the values from a lookup
4//! function; it does not read the process environment.
5
6use crate::ProductionAnalysis;
7
8/// The global production variable.
9pub const PRODUCTION_ENV: &str = "FALLOW_PRODUCTION";
10/// The production variable of the dead-code analysis.
11pub const PRODUCTION_DEAD_CODE_ENV: &str = "FALLOW_PRODUCTION_DEAD_CODE";
12/// The production variable of the health analysis.
13pub const PRODUCTION_HEALTH_ENV: &str = "FALLOW_PRODUCTION_HEALTH";
14/// The production variable of the duplication analysis.
15pub const PRODUCTION_DUPES_ENV: &str = "FALLOW_PRODUCTION_DUPES";
16
17/// The boolean value of an environment variable: `1`, `true`, `yes` and `on`
18/// are `true`; `0`, `false`, `no` and `off` are `false`. The case and the
19/// surrounding white space do not matter. Each other value is `None`.
20#[must_use]
21pub fn bool_env_value(value: &str) -> Option<bool> {
22    match value.trim().to_ascii_lowercase().as_str() {
23        "1" | "true" | "yes" | "on" => Some(true),
24        "0" | "false" | "no" | "off" => Some(false),
25        _ => None,
26    }
27}
28
29/// The values of the `FALLOW_PRODUCTION*` variables. `None` is a variable
30/// that is not set or that has no boolean value.
31#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
32pub struct ProductionEnv {
33    /// `FALLOW_PRODUCTION`.
34    pub global: Option<bool>,
35    /// `FALLOW_PRODUCTION_DEAD_CODE`.
36    pub dead_code: Option<bool>,
37    /// `FALLOW_PRODUCTION_HEALTH`.
38    pub health: Option<bool>,
39    /// `FALLOW_PRODUCTION_DUPES`.
40    pub dupes: Option<bool>,
41}
42
43impl ProductionEnv {
44    /// Read the variables through `lookup`, which returns the value of one
45    /// variable.
46    #[must_use]
47    pub fn from_lookup(lookup: impl Fn(&str) -> Option<String>) -> Self {
48        let read = |name| lookup(name).as_deref().and_then(bool_env_value);
49        Self {
50            global: read(PRODUCTION_ENV),
51            dead_code: read(PRODUCTION_DEAD_CODE_ENV),
52            health: read(PRODUCTION_HEALTH_ENV),
53            dupes: read(PRODUCTION_DUPES_ENV),
54        }
55    }
56
57    /// The production override of one analysis from the environment: the
58    /// variable of the analysis, else `FALLOW_PRODUCTION`. `None` means that
59    /// the environment does not decide.
60    #[must_use]
61    pub const fn override_for(self, analysis: ProductionAnalysis) -> Option<bool> {
62        let own = match analysis {
63            ProductionAnalysis::DeadCode => self.dead_code,
64            ProductionAnalysis::Health => self.health,
65            ProductionAnalysis::Dupes => self.dupes,
66        };
67        match own {
68            Some(value) => Some(value),
69            None => self.global,
70        }
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn bool_values_ignore_case_and_white_space() {
80        for value in ["1", "true", " YES ", "On"] {
81            assert_eq!(bool_env_value(value), Some(true), "{value:?}");
82        }
83        for value in ["0", "false", "No", " off"] {
84            assert_eq!(bool_env_value(value), Some(false), "{value:?}");
85        }
86        for value in ["", "2", "enabled"] {
87            assert_eq!(bool_env_value(value), None, "{value:?}");
88        }
89    }
90
91    #[test]
92    fn the_variable_of_the_analysis_wins_over_the_global_one() {
93        let env = ProductionEnv::from_lookup(|name| match name {
94            PRODUCTION_ENV => Some("1".to_owned()),
95            PRODUCTION_HEALTH_ENV => Some("false".to_owned()),
96            PRODUCTION_DUPES_ENV => Some("maybe".to_owned()),
97            _ => None,
98        });
99        assert_eq!(env.override_for(ProductionAnalysis::DeadCode), Some(true));
100        assert_eq!(env.override_for(ProductionAnalysis::Health), Some(false));
101        assert_eq!(env.override_for(ProductionAnalysis::Dupes), Some(true));
102        assert_eq!(
103            ProductionEnv::default().override_for(ProductionAnalysis::DeadCode),
104            None
105        );
106    }
107}