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,
13}
14
15impl Command {
16 pub fn new<S: AsRef<OsStr>>(program: S) -> Self {
17 let mut inner = process::Command::new(program);
18 inner.kill_on_drop(true);
24 Self { inner }
25 }
26
27 pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Self {
28 self.inner.arg(arg);
29 self
30 }
31
32 pub fn args<I, S>(&mut self, args: I) -> &mut Self
33 where
34 I: IntoIterator<Item = S>,
35 S: AsRef<OsStr>,
36 {
37 self.inner.args(args);
38 self
39 }
40
41 pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Self
42 where
43 I: IntoIterator<Item = (K, V)>,
44 K: AsRef<OsStr>,
45 V: AsRef<OsStr>,
46 {
47 self.inner.envs(vars);
48 self
49 }
50
51 pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
52 self.inner.stderr(cfg);
53 self
54 }
55
56 pub fn spawn(&mut self) -> std::io::Result<Child> {
57 let inner = self.inner.spawn()?;
58 Ok(Child::new(inner))
59 }
60}
61
62#[derive(Debug)]
63pub struct Child {
64 pub stderr: Option<ChildStderr>,
66 pub inner: process::Child,
68}
69
70impl Child {
75 fn new(mut inner: process::Child) -> Self {
76 let stderr = inner.stderr.take();
77 Self {
78 inner,
79 stderr: stderr.map(|inner| ChildStderr { inner }),
80 }
81 }
82
83 pub async fn kill(&mut self) -> std::io::Result<()> {
86 self.inner.kill().await
87 }
88
89 pub async fn wait(&mut self) -> std::io::Result<ExitStatus> {
91 self.inner.wait().await
92 }
93
94 pub fn try_wait(&mut self) -> std::io::Result<Option<ExitStatus>> {
96 self.inner.try_wait()
97 }
98
99 pub fn as_mut_inner(&mut self) -> &mut process::Child {
103 &mut self.inner
104 }
105
106 pub fn into_inner(self) -> process::Child {
108 let mut inner = self.inner;
109 inner.stderr = self.stderr.map(ChildStderr::into_inner);
110 inner
111 }
112}
113
114#[derive(Debug)]
115pub struct ChildStderr {
116 pub inner: process::ChildStderr,
117}
118
119impl ChildStderr {
120 pub fn into_inner(self) -> process::ChildStderr {
121 self.inner
122 }
123}
124
125impl futures::AsyncRead for ChildStderr {
126 fn poll_read(
127 mut self: Pin<&mut Self>,
128 cx: &mut Context<'_>,
129 buf: &mut [u8],
130 ) -> Poll<std::io::Result<usize>> {
131 let mut buf = tokio::io::ReadBuf::new(buf);
132 futures::ready!(tokio::io::AsyncRead::poll_read(
133 Pin::new(&mut self.inner),
134 cx,
135 &mut buf
136 ))?;
137 Poll::Ready(Ok(buf.filled().len()))
138 }
139}