Skip to content

Commit d9f868b

Browse files
committed
fix/any matching
Create a macros instead of the ABInterest solution. The macros basically just tries to pull the result of every match. And gives a end user a `match` like interface to cover all possible cases. Signed-off-by: Maxim Zhiburt <[email protected]>
1 parent 25453f2 commit d9f868b

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ pub mod session;
8282
pub mod reader;
8383

8484
pub use session::{spawn, spawn_bash, spawn_python, spawn_stream};
85-
pub use reader::{Until};
8685

8786
pub use ptyproc::{Command, PtyProcess, PtyReader, PtyWriter};
8887

src/reader.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,19 @@ impl<N:Needle> AsRef<N> for Until<N> {
184184
}
185185
}
186186

187+
#[macro_export]
188+
macro_rules! read_any {
189+
($reader: ident, $($needle:expr, $var:pat => $case:block)* _ => $eb:block) => {
190+
if false {}
191+
$( else if let Ok($var) = $reader.read_until(&$needle) {
192+
$case
193+
})*
194+
else {
195+
$eb
196+
}
197+
}
198+
}
199+
187200
/// Non blocking reader
188201
///
189202
/// Typically you'd need that to check for output of a process without blocking your thread.

src/session.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ impl<W: Write> StreamSession<W> {
134134
self.exp(&Str(needle.to_string()))
135135
}
136136
}
137+
138+
#[macro_export]
139+
macro_rules! exp_any {
140+
($session: ident, $($rest:tt)*) => {
141+
// $crate::read_any!(session.reader, $($rest)*)
142+
{
143+
let reader = &mut $session.reader;
144+
$crate::read_any!(reader, $($rest)*);
145+
}
146+
}
147+
}
148+
137149
/// Interact with a process with read/write/signals, etc.
138150
#[allow(dead_code)]
139151
pub struct PtySession {
@@ -434,7 +446,8 @@ pub fn spawn_stream<R: Read + Send + 'static, W: Write>(reader: R, writer: W, ti
434446
#[cfg(test)]
435447
mod tests {
436448
use super::*;
437-
use super::super::reader::{NBytes, Until, OrInterest};
449+
use super::super::reader::{NBytes};
450+
use crate::exp_any;
438451

439452
#[cfg(unix)]
440453
#[test]
@@ -510,13 +523,12 @@ mod tests {
510523
let mut p = spawn("cat", Some(1000)).expect("cannot run cat");
511524
p.send_line("Hi")?;
512525

513-
let until = Until(NBytes(3)).or(Str("Hi"));
526+
exp_any!(p,
527+
NBytes(3), res => { assert_eq!("Hi\r".to_string(), res) }
528+
Str("Hi"), _ => { assert!(false) }
529+
_ => { assert!(false, format!("unxpectedly, didn't find a match")) }
530+
);
514531

515-
match p.exp(until.as_ref()) {
516-
Ok(OrInterest::Lhs(s)) => assert_eq!("Hi\r".to_string(), s),
517-
Ok(OrInterest::Rhs(_)) => assert!(false),
518-
Err(e) => assert!(false, format!("got error: {}", e)),
519-
}
520532
Ok(())
521533
}()
522534
.unwrap_or_else(|e| panic!("test_expect_any failed: {}", e));
@@ -529,14 +541,13 @@ mod tests {
529541
let mut p = spawn("cat", Some(1000)).expect("cannot run cat");
530542
p.send_line("Hello World")?;
531543

532-
let until = Until(Str("Hi")).or(Str("World")).or(NBytes(3));
544+
exp_any!(p,
545+
Str("Hi"), _ => { assert!(false) }
546+
Str("World"), res => { assert_eq!("Hello ".to_string(), res) }
547+
NBytes(3), _ => { assert!(false) }
548+
_ => { assert!(false, format!("unxpectedly, didn't find a match")) }
549+
);
533550

534-
match p.exp(until.as_ref()) {
535-
Ok(OrInterest::Lhs(OrInterest::Lhs(_))) => assert!(false),
536-
Ok(OrInterest::Lhs(OrInterest::Rhs(s))) => assert_eq!("Hello ".to_string(), s),
537-
Ok(OrInterest::Rhs(_)) => assert!(false),
538-
Err(e) => assert!(false, format!("got error: {}", e)),
539-
}
540551
Ok(())
541552
}()
542553
.unwrap_or_else(|e| panic!("test_expect_any failed: {}", e));

0 commit comments

Comments
 (0)