From 1de0e1674e88b348d4b349a46635674e8eb58ee9 Mon Sep 17 00:00:00 2001 From: Ahurac Date: Tue, 9 Apr 2024 19:22:40 +0200 Subject: [PATCH] =?UTF-8?q?command=20:=20impl=C3=A9mentation=20de=20mes=20?= =?UTF-8?q?propres=20ExitCodes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/command.rs | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/command.rs b/src/command.rs index b909d76..d51e003 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,7 +1,15 @@ -use std::process::ExitStatus; +pub struct ExitCode { + exit_code: u8, +} pub trait Command { - fn spawn(&self) -> ExitStatus; + fn spawn(&self) -> ExitCode; +} + +impl ExitCode { + fn new(exit_code: u8) -> Self { + Self { exit_code } + } } pub struct UnixProgram { @@ -20,14 +28,34 @@ impl UnixProgram { } impl Command for UnixProgram { - fn spawn(&self) -> ExitStatus { + fn spawn(&self) -> ExitCode { let mut argv = self.argv.clone(); let program = argv.remove(0); let mut command = std::process::Command::new(program); command.args(argv); - let mut handle = command.spawn().expect("error spawning the command"); - handle.wait().expect("error waiting for the child") + let handle = command.spawn(); + + if handle.is_ok() { + let exit_code = match handle + .unwrap() + .wait() + .expect("error waiting for the child") + .code() + { + Some(code) => code, + None => 1, + }; + + let exit_code = match u8::try_from(exit_code) { + Ok(code) => ExitCode::new(code), + Err(_e) => ExitCode::new(255), + }; + + exit_code + } else { + ExitCode::new(127) + } } }