use {
crate::*,
anyhow::*,
std::process::ExitStatus,
};
pub enum CommandResult {
Report(Report),
Failure(Failure),
None,
}
impl CommandResult {
pub fn new(lines: Vec<CommandOutputLine>, exit_status: Option<ExitStatus>) -> Result<Self> {
let error_code = exit_status.and_then(|s| s.code()).filter(|&c| c != 0);
let mut report = Report::from_lines(&lines)?;
if let Some(error_code) = error_code {
if report.stats.errors + report.stats.test_fails == 0 {
return Ok(Self::Failure(Failure { error_code, lines }));
}
}
report.cmd_lines = lines; Ok(Self::Report(report))
}
pub fn report(&self) -> Option<&Report> {
match self {
Self::Report(report) => Some(report),
_ => None,
}
}
pub fn is_success(&self) -> bool {
match self {
Self::Report(report) => {
report.stats.errors + report.stats.warnings + report.stats.test_fails == 0
}
_ => false,
}
}
pub fn reverse(&mut self) {
match self {
Self::Report(report) => {
report.reverse();
}
Self::Failure(failure) => {
failure.lines.reverse();
}
Self::None => {}
}
}
pub fn lines_len(&self) -> usize {
match self {
Self::Report(report) => report.lines.len(),
Self::Failure(failure) => failure.lines.len(),
Self::None => 0,
}
}
}