bawawa/
control.rs

1use crate::{Capture, Command, Result, SendStdin};
2use tokio_codec::{Decoder, Encoder, FramedRead, FramedWrite};
3use tokio_process::{ChildStderr, ChildStdin, ChildStdout};
4
5/// [`Process`] control trait, access Program ID, the command line or kill the
6/// running process
7///
8/// [`Process`]: ./struct.Process.html
9pub trait Control: Sized {
10    /// access the underlying command settings
11    fn command(&self) -> &Command;
12
13    /// retrieve the Process ID of the given running program.
14    fn id(&self) -> u32;
15
16    /// force the process to finish
17    ///
18    /// this is equivalent to `SIGKILL` on unix platform
19    fn kill(&mut self) -> Result<()>;
20}
21
22/// Access the standard input of a running [`Process`]
23///
24/// [`Process`]: ./struct.Process.html
25pub trait StandardInput<'a>: Control + 'a {
26    /// get access to the standard input so we can send in data
27    ///
28    fn standard_input(&mut self) -> &mut ChildStdin;
29
30    #[inline]
31    fn framed_stdin<E, Item>(&mut self, encoder: E) -> FramedWrite<&mut ChildStdin, E>
32    where
33        E: Encoder<Item = Item>,
34    {
35        FramedWrite::new(self.standard_input(), encoder)
36    }
37
38    #[inline]
39    fn send_stdin<E, Item>(self, encoder: E) -> SendStdin<'a, Self, E, Item>
40    where
41        E: Encoder<Item = Item>,
42    {
43        SendStdin::new(self, encoder)
44    }
45}
46
47/// Access the standard output of a running [`Process`]
48///
49/// [`Process`]: ./struct.Process.html
50pub trait StandardOutput<'a>: Control + 'a {
51    /// get access to the standard output
52    fn standard_output(&mut self) -> &mut ChildStdout;
53
54    #[inline]
55    fn framed_stdout<D, Item>(&mut self, decoder: D) -> FramedRead<&mut ChildStdout, D>
56    where
57        D: Decoder<Item = Item>,
58    {
59        FramedRead::new(self.standard_output(), decoder)
60    }
61
62    #[inline]
63    fn capture_stdout<D, Item>(self, decoder: D) -> Capture<'a, Self, D, ChildStdout, Item>
64    where
65        D: Decoder<Item = Item>,
66    {
67        Capture::new_stdout(self, decoder)
68    }
69}
70
71/// Access the standard error output of a running [`Process`]
72///
73/// [`Process`]: ./struct.Process.html
74pub trait StandardError<'a>: Control + 'a {
75    /// get access to the standard output
76    fn standard_error(&mut self) -> &mut ChildStderr;
77
78    #[inline]
79    fn framed_stderr<D, Item>(&mut self, decoder: D) -> FramedRead<&mut ChildStderr, D>
80    where
81        D: Decoder<Item = Item>,
82    {
83        FramedRead::new(self.standard_error(), decoder)
84    }
85
86    #[inline]
87    fn capture_stderr<D, Item>(self, decoder: D) -> Capture<'a, Self, D, ChildStderr, Item>
88    where
89        D: Decoder<Item = Item>,
90    {
91        Capture::new_stderr(self, decoder)
92    }
93}