use {
crate::*,
lazy_regex::*,
};
#[cfg(not(windows))]
const CSI_ERROR_BODY: &str = CSI_BOLD;
#[cfg(windows)]
const CSI_ERROR_BODY: &str = CSI_BOLD_WHITE;
#[derive(Debug, Clone)]
pub struct LineAnalysis {
pub line_type: LineType,
pub key: Option<String>,
}
impl From<&CommandOutputLine> for LineAnalysis {
fn from(cmd_line: &CommandOutputLine) -> Self {
let content = &cmd_line.content;
let mut key = None;
let line_type = if cmd_line.content.is_blank() {
LineType::Normal
} else if let Some(content) = cmd_line.content.if_unstyled() {
if let Some((k, r)) = as_test_result(content) {
key = Some(k.to_string());
LineType::TestResult(r)
} else if let Some(k) = as_fail_result_title(content) {
key = Some(k.to_string());
LineType::Title(Kind::TestFail)
} else if regex_is_match!("^failures:$", content) {
LineType::Title(Kind::Sum)
} else if regex_is_match!("^note: run with `RUST_BACKTRACE=", content) {
LineType::BacktraceSuggestion
} else {
LineType::Normal
}
} else {
if let (Some(title), Some(body)) = (content.strings.get(0), content.strings.get(1)) {
match (
title.csi.as_ref(),
title.raw.as_ref(),
body.csi.as_ref(),
body.raw.as_ref(),
) {
(CSI_BOLD_RED, "error", CSI_ERROR_BODY, body_raw)
if body_raw.starts_with(": aborting due to") =>
{
LineType::Title(Kind::Sum)
}
(CSI_BOLD_RED, title_raw, CSI_ERROR_BODY, _)
if title_raw.starts_with("error") =>
{
LineType::Title(Kind::Error)
}
#[cfg(not(windows))]
(CSI_BOLD_YELLOW, "warning", _, body_raw) => {
determine_warning_type(body_raw, content)
}
#[cfg(windows)]
(CSI_BOLD_YELLOW | CSI_BOLD_4BIT_YELLOW, "warning", _, body_raw) => {
determine_warning_type(body_raw, content)
}
("", title_raw, CSI_BOLD_BLUE, "--> ") if is_spaces(title_raw) => {
LineType::Location
}
("", k, CSI_BOLD_RED | CSI_RED, "FAILED") if content.strings.len() == 2 => {
if let Some(k) = as_test_name(k) {
key = Some(k.to_string());
LineType::TestResult(false)
} else {
LineType::Normal
}
}
("", k, CSI_GREEN, "ok") => {
if let Some(k) = as_test_name(k) {
key = Some(k.to_string());
LineType::TestResult(true)
} else {
LineType::Normal
}
}
_ => LineType::Normal,
}
} else {
LineType::Normal }
};
LineAnalysis { line_type, key }
}
}
fn determine_warning_type(
body_raw: &str,
content: &TLine,
) -> LineType {
if is_n_warnings_emitted(body_raw)
|| is_generated_n_warnings(&content.strings)
|| is_build_failed(content.strings.get(2))
{
LineType::Title(Kind::Sum)
} else {
LineType::Title(Kind::Warning)
}
}
fn is_spaces(s: &str) -> bool {
s.chars().all(|c| c.is_ascii_whitespace())
}
fn is_n_warnings_emitted(s: &str) -> bool {
regex_is_match!(r#"^: \d+ warnings? emitted"#, s)
}
fn is_generated_n_warnings(ts: &[TString]) -> bool {
ts.iter()
.any(|ts| regex_is_match!(r#"generated \d+ warnings?"#, &ts.raw))
}
fn is_build_failed(ts: Option<&TString>) -> bool {
ts.map_or(false, |ts| regex_is_match!(r#"^\s*build failed"#, &ts.raw))
}
fn as_test_name(s: &str) -> Option<&str> {
regex_captures!(
r#"^test\s+(.+?)(?: - should panic\s*)?(?: - compile\s*)?\s+...\s*$"#,
s
)
.map(|(_, key)| key)
}
fn as_test_result(s: &str) -> Option<(&str, bool)> {
regex_captures!(
r#"^test\s+(.+?)(?: - should panic\s*)?(?: - compile\s*)?\s+...\s+(\w+)$"#,
s
)
.and_then(|(_, key, outcome)| match outcome {
"ok" => Some((key, true)),
"FAILED" => Some((key, false)),
other => {
warn!("unrecognized doctest outcome: {:?}", other);
None
}
})
}
fn as_fail_result_title(s: &str) -> Option<&str> {
regex_captures!(r#"^---- (.+) stdout ----$"#, s).map(|(_, key)| key)
}