alopex_cli/batch/
exit_code.rs1use crate::config::EXIT_CODE_INTERRUPTED;
2
3#[allow(dead_code)]
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5#[repr(i32)]
6pub enum ExitCode {
7 Success = 0,
8 Error = 1,
9 Warning = 2,
10 Interrupted = EXIT_CODE_INTERRUPTED,
11}
12
13#[allow(dead_code)]
14impl ExitCode {
15 pub fn as_i32(self) -> i32 {
16 self as i32
17 }
18}
19
20#[allow(dead_code)]
21#[derive(Debug, Default)]
22pub struct ExitCodeCollector {
23 success_count: usize,
24 error_count: usize,
25}
26
27#[allow(dead_code)]
28impl ExitCodeCollector {
29 pub fn new() -> Self {
30 Self::default()
31 }
32
33 pub fn record_success(&mut self) {
34 self.success_count += 1;
35 }
36
37 pub fn record_error(&mut self) {
38 self.error_count += 1;
39 }
40
41 pub fn finalize(&self) -> ExitCode {
42 match (self.success_count > 0, self.error_count > 0) {
43 (false, false) => ExitCode::Success,
44 (true, false) => ExitCode::Success,
45 (false, true) => ExitCode::Error,
46 (true, true) => ExitCode::Warning,
47 }
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn exit_code_values() {
57 assert_eq!(ExitCode::Success.as_i32(), 0);
58 assert_eq!(ExitCode::Error.as_i32(), 1);
59 assert_eq!(ExitCode::Warning.as_i32(), 2);
60 assert_eq!(ExitCode::Interrupted.as_i32(), EXIT_CODE_INTERRUPTED);
61 }
62
63 #[test]
64 fn collector_defaults_to_success() {
65 let collector = ExitCodeCollector::new();
66
67 assert_eq!(collector.finalize(), ExitCode::Success);
68 }
69
70 #[test]
71 fn collector_reports_success_only() {
72 let mut collector = ExitCodeCollector::new();
73 collector.record_success();
74
75 assert_eq!(collector.finalize(), ExitCode::Success);
76 }
77
78 #[test]
79 fn collector_reports_error_only() {
80 let mut collector = ExitCodeCollector::new();
81 collector.record_error();
82
83 assert_eq!(collector.finalize(), ExitCode::Error);
84 }
85
86 #[test]
87 fn collector_reports_warning_on_mixed_results() {
88 let mut collector = ExitCodeCollector::new();
89 collector.record_success();
90 collector.record_error();
91
92 assert_eq!(collector.finalize(), ExitCode::Warning);
93 }
94}