use std::borrow::Cow;
#[derive(Debug)]
pub struct OutputMatcher<'a> {
stdout: Cow<'a, str>,
stderr: Cow<'a, str>,
status: std::process::ExitStatus,
}
impl<'a> OutputMatcher<'a> {
pub fn new(output: &'a std::process::Output) -> Self {
Self {
stdout: String::from_utf8_lossy(&output.stdout),
stderr: String::from_utf8_lossy(&output.stderr),
status: output.status,
}
}
fn stdout(&self) -> &str {
self.stdout.trim()
}
fn stderr(&self) -> &str {
self.stderr.trim()
}
pub fn starts_with(&self, pat: &str) -> bool {
self.stdout().starts_with(pat) || self.stderr().starts_with(pat)
}
pub fn contains(&self, pat: &str) -> bool {
self.stdout().contains(pat) || self.stderr().contains(pat)
}
pub fn ends_with(&self, pat: &str) -> bool {
self.stdout().ends_with(pat) || self.stderr().ends_with(pat)
}
pub fn exit_code(&self) -> Option<i32> {
self.status.code()
}
pub fn is_empty(&self) -> bool {
self.stdout().is_empty() && self.stderr().is_empty()
}
}
impl<'x> From<&'x std::process::Output> for OutputMatcher<'x> {
fn from(value: &'x std::process::Output) -> Self {
Self::new(value)
}
}