Skip to content

Commit 53b6b1f

Browse files
committed
Update custom command API signature
To allow spawning process with stdin/stdout redirection.
1 parent 787e35d commit 53b6b1f

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

macros/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ pub fn main(
4444
/// ```
4545
/// # use cmd_lib::*;
4646
/// fn my_cmd(env: &mut CmdEnv) -> CmdResult {
47-
/// let msg = format!("msg from foo(), args: {:?}\n", env.args());
48-
/// writeln!(env.stderr(), "{}", msg)?;
47+
/// let msg = format!("msg from foo(), args: {:?}", env.get_args());
48+
/// writeln!(env.stderr(), "{msg}")?;
4949
/// writeln!(env.stdout(), "bar")
5050
/// }
5151
///

src/io.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ impl From<CmdIn> for Stdio {
2929
}
3030
}
3131

32+
impl CmdIn {
33+
pub fn try_clone(&self) -> Result<Self> {
34+
match self {
35+
CmdIn::Null => Ok(CmdIn::Null),
36+
CmdIn::File(file) => file.try_clone().map(CmdIn::File),
37+
CmdIn::Pipe(pipe) => pipe.try_clone().map(CmdIn::Pipe),
38+
}
39+
}
40+
}
41+
3242
pub enum CmdOut {
3343
Null,
3444
File(File),

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ pub type FunResult = std::io::Result<String>;
377377
/// Return type for [`run_cmd!()`] macro.
378378
pub type CmdResult = std::io::Result<()>;
379379
pub use child::{CmdChildren, FunChildren};
380+
pub use io::{CmdIn, CmdOut};
380381
#[doc(hidden)]
381382
pub use log as inner_log;
382383
#[doc(hidden)]

src/process.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::collections::HashMap;
1010
use std::ffi::{OsStr, OsString};
1111
use std::fmt;
1212
use std::fs::{File, OpenOptions};
13-
use std::io::{Error, ErrorKind, Read, Result, Write};
13+
use std::io::{Error, ErrorKind, Result};
1414
use std::path::{Path, PathBuf};
1515
use std::process::Command;
1616
use std::sync::Mutex;
@@ -50,17 +50,17 @@ impl CmdEnv {
5050
}
5151

5252
/// Returns a new handle to the standard input for this command.
53-
pub fn stdin(&mut self) -> impl Read + '_ {
53+
pub fn stdin(&mut self) -> &mut CmdIn {
5454
&mut self.stdin
5555
}
5656

5757
/// Returns a new handle to the standard output for this command.
58-
pub fn stdout(&mut self) -> impl Write + '_ {
58+
pub fn stdout(&mut self) -> &mut CmdOut {
5959
&mut self.stdout
6060
}
6161

6262
/// Returns a new handle to the standard error for this command.
63-
pub fn stderr(&mut self) -> impl Write + '_ {
63+
pub fn stderr(&mut self) -> &mut CmdOut {
6464
&mut self.stderr
6565
}
6666
}

0 commit comments

Comments
 (0)