Skip to content

Commit b2b6bb0

Browse files
authored
Merge pull request #169 from epage/api
fix!: Make it easier to evolve the API
2 parents 7e96abe + 029796a commit b2b6bb0

File tree

5 files changed

+22
-27
lines changed

5 files changed

+22
-27
lines changed

examples/repl.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,15 @@ use rexpect::session::PtyReplSession;
55
use rexpect::spawn;
66

77
fn ed_session() -> Result<PtyReplSession, Error> {
8-
let mut ed = PtyReplSession {
8+
let mut ed = PtyReplSession::new(spawn("/bin/ed -p '> '", Some(2000))?, "> ".to_owned())
99
// for `echo_on` you need to figure that out by trial and error.
1010
// For bash and python repl it is false
11-
echo_on: false,
12-
13-
// used for `wait_for_prompt()`
14-
prompt: "> ".to_owned(),
15-
pty_session: spawn("/bin/ed -p '> '", Some(2000))?,
11+
.echo_on(false)
1612
// command which is sent when the instance of this struct is dropped
1713
// in the below example this is not needed, but if you don't explicitly
1814
// exit a REPL then rexpect tries to send a SIGTERM and depending on the repl
1915
// this does not end the repl and would end up in an error
20-
quit_command: Some("Q".to_owned()),
21-
};
16+
.quit_command(Some("Q".to_owned()));
2217
ed.wait_for_prompt()?;
2318
Ok(ed)
2419
}

src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::time;
22

33
#[derive(Debug, thiserror::Error)]
4+
#[non_exhaustive]
45
pub enum Error {
56
#[error("EOF (End of File): Expected {:?} but got EOF after reading {:?} process terminated with {:?}", .expected, .got, .exit_code.as_ref().unwrap_or(&"unknown".to_owned()))]
67
EOF {

src/process.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use nix;
55
use nix::fcntl::{OFlag, open};
66
use nix::libc::STDERR_FILENO;
77
use nix::pty::{PtyMaster, grantpt, posix_openpt, unlockpt};
8+
use nix::sys::{signal, wait};
89
use nix::sys::{stat, termios};
910
use nix::unistd::{
1011
ForkResult, Pid, close, dup, dup2_stderr, dup2_stdin, dup2_stdout, fork, setsid,
@@ -17,7 +18,6 @@ use std::os::unix::process::CommandExt;
1718
use std::process::Command;
1819
use std::{thread, time};
1920

20-
pub use nix::sys::{signal, wait};
2121
pub use signal::Signal;
2222
pub use wait::WaitStatus;
2323

@@ -57,8 +57,8 @@ pub use wait::WaitStatus;
5757
/// # }
5858
/// ```
5959
pub struct PtyProcess {
60-
pub pty: PtyMaster,
61-
pub child_pid: Pid,
60+
pty: PtyMaster,
61+
pub(crate) child_pid: Pid,
6262
kill_timeout: Option<time::Duration>,
6363
}
6464

src/reader.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ pub struct Options {
1414
/// `None`: `read_until` is blocking forever. This is probably not what you want
1515
///
1616
/// `Some(millis)`: after millis milliseconds a timeout error is raised
17-
pub timeout_ms: Option<u64>,
17+
pub(crate) timeout_ms: Option<u64>,
1818
/// Whether to filter out escape codes, such as colors.
19-
pub strip_ansi_escape_codes: bool,
19+
pub(crate) strip_ansi_escape_codes: bool,
2020
}
2121

2222
impl Options {
@@ -214,6 +214,7 @@ impl NBReader {
214214
/// See [`NBReader::read_until`]
215215
///
216216
/// Note that when used with a tty the lines end with \r\n
217+
#[non_exhaustive]
217218
pub enum ReadUntil {
218219
/// Searches for string (use '\n'.`to_string()` to search for newline).
219220
///
@@ -268,7 +269,7 @@ impl fmt::Display for ReadUntil {
268269
/// Tuple with match positions:
269270
/// 1. position before match (0 in case of EOF and Nbytes)
270271
/// 2. position after match
271-
pub fn find(needle: &ReadUntil, buffer: &str, eof: bool) -> Option<(usize, usize)> {
272+
fn find(needle: &ReadUntil, buffer: &str, eof: bool) -> Option<(usize, usize)> {
272273
match needle {
273274
ReadUntil::String(s) => buffer.find(s).map(|pos| (pos, pos + s.len())),
274275
ReadUntil::Regex(pattern) => pattern.find(buffer).map(|mat| (mat.start(), mat.end())),

src/session.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use std::process::Command;
1212
use tempfile;
1313

1414
pub struct StreamSession<W: Write> {
15-
pub writer: LineWriter<W>,
16-
pub reader: NBReader,
15+
writer: LineWriter<W>,
16+
reader: NBReader,
1717
}
1818

1919
impl<W: Write> StreamSession<W> {
@@ -166,8 +166,8 @@ impl<W: Write> StreamSession<W> {
166166
/// Interact with a process with read/write/signals, etc.
167167
#[allow(dead_code)]
168168
pub struct PtySession {
169-
pub process: PtyProcess,
170-
pub stream: StreamSession<File>,
169+
process: PtyProcess,
170+
stream: StreamSession<File>,
171171
}
172172

173173
// make StreamSession's methods available directly
@@ -273,10 +273,10 @@ pub fn spawn_with_options(command: Command, options: Options) -> Result<PtySessi
273273
/// You have a prompt where a user inputs commands and the shell
274274
/// executes it and writes some output
275275
pub struct PtyReplSession {
276-
pub pty_session: PtySession,
277-
pub prompt: String,
278-
pub quit_command: Option<String>,
279-
pub echo_on: bool,
276+
pty_session: PtySession,
277+
prompt: String,
278+
quit_command: Option<String>,
279+
echo_on: bool,
280280
}
281281

282282
impl PtyReplSession {
@@ -484,17 +484,15 @@ pub fn spawn_stream<R: Read + Send + 'static, W: Write>(
484484
#[cfg(test)]
485485
mod tests {
486486
use super::*;
487+
use nix::sys::wait;
487488

488489
#[test]
489490
fn test_read_line() -> Result<(), Error> {
490491
let mut s = spawn("cat", Some(100000))?;
491492
s.send_line("hans")?;
492493
assert_eq!("hans", s.read_line()?);
493-
let should = crate::process::wait::WaitStatus::Signaled(
494-
s.process.child_pid,
495-
crate::process::signal::Signal::SIGTERM,
496-
false,
497-
);
494+
let should =
495+
wait::WaitStatus::Signaled(s.process.child_pid, crate::process::Signal::SIGTERM, false);
498496
assert_eq!(should, s.process.exit()?);
499497
Ok(())
500498
}

0 commit comments

Comments
 (0)