Parses a diagnostic header line: `error[E0308]: mismatched types` `warning: unused variable` `error: useless conversion ...`
(line: &str)
| 94 | /// `warning: unused variable` |
| 95 | /// `error: useless conversion ...` |
| 96 | fn parse_header(line: &str) -> Option<(Severity, Option<String>, String)> { |
| 97 | // ANSI escapes can appear when cargo is run with `--color=always`; strip |
| 98 | // a leading reset sequence if present. We don't bother with full ANSI |
| 99 | // stripping — the typical input is plain text. |
| 100 | let line = line.trim_start_matches("\u{1b}[0m"); |
| 101 | |
| 102 | // Find the first colon. Severity is everything before it (optionally |
| 103 | // followed by `[CODE]`). Message is everything after. |
| 104 | let (head, rest) = line.split_once(": ")?; |
| 105 | let (sev_token, code) = if let Some(idx) = head.find('[') { |
| 106 | if !head.ends_with(']') { |
| 107 | return None; |
| 108 | } |
| 109 | let sev = &head[..idx]; |
| 110 | let code = &head[idx + 1..head.len() - 1]; |
| 111 | (sev, Some(code.to_string())) |
| 112 | } else { |
| 113 | (head, None) |
| 114 | }; |
| 115 | let severity = Severity::parse(sev_token.trim())?; |
| 116 | Some((severity, code, rest.trim().to_string())) |
| 117 | } |
| 118 | |
| 119 | /// Parses a primary-span line: |
| 120 | /// ` --> src/foo.rs:42:10` |
no test coverage detected