chromiumoxide/
async_process.rs1use ::tokio::process;
4use std::ffi::OsStr;
5use std::pin::Pin;
6pub use std::process::{ExitStatus, Stdio};
7use std::task::{Context, Poll};
8
9#[derive(Debug)]
10pub struct Command {
11 inner: process::Command,
12}
13
14impl Command {
15 pub fn new<S: AsRef<OsStr>>(program: S) -> Self {
16 let mut inner = process::Command::new(program);
17 inner.kill_on_drop(true);
23 Self { inner }
24 }
25
26 pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Self {
27 self.inner.arg(arg);
28 self
29 }
30
31 pub fn args<I, S>(&mut self, args: I) -> &mut Self
32 where
33 I: IntoIterator<Item = S>,
34 S: AsRef<OsStr>,
35 {
36 self.inner.args(args);
37 self
38 }
39
40 pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Self
41 where
42 I: IntoIterator<Item = (K, V)>,
43 K: AsRef<OsStr>,
44 V: AsRef<OsStr>,
45 {
46 self.inner.envs(vars);
47 self
48 }
49
50 pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
51 self.inner.stderr(cfg);
52 self
53 }
54
55 pub fn spawn(&mut self) -> std::io::Result<Child> {
56 let inner = self.inner.spawn()?;
57 Ok(Child::new(inner))
58 }
59}
60
61#[derive(Debug)]
62pub struct Child {
63 pub stderr: Option<ChildStderr>,
64 pub inner: process::Child,
65}
66
67impl Child {
72 fn new(mut inner: process::Child) -> Self {
73 let stderr = inner.stderr.take();
74 Self {
75 inner,
76 stderr: stderr.map(|inner| ChildStderr { inner }),
77 }
78 }
79
80 pub async fn kill(&mut self) -> std::io::Result<()> {
83 self.inner.kill().await
84 }
85
86 pub async fn wait(&mut self) -> std::io::Result<ExitStatus> {
88 self.inner.wait().await
89 }
90
91 pub fn try_wait(&mut self) -> std::io::Result<Option<ExitStatus>> {
93 self.inner.try_wait()
94 }
95
96 pub fn as_mut_inner(&mut self) -> &mut process::Child {
100 &mut self.inner
101 }
102
103 pub fn into_inner(self) -> process::Child {
105 let mut inner = self.inner;
106 inner.stderr = self.stderr.map(ChildStderr::into_inner);
107 inner
108 }
109}
110
111#[derive(Debug)]
112pub struct ChildStderr {
113 pub inner: process::ChildStderr,
114}
115
116impl ChildStderr {
117 pub fn into_inner(self) -> process::ChildStderr {
118 self.inner
119 }
120}
121
122impl futures::AsyncRead for ChildStderr {
123 fn poll_read(
124 mut self: Pin<&mut Self>,
125 cx: &mut Context<'_>,
126 buf: &mut [u8],
127 ) -> Poll<std::io::Result<usize>> {
128 let mut buf = tokio::io::ReadBuf::new(buf);
129 futures::ready!(tokio::io::AsyncRead::poll_read(
130 Pin::new(&mut self.inner),
131 cx,
132 &mut buf
133 ))?;
134 Poll::Ready(Ok(buf.filled().len()))
135 }
136}