Struct reckon::base::Process
[−]
[src]
pub struct Process { /* fields omitted */ }
Provids necessary lifetime management for subprocess resources.
Methods
impl Process
[src]
fn new(exe: &str, args: Vec<&str>) -> Result<Process, Error>
Starts a subprocess for later interaction.
Examples
Process::new("true", vec![]).expect("Your UNIX is broken.");
Processes that cannot be invoked will denote this in a way that can be handled properly:
Process::new("nope-i-don't-exist", vec![]).expect("This should fail.");
fn emit(&mut self, data: &str) -> Result<(), Error>
Writes some data to the subprocess.
This doesn't do any special processing of the data; it just shovels it onto the subprocess as fast as it can, and propagates any errors that occurred during this operation.
Of note is the fact that reckon
assumes that strings are being sent betwixt processes;
future support for raw bytes might come if needed.
let mut p = Process::new("cat", vec![]).unwrap(); p.emit("Hello\n");
fn expect(
&mut self,
needles: Vec<&str>,
timeout: Duration
) -> Result<(usize, String), Error>
&mut self,
needles: Vec<&str>,
timeout: Duration
) -> Result<(usize, String), Error>
Searches for some marker in data from the subprocess.
This follows a similar format to other libexpect
-alikes; you can specify a set of regular
expressions to try and match on, and it will return back which one of them matched first.
Seemingly unique to this particular implementation is that it always returns the data that matched, for later processing/matching by callers, without having to keep a buffer around after the call.
Examples
let mut p = Process::new("bash", vec!["test.sh"]).unwrap(); let (m, _) = p.expect(vec!["Hello"], Duration::from_secs(1)).unwrap();
The matcher supports timeouts, as well:
p.emit("nope\n"); p.expect(vec![r"!"], Duration::from_secs(1)).expect("This should fail.");
By chaining this and emit, you can build complex interactions with subprocesses:
p.emit("test\n").unwrap(); p.expect(vec!["test script"], Duration::from_secs(1)).unwrap();
Note the need to explicitly wait for the test.sh
prompt again - this is because
reckon literally reads all data that comes from the program, and skips nothing.
This is somewhat of a departure from how programs like pexpect work, given that they
feed a buffer continuously from a background thread, and match/clear that for expect()
calls.
p.expect(vec!["!"], Duration::from_secs(1)).unwrap(); p.emit("commit synchronize\n").unwrap();
// Multiple matches are possible, and when this happens, the first one to match will // be returned, and the input stream will be stopped.
let (m, _) = p.expect(vec!["!", "no route to re1"], Duration::from_secs(1)).unwrap(); assert_eq!(m, 1);
Trait Implementations
impl Drop for Process
[src]
fn drop(&mut self)
Destructor to automatically clean up the subprocess.
This prevents the child process sticking around when the parent dies, which apparently can
happen when you capture all std{io,err,out}
pipes.