1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0
use crate::helpers::{format_duration, plural};
use camino::Utf8Path;
use owo_colors::{OwoColorize, Style};
use std::{
io::{self, Write},
time::Duration,
};
#[derive(Debug)]
/// Reporter for archive operations.
pub struct ArchiveReporter {
styles: Styles,
// TODO: message-format json?
}
impl ArchiveReporter {
/// Creates a new reporter for archive events.
pub fn new(_verbose: bool) -> Self {
Self {
styles: Styles::default(),
}
}
/// Colorizes output.
pub fn colorize(&mut self) {
self.styles.colorize();
}
/// Reports an archive event.
pub fn report_event(
&mut self,
event: ArchiveEvent<'_>,
mut writer: impl Write,
) -> io::Result<()> {
match event {
ArchiveEvent::ArchiveStarted {
test_binary_count,
non_test_binary_count,
build_script_out_dir_count,
linked_path_count,
output_file,
} => {
write!(writer, "{:>12} ", "Archiving".style(self.styles.success))?;
self.report_binary_counts(
test_binary_count,
non_test_binary_count,
build_script_out_dir_count,
linked_path_count,
&mut writer,
)?;
writeln!(writer, " to {}", output_file.style(self.styles.bold))?;
}
ArchiveEvent::Archived {
file_count,
output_file,
elapsed,
} => {
write!(writer, "{:>12} ", "Archived".style(self.styles.success))?;
writeln!(
writer,
"{} files to {} in {}",
file_count.style(self.styles.bold),
output_file.style(self.styles.bold),
format_duration(elapsed),
)?;
}
ArchiveEvent::ExtractStarted {
test_binary_count,
non_test_binary_count,
build_script_out_dir_count,
linked_path_count,
dest_dir: destination_dir,
} => {
write!(writer, "{:>12} ", "Extracting".style(self.styles.success))?;
self.report_binary_counts(
test_binary_count,
non_test_binary_count,
build_script_out_dir_count,
linked_path_count,
&mut writer,
)?;
writeln!(writer, " to {}", destination_dir.style(self.styles.bold))?;
}
ArchiveEvent::Extracted {
file_count,
dest_dir: destination_dir,
elapsed,
} => {
write!(writer, "{:>12} ", "Extracted".style(self.styles.success))?;
writeln!(
writer,
"{} {} to {} in {}",
file_count.style(self.styles.bold),
plural::files_str(file_count),
destination_dir.style(self.styles.bold),
format_duration(elapsed),
)?;
}
}
Ok(())
}
fn report_binary_counts(
&mut self,
test_binary_count: usize,
non_test_binary_count: usize,
build_script_out_dir_count: usize,
linked_path_count: usize,
mut writer: impl Write,
) -> io::Result<()> {
let total_binary_count = test_binary_count + non_test_binary_count;
let non_test_text = if non_test_binary_count > 0 {
format!(
" (including {} non-test {})",
non_test_binary_count.style(self.styles.bold),
plural::binaries_str(non_test_binary_count),
)
} else {
"".to_owned()
};
let mut more = Vec::new();
if build_script_out_dir_count > 0 {
more.push(format!(
"{} build script output {}",
build_script_out_dir_count.style(self.styles.bold),
plural::directories_str(build_script_out_dir_count),
));
}
if linked_path_count > 0 {
more.push(format!(
"{} linked {}",
linked_path_count.style(self.styles.bold),
plural::paths_str(linked_path_count),
));
}
write!(
writer,
"{} {}{non_test_text}",
total_binary_count.style(self.styles.bold),
plural::binaries_str(total_binary_count),
)?;
match more.len() {
0 => Ok(()),
1 => {
write!(writer, " and {}", more[0])
}
_ => {
write!(
writer,
", {}, and {}",
more[..more.len() - 1].join(", "),
more.last().unwrap(),
)
}
}
}
}
#[derive(Debug, Default)]
struct Styles {
bold: Style,
success: Style,
}
impl Styles {
fn colorize(&mut self) {
self.bold = Style::new().bold();
self.success = Style::new().green().bold();
}
}
/// An archive event.
///
/// Events are produced by archive and extract operations, and consumed by an [`ArchiveReporter`].
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ArchiveEvent<'a> {
/// The archive process started.
ArchiveStarted {
/// The number of test binaries to archive.
test_binary_count: usize,
/// The number of non-test binaries to archive.
non_test_binary_count: usize,
/// The number of build script output directories to archive.
build_script_out_dir_count: usize,
/// The number of linked paths to archive.
linked_path_count: usize,
/// The archive output file.
output_file: &'a Utf8Path,
},
/// The archive operation completed successfully.
Archived {
/// The number of files archived.
file_count: usize,
/// The archive output file.
output_file: &'a Utf8Path,
/// How long it took to create the archive.
elapsed: Duration,
},
/// The extraction process started.
ExtractStarted {
/// The number of test binaries to extract.
test_binary_count: usize,
/// The number of non-test binaries to extract.
non_test_binary_count: usize,
/// The number of build script output directories to archive.
build_script_out_dir_count: usize,
/// The number of linked paths to extract.
linked_path_count: usize,
/// The destination directory.
dest_dir: &'a Utf8Path,
},
/// The extraction process completed successfully.
Extracted {
/// The number of files extracted.
file_count: usize,
/// The destination directory.
dest_dir: &'a Utf8Path,
/// How long it took to extract the archive.
elapsed: Duration,
},
}