summaryrefslogtreecommitdiffstats
path: root/vendor/snapbox/src/action.rs
blob: a4b849919d7a900fdef98a5fdaf3eda5890e32ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
pub const DEFAULT_ACTION_ENV: &str = "SNAPSHOTS";

/// Test action, see [`Assert`][crate::Assert]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Action {
    /// Do not run the test
    Skip,
    /// Ignore test failures
    Ignore,
    /// Fail on mismatch
    Verify,
    /// Overwrite on mismatch
    Overwrite,
}

impl Action {
    pub fn with_env_var(var: impl AsRef<std::ffi::OsStr>) -> Option<Self> {
        let var = var.as_ref();
        let value = std::env::var_os(var)?;
        Self::with_env_value(value)
    }

    pub fn with_env_value(value: impl AsRef<std::ffi::OsStr>) -> Option<Self> {
        let value = value.as_ref();
        match value.to_str()? {
            "skip" => Some(Action::Skip),
            "ignore" => Some(Action::Ignore),
            "verify" => Some(Action::Verify),
            "overwrite" => Some(Action::Overwrite),
            _ => None,
        }
    }
}

impl Default for Action {
    fn default() -> Self {
        Self::Verify
    }
}