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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Program exit code hinting through the `ExitCode` trait

use super::CheckError;
use super::OutcomeStats;
use super::OutcomesByDescription;
use super::RuleOutcome;
use checklist::FilterError;
use failure;

/// A means of genericizing expected process exit code
/// Once the `std::process::Termination` trait hits stable,
/// this trait may be deprecated or abstracted away.
/// See: https://github.com/rust-lang/rust/issues/43301
pub trait ExitCode {
    /// The
    fn exit_code(&self) -> i32;
}

impl ExitCode for RuleOutcome {
    fn exit_code(&self) -> i32 {
        match *self {
            RuleOutcome::Success => 0,
            RuleOutcome::Failure => 1,
            RuleOutcome::Undetermined => 2,
        }
    }
}

impl ExitCode for OutcomeStats {
    fn exit_code(&self) -> i32 {
        RuleOutcome::from(self).exit_code()
    }
}

impl ExitCode for OutcomesByDescription {
    fn exit_code(&self) -> i32 {
        OutcomeStats::from(self).exit_code()
    }
}

impl ExitCode for CheckError {
    fn exit_code(&self) -> i32 {
        match *self {
            CheckError::PrintOutputFailure { .. } => 11,
            _ => 10,
        }
    }
}

impl ExitCode for FilterError {
    fn exit_code(&self) -> i32 {
        match *self {
            FilterError::RuleChecklistReadError(_) => 21,
            FilterError::RequestedRuleNotFound { .. } => 22,
            _ => 20,
        }
    }
}

impl ExitCode for failure::Error {
    fn exit_code(&self) -> i32 {
        1
    }
}

impl<T, E> ExitCode for Result<T, E>
where
    T: ExitCode,
    E: ExitCode,
{
    fn exit_code(&self) -> i32 {
        match self {
            Ok(ref r) => r.exit_code(),
            Err(e) => e.exit_code(),
        }
    }
}