referrerpolicy=no-referrer-when-downgrade
pub async fn wait_for_stream_pattern_match<R>(
    stream: R,
    re: Regex,
) -> Result<(), String>
where R: AsyncRead + Unpin,
Expand description

Takes a readable tokio stream (e.g. from a child process ChildStderr or ChildStdout) and a Regex pattern, and checks each line against the given pattern as it is produced. The function returns OK(()) as soon as a line matching the pattern is found, or an Err if the stream ends without any lines matching the pattern.

§Arguments

  • child_stream - An async tokio stream, e.g. from a child process ChildStderr or ChildStdout.
  • re - A Regex pattern to search for in the stream.

§Returns

  • Ok(()) if a line matching the pattern is found.
  • Err(String) if the stream ends without any lines matching the pattern.

§Examples

use regex::Regex;
use tokio::process::Command;
use tokio::io::AsyncRead;

let child = Command::new("some-command").stderr(std::process::Stdio::piped()).spawn().unwrap();
let stderr = child.stderr.unwrap();
let re = Regex::new("error:").unwrap();

match wait_for_pattern_match_in_stream(stderr, re).await {
    Ok(()) => println!("Error found in stderr"),
    Err(e) => println!("Error: {}", e),
}