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>,
65 pub inner: process::Child,
66}
67
68impl Child {
73 fn new(mut inner: process::Child) -> Self {
74 let stderr = inner.stderr.take();
75 Self {
76 inner,
77 stderr: stderr.map(|inner| ChildStderr { inner }),
78 }
79 }
80
81 pub async fn kill(&mut self) -> std::io::Result<()> {
84 self.inner.kill().await
85 }
86
87 pub async fn wait(&mut self) -> std::io::Result<ExitStatus> {
89 self.inner.wait().await
90 }
91
92 pub fn try_wait(&mut self) -> std::io::Result<Option<ExitStatus>> {
94 self.inner.try_wait()
95 }
96
97 pub fn as_mut_inner(&mut self) -> &mut process::Child {
101 &mut self.inner
102 }
103
104 pub fn into_inner(self) -> process::Child {
106 let mut inner = self.inner;
107 inner.stderr = self.stderr.map(ChildStderr::into_inner);
108 inner
109 }
110}
111
112#[derive(Debug)]
113pub struct ChildStderr {
114 pub inner: process::ChildStderr,
115}
116
117impl ChildStderr {
118 pub fn into_inner(self) -> process::ChildStderr {
119 self.inner
120 }
121}
122
123impl futures::AsyncRead for ChildStderr {
124 fn poll_read(
125 mut self: Pin<&mut Self>,
126 cx: &mut Context<'_>,
127 buf: &mut [u8],
128 ) -> Poll<std::io::Result<usize>> {
129 let mut buf = tokio::io::ReadBuf::new(buf);
130 futures::ready!(tokio::io::AsyncRead::poll_read(
131 Pin::new(&mut self.inner),
132 cx,
133 &mut buf
134 ))?;
135 Poll::Ready(Ok(buf.filled().len()))
136 }
137}