Skip to main content

running_process/
types.rs

1use std::path::PathBuf;
2
3use thiserror::Error;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum StreamKind {
7    Stdout,
8    Stderr,
9}
10
11impl StreamKind {
12    pub fn as_str(self) -> &'static str {
13        match self {
14            Self::Stdout => "stdout",
15            Self::Stderr => "stderr",
16        }
17    }
18}
19
20#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct StreamEvent {
22    pub stream: StreamKind,
23    pub line: Vec<u8>,
24}
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub enum ReadStatus<T> {
28    Line(T),
29    Timeout,
30    Eof,
31}
32
33#[derive(Debug, Error)]
34pub enum ProcessError {
35    #[error("process already started")]
36    AlreadyStarted,
37    #[error("process is not running")]
38    NotRunning,
39    #[error("process stdin is not available")]
40    StdinUnavailable,
41    #[error("failed to spawn process: {0}")]
42    Spawn(std::io::Error),
43    #[error("failed to read process output: {0}")]
44    Io(std::io::Error),
45    #[error("process timed out")]
46    Timeout,
47}
48
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct RunOutput {
51    /// Raw stdout bytes captured from the child.
52    pub stdout: Vec<u8>,
53    /// Raw stderr bytes captured from the child.
54    pub stderr: Vec<u8>,
55    /// Process exit code, with Unix signal exits represented as negative signal numbers.
56    pub exit_code: i32,
57}
58
59#[derive(Debug, Clone)]
60pub enum CommandSpec {
61    Shell(String),
62    Argv(Vec<String>),
63}
64
65#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66pub enum StdinMode {
67    Inherit,
68    Piped,
69    Null,
70}
71
72#[derive(Debug, Clone, Copy, PartialEq, Eq)]
73pub enum StderrMode {
74    Stdout,
75    Pipe,
76}
77
78#[derive(Debug, Clone)]
79pub struct ProcessConfig {
80    pub command: CommandSpec,
81    pub cwd: Option<PathBuf>,
82    pub env: Option<Vec<(String, String)>>,
83    pub capture: bool,
84    pub stderr_mode: StderrMode,
85    pub creationflags: Option<u32>,
86    pub create_process_group: bool,
87    pub stdin_mode: StdinMode,
88    pub nice: Option<i32>,
89}