summaryrefslogtreecommitdiffstats
path: root/crates/runner/src/util/checkable.rs
blob: 8528d29eb9e3263b49ff5ad31dc3da66a7614985 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use std::{
	io::{Error, ErrorKind, Result},
	process::ExitStatus,
};

use nix::sys::wait;

pub trait Checkable {
	fn check(&self) -> Result<()>;
}

impl Checkable for ExitStatus {
	fn check(&self) -> Result<()> {
		if self.success() {
			Ok(())
		} else {
			Err(Error::new(
				ErrorKind::Other,
				format!("Process exited with {}", self),
			))
		}
	}
}

impl Checkable for wait::WaitStatus {
	fn check(&self) -> Result<()> {
		let message = match self {
			wait::WaitStatus::Exited(_, 0) => return Ok(()),
			wait::WaitStatus::Exited(_, code) => format!("Process exited with exit code: {}", code),
			wait::WaitStatus::Signaled(_, signal, _) => {
				format!("Process exited with signal: {}", signal)
			}
			_ => format!("Process in unexpected status: {:?}", self),
		};
		Err(Error::new(ErrorKind::Other, message))
	}
}