Macro expectrl::check

source ·
macro_rules! check {
    (@check ($($tokens:tt)*) ($session:expr)) => { ... };
    (@check ($session:expr, $($tokens:tt)*) ()) => { ... };
    (@check ($session:expr, $($tokens:tt)*) ($session2:expr)) => { ... };
    (@check ($($tokens:tt)*) ()) => { ... };
    (@check () ($session:expr)) => { ... };
    (@case $session:expr, ($var:tt = $exp:expr => $body:tt, $($tail:tt)*), ($($head:tt)*), ($($default:tt)*)) => { ... };
    (@case $session:expr, ($var:tt = $exp:expr => $body:tt $($tail:tt)*), ($($head:tt)*), ($($default:tt)*)) => { ... };
    (@case $session:expr, (default => $($tail:tt)*), ($($head:tt)*), ($($default:tt)+)) => { ... };
    (@case $session:expr, (default => $body:tt, $($tail:tt)*), ($($head:tt)*), ()) => { ... };
    (@case $session:expr, (default => $body:tt $($tail:tt)*), ($($head:tt)*), ()) => { ... };
    (@case $session:expr, (), ($($head:tt)*), ()) => { ... };
    (@case $session:expr, (), ($($tail:tt)*), ($($default:tt)*)) => { ... };
    (@branch $session:expr, ($var:tt = $exp:expr => $body:tt, $($tail:tt)*), ($($default:tt)*)) => { ... };
    (@branch $session:expr, (), ($default:tt)) => { ... };
    (@branch $session:expr, ($($tail:tt)*), ($($default:tt)*)) => { ... };
    ($($tokens:tt)*) => { ... };
}
Expand description

Check macros provides a convient way to check if things are available in a stream of a process.

It falls into a corresponding branch when a pattern was matched. It runs checks from top to bottom. It doesn’t wait until any of them be available. If you want to wait until any of the input available you must use your own approach for example putting it in a loop.

You can specify a default branch which will be called if nothing was matched.

The macros levareges crate::Session::check function, so its just made for convience.

Example

loop {
    expectrl::check!{
        &mut session,
        world = "\r" => {
            // handle end of line
        },
        _ = "Hello World" => {
            // handle Hello World
        },
        default => {
            // handle no matches
        },
    }
    .unwrap();
}