Skip to main content

greentic_flow/
schema_mode.rs

1use anyhow::{Result, anyhow};
2use std::env;
3
4/// CLI-wide mode governing how input schemas are enforced.
5#[derive(Copy, Clone, Debug, PartialEq, Eq)]
6pub enum SchemaMode {
7    Strict,
8    Permissive,
9}
10
11impl SchemaMode {
12    /// Resolve the schema mode from the CLI flag and `GREENTIC_FLOW_STRICT`.
13    ///
14    /// - `--permissive` trumps the environment variable.
15    /// - `GREENTIC_FLOW_STRICT=0` → permissive, `1` → strict.
16    /// - Missing settings default to strict.
17    pub fn resolve(cli_permissive: bool) -> Result<Self> {
18        if cli_permissive {
19            return Ok(SchemaMode::Permissive);
20        }
21        match env::var("GREENTIC_FLOW_STRICT") {
22            Ok(val) => match val.as_str() {
23                "0" => Ok(SchemaMode::Permissive),
24                "1" => Ok(SchemaMode::Strict),
25                other => Err(anyhow!(
26                    "GREENTIC_FLOW_STRICT must be '0' or '1', got '{other}'"
27                )),
28            },
29            Err(env::VarError::NotPresent) => Ok(SchemaMode::Strict),
30            Err(err) => Err(anyhow!("failed to read GREENTIC_FLOW_STRICT: {err}")),
31        }
32    }
33
34    pub fn is_permissive(&self) -> bool {
35        matches!(self, SchemaMode::Permissive)
36    }
37}