Skip to main content

sandlock_core/
result.rs

1/// The result of running a sandboxed process.
2#[derive(Debug, Clone)]
3pub struct RunResult {
4    pub exit_status: ExitStatus,
5    pub stdout: Option<Vec<u8>>,
6    pub stderr: Option<Vec<u8>>,
7}
8
9impl RunResult {
10    pub fn success(&self) -> bool {
11        matches!(self.exit_status, ExitStatus::Code(0))
12    }
13
14    pub fn code(&self) -> Option<i32> {
15        match self.exit_status {
16            ExitStatus::Code(c) => Some(c),
17            _ => None,
18        }
19    }
20
21    pub fn stdout_str(&self) -> Option<&str> {
22        self.stdout
23            .as_ref()
24            .and_then(|b| std::str::from_utf8(b).ok().map(|s| s.trim_end_matches('\n')))
25    }
26
27    pub fn timeout() -> Self {
28        RunResult {
29            exit_status: ExitStatus::Timeout,
30            stdout: None,
31            stderr: None,
32        }
33    }
34
35    pub fn stderr_str(&self) -> Option<&str> {
36        self.stderr
37            .as_ref()
38            .and_then(|b| std::str::from_utf8(b).ok().map(|s| s.trim_end_matches('\n')))
39    }
40}
41
42/// How a sandboxed process exited.
43#[derive(Debug, Clone, PartialEq, Eq)]
44pub enum ExitStatus {
45    Code(i32),
46    Signal(i32),
47    Killed,
48    Timeout,
49}