use std::ffi::OsStr;
use std::ops::{Deref, DerefMut};
use std::process::Command;
use crate::Process;
#[derive(Debug)]
pub struct Program {
cmd: Command,
stdio: Stdio,
}
#[derive(Clone, Debug, Copy)]
pub enum Stdio {
Inherit,
Null,
Pipe,
}
impl Program {
pub fn new<P: AsRef<OsStr>>(program: P) -> Self {
Self {
cmd: Command::new(program),
stdio: Stdio::Inherit,
}
}
pub fn stdio(mut self, stdio: Stdio) -> Self {
use std::process::Stdio as StdStdio;
match stdio {
Stdio::Inherit => {
self.cmd.stdin(StdStdio::inherit());
self.cmd.stdout(StdStdio::inherit());
self.cmd.stderr(StdStdio::inherit());
}
Stdio::Null => {
self.cmd.stdin(StdStdio::null());
self.cmd.stdout(StdStdio::null());
self.cmd.stderr(StdStdio::null());
}
Stdio::Pipe => {
self.cmd.stdin(StdStdio::piped());
self.cmd.stdout(StdStdio::piped());
self.cmd.stderr(StdStdio::piped());
}
}
self.stdio = stdio;
self
}
pub(crate) fn stdio_value(&self) -> Stdio {
self.stdio
}
pub(crate) fn command(&self) -> &Command {
&self.cmd
}
pub fn into_command(self) -> Command {
self.cmd
}
}
impl From<Command> for Program {
fn from(cmd: Command) -> Self {
Program {
cmd,
stdio: Stdio::Inherit,
}
}
}
impl From<&OsStr> for Program {
fn from(program: &OsStr) -> Self {
Program::new(program)
}
}
impl From<&str> for Program {
fn from(program: &str) -> Self {
Program::new(program)
}
}
impl From<Program> for Command {
fn from(program: Program) -> Self {
program.cmd
}
}
impl Deref for Program {
type Target = Command;
fn deref(&self) -> &Self::Target {
&self.cmd
}
}
impl DerefMut for Program {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.cmd
}
}
#[derive(Debug)]
#[allow(dead_code)]
pub struct Child {
pid: i32,
stdio: Stdio,
_priv: (),
}
impl Child {
pub(crate) fn new(process: Process, stdio: Stdio) -> Self {
Self {
pid: process.pid(),
stdio,
_priv: (),
}
}
}