Skip to content
Myr

exit_codes.rs

This file

Path

src/exit_codes.rs

94 lines · 2.4 KiB · Rust

Last changed by

Raise SIGINT when dying by ^CTavian Barnesbefore adoptionfe99270

History of this file

Take it

The walk stopped after 200 commits: lines older than 30cb4ad point at it.

Add an exit() method to ExitCodeTavian Barnesbefore adoption2b1bf47

lines 1–2why, one step back
1use std::process;
2

Raise SIGINT when dying by ^CTavian Barnesbefore adoptionfe99270

lines 3–5why, one step back
3#[cfg(unix)]
4use nix::sys::signal::{raise, signal, SigHandler, Signal};5

[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad

lines 6–8why, one step back
6#[derive(Debug, Clone, Copy, PartialEq)]
7pub enum ExitCode {8 Success,

Add -q/--quiet/--has-match flag (#813)Asha20before adoption334488c

line 9why, one step back
9 HasResults(bool),

[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad

lines 10–13why, one step back
10 GeneralError,
11 KilledBySigint,12}13

Fix clippy warningsDavid Peterbefore adoption2a2126c

lines 14–16why, one step back
14impl From<ExitCode> for i32 {
15 fn from(code: ExitCode) -> Self {16 match code {

[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad

line 17why, one step back
17 ExitCode::Success => 0,

Add -q/--quiet/--has-match flag (#813)Asha20before adoption334488c

line 18why, one step back
18 ExitCode::HasResults(has_results) => !has_results as i32,

[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad

lines 19–25why, one step back
19 ExitCode::GeneralError => 1,
20 ExitCode::KilledBySigint => 130,21 }22 }23}2425impl ExitCode {

Fix clippy warningsDavid Peterbefore adoption2a2126c

line 26why, one step back
26 fn is_error(self) -> bool {

Add -q/--quiet/--has-match flag (#813)Asha20before adoption334488c

line 27why, one step back
27 i32::from(self) != 0

Add an exit() method to ExitCodeTavian Barnesbefore adoption2b1bf47

lines 28–31why, one step back
28 }
2930 /// Exit the process with the appropriate code.31 pub fn exit(self) -> ! {

Raise SIGINT when dying by ^CTavian Barnesbefore adoptionfe99270

lines 32–41why, one step back
32 #[cfg(unix)]
33 if self == ExitCode::KilledBySigint {34 // Get rid of the SIGINT handler, if present, and raise SIGINT35 unsafe {36 if signal(Signal::SIGINT, SigHandler::SigDfl).is_ok() {37 let _ = raise(Signal::SIGINT);38 }39 }40 }41

Add an exit() method to ExitCodeTavian Barnesbefore adoption2b1bf47

line 42why, one step back
42 process::exit(self.into())

[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad

lines 43–45why, one step back
43 }
44}45

Refactor: `merge_exit_codes` now takes an `impl IntoIterator<Item = ExitCode>`Niklas Mohrinbefore adoption45a8645

lines 46–47why, one step back
46pub fn merge_exitcodes(results: impl IntoIterator<Item = ExitCode>) -> ExitCode {
47 if results.into_iter().any(ExitCode::is_error) {

[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad

lines 48–58why, one step back
48 return ExitCode::GeneralError;
49 }50 ExitCode::Success51}5253#[cfg(test)]54mod tests {55 use super::*;5657 #[test]58 fn success_when_no_results() {

Refactor: `merge_exit_codes` now takes an `impl IntoIterator<Item = ExitCode>`Niklas Mohrinbefore adoption45a8645

line 59why, one step back
59 assert_eq!(merge_exitcodes([]), ExitCode::Success);

[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad

lines 60–64why, one step back
60 }
6162 #[test]63 fn general_error_if_at_least_one_error() {64 assert_eq!(

Refactor: `merge_exit_codes` now takes an `impl IntoIterator<Item = ExitCode>`Niklas Mohrinbefore adoption45a8645

line 65why, one step back
65 merge_exitcodes([ExitCode::GeneralError]),

[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad

lines 66–68why, one step back
66 ExitCode::GeneralError
67 );68 assert_eq!(

Refactor: `merge_exit_codes` now takes an `impl IntoIterator<Item = ExitCode>`Niklas Mohrinbefore adoption45a8645

line 69why, one step back
69 merge_exitcodes([ExitCode::KilledBySigint]),

[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad

lines 70–72why, one step back
70 ExitCode::GeneralError
71 );72 assert_eq!(

Refactor: `merge_exit_codes` now takes an `impl IntoIterator<Item = ExitCode>`Niklas Mohrinbefore adoption45a8645

line 73why, one step back
73 merge_exitcodes([ExitCode::KilledBySigint, ExitCode::Success]),

[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad

lines 74–76why, one step back
74 ExitCode::GeneralError
75 );76 assert_eq!(

Refactor: `merge_exit_codes` now takes an `impl IntoIterator<Item = ExitCode>`Niklas Mohrinbefore adoption45a8645

line 77why, one step back
77 merge_exitcodes([ExitCode::Success, ExitCode::GeneralError]),

[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad

lines 78–80why, one step back
78 ExitCode::GeneralError
79 );80 assert_eq!(

Refactor: `merge_exit_codes` now takes an `impl IntoIterator<Item = ExitCode>`Niklas Mohrinbefore adoption45a8645

line 81why, one step back
81 merge_exitcodes([ExitCode::GeneralError, ExitCode::KilledBySigint]),

[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad

lines 82–87why, one step back
82 ExitCode::GeneralError
83 );84 }8586 #[test]87 fn success_if_no_error() {

Refactor: `merge_exit_codes` now takes an `impl IntoIterator<Item = ExitCode>`Niklas Mohrinbefore adoption45a8645

line 88why, one step back
88 assert_eq!(merge_exitcodes([ExitCode::Success]), ExitCode::Success);

[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad

line 89why, one step back
89 assert_eq!(

Refactor: `merge_exit_codes` now takes an `impl IntoIterator<Item = ExitCode>`Niklas Mohrinbefore adoption45a8645

line 90why, one step back
90 merge_exitcodes([ExitCode::Success, ExitCode::Success]),

[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad

lines 91–94why, one step back
91 ExitCode::Success
92 );93 }94}