cmd_lib_cf/
io.rs

1use os_pipe::*;
2use std::fs::File;
3use std::io::{Read, Result, Write};
4use std::process::Stdio;
5
6#[derive(Debug)]
7pub enum CmdIn {
8    Null,
9    File(File),
10    Pipe(PipeReader),
11}
12
13impl Read for CmdIn {
14    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
15        match self {
16            CmdIn::Null => Ok(0),
17            CmdIn::File(file) => file.read(buf),
18            CmdIn::Pipe(pipe) => pipe.read(buf),
19        }
20    }
21}
22
23impl From<CmdIn> for Stdio {
24    fn from(cmd_in: CmdIn) -> Stdio {
25        match cmd_in {
26            CmdIn::Null => Stdio::null(),
27            CmdIn::File(file) => Stdio::from(file),
28            CmdIn::Pipe(pipe) => Stdio::from(pipe),
29        }
30    }
31}
32
33#[derive(Debug)]
34pub enum CmdOut {
35    Null,
36    File(File),
37    Pipe(PipeWriter),
38}
39
40impl Write for CmdOut {
41    fn write(&mut self, buf: &[u8]) -> Result<usize> {
42        match self {
43            CmdOut::Null => Ok(buf.len()),
44            CmdOut::File(file) => file.write(buf),
45            CmdOut::Pipe(pipe) => pipe.write(buf),
46        }
47    }
48
49    fn flush(&mut self) -> Result<()> {
50        match self {
51            CmdOut::Null => Ok(()),
52            CmdOut::File(file) => file.flush(),
53            CmdOut::Pipe(pipe) => pipe.flush(),
54        }
55    }
56}
57
58impl CmdOut {
59    pub fn try_clone(&self) -> Result<Self> {
60        match self {
61            CmdOut::Null => Ok(CmdOut::Null),
62            CmdOut::File(file) => file.try_clone().map(CmdOut::File),
63            CmdOut::Pipe(pipe) => pipe.try_clone().map(CmdOut::Pipe),
64        }
65    }
66}
67
68impl From<CmdOut> for Stdio {
69    fn from(cmd_out: CmdOut) -> Stdio {
70        match cmd_out {
71            CmdOut::Null => Stdio::null(),
72            CmdOut::File(file) => Stdio::from(file),
73            CmdOut::Pipe(pipe) => Stdio::from(pipe),
74        }
75    }
76}