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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use crate::io::merge::MergedAsyncReader;
use crate::io::Pipe;
use std::{ffi::OsStr, process::Stdio, result};
use thiserror::*;
use tokio::process::*;

#[derive(Error, Debug)]
pub enum ProcessPipeError {
    #[error("stdin not set")]
    StdinNotSet,
    #[error("stdout not set")]
    StdoutNotSet,
    #[error("stderr not set")]
    StdErrNotSet,
    #[error("io error")]
    IoError(#[from] tokio::io::Error),
}

pub type ProcessPipeResult<T> = result::Result<T, ProcessPipeError>;

pub type ProcessPipe = Pipe<MergedAsyncReader<ChildStdout, ChildStderr>, ChildStdin>;
pub type StdoutPipe = Pipe<ChildStdout, ChildStdin>;
pub type StderrPipe = Pipe<ChildStderr, ChildStdin>;

impl ProcessPipe {
    pub async fn from_app<S: AsRef<OsStr>>(program: S) -> ProcessPipeResult<Self> {
        let command = Command::new(program);
        Self::spawn_command(command)
    }

    pub async fn from_app_args<S: AsRef<OsStr>, I: IntoIterator<Item = S>>(
        program: S,
        args: I,
    ) -> ProcessPipeResult<Self> {
        let mut command = Command::new(program);
        let _ = command.args(args);
        Self::spawn_command(command)
    }

    pub fn spawn_command(mut value: Command) -> ProcessPipeResult<Self> {
        let process = value
            .stdout(Stdio::piped())
            .stdin(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()?;

        process.try_into()
    }
}

impl StdoutPipe {
    pub async fn from_app<S: AsRef<OsStr>>(program: S) -> ProcessPipeResult<Self> {
        let command = Command::new(program);
        Self::spawn_command(command)
    }

    pub async fn from_app_args<S: AsRef<OsStr>, I: IntoIterator<Item = S>>(
        program: S,
        args: I,
    ) -> ProcessPipeResult<Self> {
        let mut command = Command::new(program);
        let _ = command.args(args);
        Self::spawn_command(command)
    }

    pub fn spawn_command(mut value: Command) -> ProcessPipeResult<Self> {
        let process = value.stdout(Stdio::piped()).stdin(Stdio::piped()).spawn()?;

        let stdin = process.stdin.ok_or(ProcessPipeError::StdinNotSet)?;
        let stdout = process.stdout.ok_or(ProcessPipeError::StdoutNotSet)?;
        Ok((stdin, stdout).into())
    }
}

impl StderrPipe {
    pub async fn from_app<S: AsRef<OsStr>>(program: S) -> ProcessPipeResult<Self> {
        let command = Command::new(program);
        Self::spawn_command(command)
    }

    pub async fn from_app_args<S: AsRef<OsStr>, I: IntoIterator<Item = S>>(
        program: S,
        args: I,
    ) -> ProcessPipeResult<Self> {
        let mut command = Command::new(program);
        let _ = command.args(args);
        Self::spawn_command(command)
    }

    pub fn spawn_command(mut value: Command) -> ProcessPipeResult<Self> {
        let process = value.stderr(Stdio::piped()).stdin(Stdio::piped()).spawn()?;

        let stdin = process.stdin.ok_or(ProcessPipeError::StdinNotSet)?;
        let stderr = process.stderr.ok_or(ProcessPipeError::StdErrNotSet)?;
        Ok((stdin, stderr).into())
    }
}

impl TryFrom<Child> for ProcessPipe {
    type Error = ProcessPipeError;

    fn try_from(value: Child) -> ProcessPipeResult<Self> {
        let stdin = value.stdin.ok_or(ProcessPipeError::StdinNotSet)?;
        let stdout = value.stdout.ok_or(ProcessPipeError::StdoutNotSet)?;
        let stderr = value.stderr.ok_or(ProcessPipeError::StdErrNotSet)?;
        Ok((stdin, stdout, stderr).into())
    }
}

impl From<(ChildStdin, ChildStdout, ChildStderr)> for ProcessPipe {
    fn from(value: (ChildStdin, ChildStdout, ChildStderr)) -> Self {
        let (stdin, stdout, stderr) = value;
        let read_stream = MergedAsyncReader {
            stream0: stdout,
            stream1: stderr,
        };
        let write_stream = stdin;
        Pipe::new(read_stream, write_stream)
    }
}

impl From<(ChildStdin, ChildStdout)> for StdoutPipe {
    fn from(value: (ChildStdin, ChildStdout)) -> Self {
        let (stdin, stdout) = value;
        let read_stream = stdout;
        let write_stream = stdin;
        Pipe::new(read_stream, write_stream)
    }
}

impl From<(ChildStdin, ChildStderr)> for StderrPipe {
    fn from(value: (ChildStdin, ChildStderr)) -> Self {
        let (stdin, stderr) = value;
        let read_stream = stderr;
        let write_stream = stdin;
        Pipe::new(read_stream, write_stream)
    }
}