command : implémentation de mes propres ExitCodes
This commit is contained in:
parent
2bf03f0662
commit
1de0e1674e
1 changed files with 33 additions and 5 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue