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 fileTake 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 back1use std::process;2Raise SIGINT when dying by ^CTavian Barnesbefore adoptionfe99270
lines 3–5why, one step back3#[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 back6#[derive(Debug, Clone, Copy, PartialEq)]7pub enum ExitCode {8 Success,Add -q/--quiet/--has-match flag (#813)Asha20before adoption334488c
line 9why, one step back9 HasResults(bool),[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad
lines 10–13why, one step back10 GeneralError,11 KilledBySigint,12}13Fix clippy warningsDavid Peterbefore adoption2a2126c
lines 14–16why, one step back14impl 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 back17 ExitCode::Success => 0,Add -q/--quiet/--has-match flag (#813)Asha20before adoption334488c
line 18why, one step back18 ExitCode::HasResults(has_results) => !has_results as i32,[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad
lines 19–25why, one step back19 ExitCode::GeneralError => 1,20 ExitCode::KilledBySigint => 130,21 }22 }23}2425impl ExitCode {Fix clippy warningsDavid Peterbefore adoption2a2126c
line 26why, one step back26 fn is_error(self) -> bool {Add -q/--quiet/--has-match flag (#813)Asha20before adoption334488c
line 27why, one step back27 i32::from(self) != 0Add an exit() method to ExitCodeTavian Barnesbefore adoption2b1bf47
lines 28–31why, one step back28 }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 back32 #[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 }41Add an exit() method to ExitCodeTavian Barnesbefore adoption2b1bf47
line 42why, one step back42 process::exit(self.into())[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad
lines 43–45why, one step back43 }44}45Refactor: `merge_exit_codes` now takes an `impl IntoIterator<Item = ExitCode>`Niklas Mohrinbefore adoption45a8645
lines 46–47why, one step back46pub 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 back48 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 back59 assert_eq!(merge_exitcodes([]), ExitCode::Success);[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad
lines 60–64why, one step back60 }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 back65 merge_exitcodes([ExitCode::GeneralError]),[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad
lines 66–68why, one step back66 ExitCode::GeneralError67 );68 assert_eq!(Refactor: `merge_exit_codes` now takes an `impl IntoIterator<Item = ExitCode>`Niklas Mohrinbefore adoption45a8645
line 69why, one step back69 merge_exitcodes([ExitCode::KilledBySigint]),[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad
lines 70–72why, one step back70 ExitCode::GeneralError71 );72 assert_eq!(Refactor: `merge_exit_codes` now takes an `impl IntoIterator<Item = ExitCode>`Niklas Mohrinbefore adoption45a8645
line 73why, one step back73 merge_exitcodes([ExitCode::KilledBySigint, ExitCode::Success]),[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad
lines 74–76why, one step back74 ExitCode::GeneralError75 );76 assert_eq!(Refactor: `merge_exit_codes` now takes an `impl IntoIterator<Item = ExitCode>`Niklas Mohrinbefore adoption45a8645
line 77why, one step back77 merge_exitcodes([ExitCode::Success, ExitCode::GeneralError]),[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad
lines 78–80why, one step back78 ExitCode::GeneralError79 );80 assert_eq!(Refactor: `merge_exit_codes` now takes an `impl IntoIterator<Item = ExitCode>`Niklas Mohrinbefore adoption45a8645
line 81why, one step back81 merge_exitcodes([ExitCode::GeneralError, ExitCode::KilledBySigint]),[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad
lines 82–87why, one step back82 ExitCode::GeneralError83 );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 back88 assert_eq!(merge_exitcodes([ExitCode::Success]), ExitCode::Success);[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad
line 89why, one step back89 assert_eq!(Refactor: `merge_exit_codes` now takes an `impl IntoIterator<Item = ExitCode>`Niklas Mohrinbefore adoption45a8645
line 90why, one step back90 merge_exitcodes([ExitCode::Success, ExitCode::Success]),[ownership] initial struct, parsing, and test frameworkAlexandru Macoveibefore adoption30cb4ad
lines 91–94why, one step back91 ExitCode::Success92 );93 }94}