1
use std::env;
2

            
3
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
4
pub enum AcceptMode {
5
    /// Normal test mode: compare generated files with expected files and do not rewrite fixtures.
6
    Disabled,
7
    /// Rewrite expected output fixtures and runtime budgets exactly as observed.
8
    Accept,
9
    /// Rewrite output fixtures, but only raise runtime budgets (max of current and observed).
10
    ///
11
    /// Useful when recording budgets over several runs: each run keeps the slowest time seen so
12
    /// far, so a one-off fast fluke does not lower the recorded value.
13
    AcceptWithMaxTimes,
14
}
15

            
16
impl AcceptMode {
17
5193
    pub fn from_env() -> Self {
18
5193
        match env::var("ACCEPT").as_deref() {
19
            Ok("false") => Self::Disabled,
20
            Ok("true") => Self::Accept,
21
            Ok("with-max-times") => Self::AcceptWithMaxTimes,
22
5193
            _ => Self::Disabled,
23
        }
24
5193
    }
25

            
26
5193
    pub fn accepts_outputs(self) -> bool {
27
5193
        !matches!(self, Self::Disabled)
28
5193
    }
29

            
30
2154
    pub fn records_expected_time(self) -> bool {
31
2154
        matches!(self, Self::Accept | Self::AcceptWithMaxTimes)
32
2154
    }
33

            
34
    pub fn expected_time_to_record(self, current: Option<u64>, observed: u64) -> Option<u64> {
35
        match self {
36
            Self::Accept => Some(observed),
37
            Self::AcceptWithMaxTimes if current.is_none_or(|current| observed > current) => {
38
                Some(observed)
39
            }
40
            _ => None,
41
        }
42
    }
43

            
44
    pub fn refresh_hint() -> &'static str {
45
        "Run with ACCEPT=true or ACCEPT=with-max-times"
46
    }
47
}