Skip to main content

fallow_config/
production_env.rs

1//! The `FALLOW_PRODUCTION*` environment variables. The CLI and the typed MCP
2//! routes read them at their own boundary through [`ProductionEnv`], so both
3//! give one value the same meaning. The programmatic API and the Node bindings
4//! do not read them: their callers pass explicit options.
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 from the environment of the process.
45    #[must_use]
46    pub fn from_process() -> Self {
47        Self::from_lookup(|name| std::env::var(name).ok())
48    }
49
50    /// Read the variables through `lookup`, which returns the value of one
51    /// variable.
52    #[must_use]
53    pub fn from_lookup(lookup: impl Fn(&str) -> Option<String>) -> Self {
54        let read = |name| lookup(name).as_deref().and_then(bool_env_value);
55        Self {
56            global: read(PRODUCTION_ENV),
57            dead_code: read(PRODUCTION_DEAD_CODE_ENV),
58            health: read(PRODUCTION_HEALTH_ENV),
59            dupes: read(PRODUCTION_DUPES_ENV),
60        }
61    }
62
63    /// The production override of one analysis from the environment: the
64    /// variable of the analysis, else `FALLOW_PRODUCTION`. `None` means that
65    /// the environment does not decide.
66    #[must_use]
67    pub const fn override_for(self, analysis: ProductionAnalysis) -> Option<bool> {
68        let own = match analysis {
69            ProductionAnalysis::DeadCode => self.dead_code,
70            ProductionAnalysis::Health => self.health,
71            ProductionAnalysis::Dupes => self.dupes,
72        };
73        match own {
74            Some(value) => Some(value),
75            None => self.global,
76        }
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn bool_values_ignore_case_and_white_space() {
86        for value in ["1", "true", " YES ", "On"] {
87            assert_eq!(bool_env_value(value), Some(true), "{value:?}");
88        }
89        for value in ["0", "false", "No", " off"] {
90            assert_eq!(bool_env_value(value), Some(false), "{value:?}");
91        }
92        for value in ["", "2", "enabled"] {
93            assert_eq!(bool_env_value(value), None, "{value:?}");
94        }
95    }
96
97    #[test]
98    fn the_variable_of_the_analysis_wins_over_the_global_one() {
99        let env = ProductionEnv::from_lookup(|name| match name {
100            PRODUCTION_ENV => Some("1".to_owned()),
101            PRODUCTION_HEALTH_ENV => Some("false".to_owned()),
102            PRODUCTION_DUPES_ENV => Some("maybe".to_owned()),
103            _ => None,
104        });
105        assert_eq!(env.override_for(ProductionAnalysis::DeadCode), Some(true));
106        assert_eq!(env.override_for(ProductionAnalysis::Health), Some(false));
107        assert_eq!(env.override_for(ProductionAnalysis::Dupes), Some(true));
108        assert_eq!(
109            ProductionEnv::default().override_for(ProductionAnalysis::DeadCode),
110            None
111        );
112    }
113}