Skip to content

Commit c6d90f2

Browse files
committed
Write the stop report via Display, and put its annotations first
1 parent 7dddf5b commit c6d90f2

3 files changed

Lines changed: 143 additions & 122 deletions

File tree

‎Cargo.lock‎

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ clap = { version = "4.5", features = ["derive", "env"] }
1515
derive_more = { version = "2", default-features = false, features = [
1616
"display",
1717
] }
18-
either = "1"
1918
flume = { version = "0.12" }
2019
futures = { version = "0.3" }
2120
http = "1"

‎src/real_stop_progress_reporter.rs‎

Lines changed: 143 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use core::error::Error as _;
2-
use core::fmt::{self, Write};
1+
use core::error::Error;
2+
use core::fmt;
33
use core::num::NonZeroU64;
44

5-
use either::Either;
65
use futures::{AsyncWrite, AsyncWriteExt};
76
use nix_types::StorePath;
87

@@ -14,6 +13,23 @@ pub(crate) struct RealStopProgressReporter<W> {
1413
writer: W,
1514
}
1615

16+
/// The report written once every store path has been handled.
17+
struct FinalReport<'a, Ctx: context::Context>(
18+
&'a event_loop::ActionReport<Ctx>,
19+
);
20+
21+
/// A number of things, shown with their name in the singular or plural.
22+
struct Count<'a>(u32, &'a str);
23+
24+
/// An error followed by its causes.
25+
enum WithCauses<'a> {
26+
/// Joined with colons on a single line, as annotations need.
27+
Inline(&'a dyn Error),
28+
29+
/// Each on its own indented line.
30+
Indented(&'a dyn Error),
31+
}
32+
1733
impl<W> RealStopProgressReporter<W> {
1834
pub(crate) fn new(writer: W) -> Self {
1935
Self { reported_paths_left_to_handle: false, writer }
@@ -27,14 +43,12 @@ impl<W: AsyncWrite + Unpin> context::StopProgressReporter
2743
&mut self,
2844
num_paths_left_to_handle: u32,
2945
) {
30-
let (verb, plural) = match num_paths_left_to_handle {
31-
0 => return,
32-
1 => ("is", ""),
33-
_ => ("are", "s"),
34-
};
46+
if num_paths_left_to_handle == 0 {
47+
return;
48+
}
3549
let msg = format!(
36-
"There {verb} still {num_paths_left_to_handle} store path{plural} \
37-
left to handle\n"
50+
"Handling the remaining {}\n",
51+
Count(num_paths_left_to_handle, "store path")
3852
);
3953
let _ = self.writer.write_all(msg.as_bytes()).await;
4054
self.reported_paths_left_to_handle = true;
@@ -45,12 +59,12 @@ impl<W: AsyncWrite + Unpin> context::StopProgressReporter
4559
path: &StorePath<StoreDir>,
4660
_: NonZeroU64,
4761
) {
48-
let msg = format!("Pushed {path}\n",);
62+
let msg = format!("Pushed {path}\n");
4963
let _ = self.writer.write_all(msg.as_bytes()).await;
5064
}
5165

5266
async fn report_path_skipped(&mut self, path: &StorePath<StoreDir>) {
53-
let msg = format!("Skipped {path}\n",);
67+
let msg = format!("Skipped {path}\n");
5468
let _ = self.writer.write_all(msg.as_bytes()).await;
5569
}
5670

@@ -59,140 +73,148 @@ impl<W: AsyncWrite + Unpin> context::StopProgressReporter
5973
path: &StorePath<StoreDir>,
6074
_error: &event_loop::HandlePathError<C, N>,
6175
) {
62-
let msg = format!("Failed handling {path}\n",);
76+
let msg = format!("Failed handling {path}\n");
6377
let _ = self.writer.write_all(msg.as_bytes()).await;
6478
}
6579

6680
async fn report_final_report<Ctx: context::Context>(
6781
mut self,
6882
report: event_loop::ActionReport<Ctx>,
6983
) {
70-
let mut msg = String::default();
84+
let mut msg = String::new();
7185

86+
// An empty line separates the report from the progress lines.
7287
if self.reported_paths_left_to_handle {
73-
msg.write_char('\n').expect("never fails");
88+
msg.push('\n');
7489
}
7590

76-
writeln!(
77-
&mut msg,
78-
"{} store path{} {} skipped",
79-
report.num_paths_skipped,
80-
if report.num_paths_skipped == 1 { "" } else { "s" },
81-
if report.num_paths_skipped == 1 { "was" } else { "were" },
82-
)
83-
.expect("never fails");
84-
85-
write!(
86-
&mut msg,
87-
"{} store path{} {} pushed",
88-
report.num_paths_pushed,
89-
if report.num_paths_pushed == 1 { "" } else { "s" },
90-
if report.num_paths_pushed == 1 { "was" } else { "were" },
91-
)
92-
.expect("never fails");
91+
msg.push_str(&FinalReport(&report).to_string());
9392

94-
if report.num_paths_pushed > 0 {
95-
writeln!(
96-
&mut msg,
97-
", totalling {}",
98-
HumanReadableByteSize(report.num_bytes_pushed)
99-
)
100-
} else {
101-
writeln!(&mut msg)
102-
}
103-
.expect("never fails");
93+
let _ = self.writer.write_all(msg.as_bytes()).await;
94+
let _ = self.writer.flush().await;
95+
}
96+
}
97+
98+
impl<Ctx: context::Context> fmt::Display for FinalReport<'_, Ctx> {
99+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100+
let report = self.0;
101+
102+
let num_paths_not_looked_up = report
103+
.narinfo_lookup_errors
104+
.iter()
105+
.map(|&(num_paths, _)| num_paths)
106+
.sum::<u32>();
107+
108+
let num_paths_failed = (report.path_closure_errors.len()
109+
+ report.path_handling_errors.len())
110+
as u32;
104111

105-
// Warnings are shown as annotations on the workflow run, since the job
106-
// itself doesn't fail.
112+
let num_paths_not_pushed = num_paths_failed
113+
+ num_paths_not_looked_up
114+
+ report.num_paths_without_token;
115+
116+
// Annotations are shown at the top of the workflow run, and here they
117+
// head the details below, since the job itself doesn't fail.
107118

108119
if let Some(error) = &report.token_error {
109-
write!(
110-
&mut msg,
111-
"::warning::Couldn't get a token to push store paths with: \
112-
{error}"
113-
)
114-
.expect("never fails");
115-
116-
// Annotations are a single line, so the causes are joined with
117-
// colons.
118-
let mut source = error.source();
119-
while let Some(err) = source {
120-
write!(&mut msg, ": {err}").expect("never fails");
121-
source = err.source();
122-
}
120+
writeln!(
121+
f,
122+
"::warning::Couldn't get a token to push store paths with: {}",
123+
WithCauses::Inline(error),
124+
)?;
125+
}
123126

124-
msg.write_char('\n').expect("never fails");
127+
if num_paths_not_pushed > 0 {
128+
writeln!(
129+
f,
130+
"::warning title=See setup-cache's post step for \
131+
details::Couldn't push {} to the cache",
132+
Count(num_paths_not_pushed, "store path"),
133+
)?;
125134
}
126135

127-
let num_errors = report.path_closure_errors.len()
128-
+ report.path_handling_errors.len();
136+
writeln!(
137+
f,
138+
"Skipped {}",
139+
Count(report.num_paths_skipped, "store path")
140+
)?;
129141

130-
let num_not_pushed =
131-
num_errors + report.num_paths_without_token as usize;
142+
write!(f, "Pushed {}", Count(report.num_paths_pushed, "store path"))?;
143+
if report.num_paths_pushed > 0 {
144+
write!(
145+
f,
146+
", totalling {}",
147+
HumanReadableByteSize(report.num_bytes_pushed)
148+
)?;
149+
}
150+
writeln!(f)?;
132151

133-
if num_not_pushed > 0 {
152+
if report.num_paths_without_token > 0 {
134153
writeln!(
135-
&mut msg,
136-
"::warning::{num_not_pushed} store path{} {} pushed to the \
137-
cache, see the logs of setup-cache's post step",
138-
if num_not_pushed == 1 { "" } else { "s" },
139-
if num_not_pushed == 1 { "wasn't" } else { "weren't" },
140-
)
141-
.expect("never fails");
154+
f,
155+
"Failed to get a token to push {} with",
156+
Count(report.num_paths_without_token, "store path"),
157+
)?;
142158
}
143159

144-
if report.num_paths_without_token > 0 {
160+
for (num_paths, error) in &report.narinfo_lookup_errors {
145161
writeln!(
146-
&mut msg,
147-
"{} store path{} {} handled because there was no token to \
148-
push {} with",
149-
report.num_paths_without_token,
150-
if report.num_paths_without_token == 1 { "" } else { "s" },
151-
if report.num_paths_without_token == 1 {
152-
"wasn't"
153-
} else {
154-
"weren't"
155-
},
156-
if report.num_paths_without_token == 1 { "it" } else { "them" },
157-
)
158-
.expect("never fails");
162+
f,
163+
"Failed to ask the cache which of {} it has: {}",
164+
Count(*num_paths, "store path"),
165+
WithCauses::Indented(error),
166+
)?;
159167
}
160168

161-
if num_errors > 0 {
169+
if num_paths_failed > 0 {
162170
writeln!(
163-
&mut msg,
164-
"{num_errors} store path{} could not be handled:",
165-
if num_errors == 1 { "" } else { "s" },
166-
)
167-
.expect("never fails");
171+
f,
172+
"Failed to push {}:",
173+
Count(num_paths_failed, "store path")
174+
)?;
168175

169-
let errors = report
176+
let closure_errors = report
170177
.path_closure_errors
171-
.into_iter()
172-
.map(|(path, err)| (path, Either::Left(err)))
173-
.chain(
174-
report
175-
.path_handling_errors
176-
.into_iter()
177-
.map(|(path, err)| (path, Either::Right(err))),
178-
);
179-
180-
for (store_path, error) in errors {
181-
write!(&mut msg, "{store_path}: {error}").expect("never fails");
182-
183-
let mut source = error.source();
184-
while let Some(err) = source {
185-
write!(&mut msg, "\n caused by: {err}")
186-
.expect("never fails");
187-
source = err.source();
188-
}
189-
190-
msg.write_char('\n').expect("never fails");
178+
.iter()
179+
.map(|(path, error)| (path, error as &dyn Error));
180+
181+
let handling_errors = report
182+
.path_handling_errors
183+
.iter()
184+
.map(|(path, error)| (path, error as &dyn Error));
185+
186+
for (path, error) in closure_errors.chain(handling_errors) {
187+
writeln!(f, "{path}: {}", WithCauses::Indented(error))?;
191188
}
192189
}
193190

194-
let _ = self.writer.write_all(msg.as_bytes()).await;
195-
let _ = self.writer.flush().await;
191+
Ok(())
192+
}
193+
}
194+
195+
impl fmt::Display for WithCauses<'_> {
196+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
197+
let (Self::Inline(error) | Self::Indented(error)) = *self;
198+
199+
write!(f, "{error}")?;
200+
201+
let mut source = error.source();
202+
while let Some(cause) = source {
203+
match self {
204+
Self::Inline(_) => write!(f, ": {cause}")?,
205+
Self::Indented(_) => write!(f, "\n caused by: {cause}")?,
206+
}
207+
source = cause.source();
208+
}
209+
210+
Ok(())
211+
}
212+
}
213+
214+
impl fmt::Display for Count<'_> {
215+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
216+
let Self(count, name) = *self;
217+
write!(f, "{count} {name}{}", if count == 1 { "" } else { "s" })
196218
}
197219
}
198220

@@ -222,6 +244,13 @@ impl fmt::Display for HumanReadableByteSize {
222244
mod tests {
223245
use super::*;
224246

247+
#[test]
248+
fn counts_are_singular_only_for_one() {
249+
assert_eq!(Count(0, "store path").to_string(), "0 store paths");
250+
assert_eq!(Count(1, "store path").to_string(), "1 store path");
251+
assert_eq!(Count(21, "store path").to_string(), "21 store paths");
252+
}
253+
225254
#[test]
226255
fn formats_binary_byte_sizes() {
227256
assert_eq!(HumanReadableByteSize(0).to_string(), "0 B");

0 commit comments

Comments
 (0)