use crate::compress::generic::GenericCompressor;
use crate::compress::{Compressor, Specificity};
pub struct BunCompressor;
const MAX_FAILURES: usize = 25;
impl Compressor for BunCompressor {
fn specificity(&self) -> Specificity {
Specificity::PackageManager
}
fn matches(&self, command: &str) -> bool {
command
.split_whitespace()
.next()
.is_some_and(|head| head == "bun")
}
fn compress(&self, command: &str, output: &str) -> String {
match bun_subcommand(command).as_deref() {
Some("install" | "add" | "remove") => compress_package(output),
Some("test") => compress_test(output),
Some("run") => GenericCompressor::compress_output(output),
Some("build") => compress_build(output),
_ => GenericCompressor::compress_output(output),
}
}
}
fn bun_subcommand(command: &str) -> Option<String> {
command
.split_whitespace()
.skip_while(|token| *token != "bun")
.skip(1)
.find(|token| !token.starts_with('-'))
.map(ToString::to_string)
}
fn compress_package(output: &str) -> String {
let mut result = Vec::new();
for line in output.lines() {
if is_bun_progress(line) {
continue;
}
let trimmed = line.trim_start();
if trimmed.contains("packages installed")
|| trimmed.contains("package installed")
|| trimmed.starts_with("error:")
|| trimmed.starts_with("bun install error:")
|| trimmed.starts_with("Saved lockfile")
{
result.push(line.to_string());
}
}
trim_trailing_lines(&result.join("\n"))
}
fn compress_build(output: &str) -> String {
let mut result = Vec::new();
let mut timing_seen = 0usize;
let mut timing_omitted = 0usize;
for line in output.lines() {
if is_timing_line(line) {
timing_seen += 1;
if timing_seen > 10 {
timing_omitted += 1;
continue;
}
}
result.push(line.to_string());
}
if timing_omitted > 0 {
result.push(format!("... and {timing_omitted} more timing lines"));
}
trim_trailing_lines(&result.join("\n"))
}
fn compress_test(output: &str) -> String {
let lines: Vec<&str> = output.lines().collect();
if lines.is_empty() {
return output.to_string();
}
let has_failures = lines.iter().any(|line| is_bun_test_fail_marker(line));
if !has_failures {
return compress_test_pass_only(&lines);
}
let mut result: Vec<String> = Vec::new();
let mut failures_kept = 0usize;
let mut failures_dropped = 0usize;
let mut index = 0usize;
let mut saw_ran_summary = false;
while index < lines.len() {
let line = lines[index];
if saw_ran_summary {
result.push(line.to_string());
index += 1;
continue;
}
if is_bun_test_header(line) {
result.push(line.to_string());
index += 1;
continue;
}
if is_file_section_header(line) {
let next_fail = next_index(&lines, index + 1, is_bun_test_fail_marker);
let next_section = next_index(&lines, index + 1, |l| {
is_file_section_header(l) || is_summary_line(l)
});
let keep_section = match (next_fail, next_section) {
(Some(fi), Some(si)) => fi < si,
(Some(_), None) => true,
(None, _) => false,
};
if keep_section {
result.push(line.to_string());
}
index += 1;
continue;
}
if is_summary_line(line) {
result.push(line.to_string());
if is_ran_summary_line(line) {
saw_ran_summary = true;
}
index += 1;
continue;
}
if is_bun_test_error_start(line) || is_bun_test_code_pointer(line) {
let block_start = index;
let mut block_end = index;
while block_end < lines.len() {
if is_bun_test_fail_marker(lines[block_end]) {
block_end += 1;
break;
}
block_end += 1;
}
failures_kept += 1;
if failures_kept <= MAX_FAILURES {
for line in &lines[block_start..block_end] {
result.push((*line).to_string());
}
} else {
failures_dropped += 1;
}
index = block_end;
continue;
}
index += 1;
}
if failures_dropped > 0 {
result.push(format!("+{failures_dropped} more failures"));
}
if result.is_empty() {
return GenericCompressor::compress_output(output);
}
trim_trailing_lines(&result.join("\n"))
}
fn compress_test_pass_only(lines: &[&str]) -> String {
let mut result: Vec<String> = Vec::new();
let mut saw_ran_summary = false;
for line in lines {
if saw_ran_summary {
result.push((*line).to_string());
continue;
}
if is_bun_test_header(line) || is_summary_line(line) {
result.push((*line).to_string());
if is_ran_summary_line(line) {
saw_ran_summary = true;
}
}
}
if result.is_empty() {
return GenericCompressor::compress_output(&lines.join("\n"));
}
trim_trailing_lines(&result.join("\n"))
}
fn next_index<F>(lines: &[&str], start: usize, predicate: F) -> Option<usize>
where
F: Fn(&str) -> bool,
{
lines
.iter()
.enumerate()
.skip(start)
.find(|(_, line)| predicate(line))
.map(|(i, _)| i)
}
fn is_bun_test_header(line: &str) -> bool {
line.starts_with("bun test v")
}
fn is_file_section_header(line: &str) -> bool {
let trimmed = line.trim_end();
if trimmed.starts_with(' ') || !trimmed.ends_with(':') {
return false;
}
let path = &trimmed[..trimmed.len() - 1];
if path.is_empty() || path.contains(' ') {
return false;
}
path.contains(".test.")
|| path.contains(".spec.")
|| path.contains("_test.")
|| path.contains("_spec.")
}
fn is_bun_test_fail_marker(line: &str) -> bool {
line.trim_start().starts_with("(fail)")
}
fn is_bun_test_error_start(line: &str) -> bool {
line.starts_with("error:")
}
fn is_bun_test_code_pointer(line: &str) -> bool {
let trimmed = line.trim_start();
if !trimmed.contains(" | ") && !trimmed.contains("| ") {
return false;
}
trimmed
.chars()
.next()
.is_some_and(|char| char.is_ascii_digit())
}
fn is_ran_summary_line(line: &str) -> bool {
let trimmed = line.trim_start();
trimmed.starts_with("Ran ")
&& trimmed.contains(" tests")
&& (trimmed.contains(" files. [") || trimmed.contains(" file. ["))
}
fn is_summary_line(line: &str) -> bool {
let trimmed = line.trim_start();
if trimmed.starts_with("Ran ") && trimmed.contains(" tests") {
return true;
}
if let Some(first_token) = trimmed.split_whitespace().next() {
if first_token.chars().all(|char| char.is_ascii_digit()) {
let rest = trimmed[first_token.len()..].trim_start();
return rest.starts_with("pass")
|| rest.starts_with("fail")
|| rest.starts_with("expect()");
}
}
false
}
fn is_bun_progress(line: &str) -> bool {
let trimmed = line.trim();
trimmed == "."
|| trimmed.chars().all(|char| char == '.')
|| trimmed.starts_with("Resolving")
|| trimmed.starts_with("Resolved")
|| trimmed.starts_with("Downloaded")
|| trimmed.starts_with("Extracted")
}
fn is_timing_line(line: &str) -> bool {
let trimmed = line.trim_start();
trimmed.starts_with('[') && trimmed.contains(" ms]")
}
fn trim_trailing_lines(input: &str) -> String {
input
.lines()
.map(str::trim_end)
.collect::<Vec<_>>()
.join("\n")
}