exit_codes.rs
This file
Path
src/exit_codes.rs
94 lines · 2.4 KiB · Rust
Last changed by
Fix clippy suggestionsDavid Peterbefore adoption9ea882d
History of this fileTake it
The walk stopped after 200 commits: lines older than a5f17db 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};5Fix clippy suggestionsDavid Peterbefore adoption9ea882d
line 6why, one step back6#[derive(Debug, Clone, Copy, PartialEq, Eq)]CI: Run clippy on stable and use msrv setting in `clippy.toml`Niklas Mohrinbefore adoptiona5f17db
lines 7–27why, one step back7pub enum ExitCode {8 Success,9 HasResults(bool),10 GeneralError,11 KilledBySigint,12}1314impl From<ExitCode> for i32 {15 fn from(code: ExitCode) -> Self {16 match code {17 ExitCode::Success => 0,18 ExitCode::HasResults(has_results) => !has_results as i32,19 ExitCode::GeneralError => 1,20 ExitCode::KilledBySigint => 130,21 }22 }23}2425impl ExitCode {26 fn is_error(self) -> bool {27 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())CI: Run clippy on stable and use msrv setting in `clippy.toml`Niklas Mohrinbefore adoptiona5f17db
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) {CI: Run clippy on stable and use msrv setting in `clippy.toml`Niklas Mohrinbefore adoptiona5f17db
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);CI: Run clippy on stable and use msrv setting in `clippy.toml`Niklas Mohrinbefore adoptiona5f17db
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]),CI: Run clippy on stable and use msrv setting in `clippy.toml`Niklas Mohrinbefore adoptiona5f17db
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]),CI: Run clippy on stable and use msrv setting in `clippy.toml`Niklas Mohrinbefore adoptiona5f17db
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]),CI: Run clippy on stable and use msrv setting in `clippy.toml`Niklas Mohrinbefore adoptiona5f17db
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]),CI: Run clippy on stable and use msrv setting in `clippy.toml`Niklas Mohrinbefore adoptiona5f17db
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]),CI: Run clippy on stable and use msrv setting in `clippy.toml`Niklas Mohrinbefore adoptiona5f17db
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);CI: Run clippy on stable and use msrv setting in `clippy.toml`Niklas Mohrinbefore adoptiona5f17db
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]),CI: Run clippy on stable and use msrv setting in `clippy.toml`Niklas Mohrinbefore adoptiona5f17db
lines 91–94why, one step back91 ExitCode::Success92 );93 }94}