Skip to content

Commit b104ef4

Browse files
mablrzerosnacks
andauthored
refactor(common): remove ReportKind struct (#11511)
Updated SizeReport, GasReport, and TestSummaryReport to eliminate report_kind usage to directly check shell type for JSON. Unified pattern for table style selection based on shell. Co-authored-by: zerosnacks <[email protected]>
1 parent a1534da commit b104ef4

File tree

5 files changed

+29
-112
lines changed

5 files changed

+29
-112
lines changed

crates/common/src/compile.rs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
//! Support for compiling [foundry_compilers::Project]
22
33
use crate::{
4-
TestFunctionExt,
5-
preprocessor::DynamicTestLinkingPreprocessor,
6-
reports::{ReportKind, report_kind},
7-
shell,
8-
term::SpinnerReporter,
4+
TestFunctionExt, preprocessor::DynamicTestLinkingPreprocessor, shell, term::SpinnerReporter,
95
};
106
use comfy_table::{Cell, Color, Table, modifiers::UTF8_ROUND_CORNERS, presets::ASCII_MARKDOWN};
117
use eyre::Result;
@@ -277,8 +273,7 @@ impl ProjectCompiler {
277273
sh_println!()?;
278274
}
279275

280-
let mut size_report =
281-
SizeReport { report_kind: report_kind(), contracts: BTreeMap::new() };
276+
let mut size_report = SizeReport { contracts: BTreeMap::new() };
282277

283278
let mut artifacts: BTreeMap<String, Vec<_>> = BTreeMap::new();
284279
for (id, artifact) in output.artifact_ids().filter(|(id, _)| {
@@ -348,8 +343,6 @@ const CONTRACT_INITCODE_SIZE_LIMIT: usize = 49152;
348343

349344
/// Contracts with info about their size
350345
pub struct SizeReport {
351-
/// What kind of report to generate.
352-
report_kind: ReportKind,
353346
/// `contract name -> info`
354347
pub contracts: BTreeMap<String, ContractInfo>,
355348
}
@@ -388,18 +381,11 @@ impl SizeReport {
388381

389382
impl Display for SizeReport {
390383
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
391-
match self.report_kind {
392-
ReportKind::Text => {
393-
writeln!(f, "\n{}", self.format_table_output(false))?;
394-
}
395-
ReportKind::JSON => {
396-
writeln!(f, "{}", self.format_json_output())?;
397-
}
398-
ReportKind::Markdown => {
399-
writeln!(f, "\n{}", self.format_table_output(true))?;
400-
}
384+
if shell::is_json() {
385+
writeln!(f, "{}", self.format_json_output())?;
386+
} else {
387+
writeln!(f, "\n{}", self.format_table_output())?;
401388
}
402-
403389
Ok(())
404390
}
405391
}
@@ -426,9 +412,9 @@ impl SizeReport {
426412
serde_json::to_string(&contracts).unwrap()
427413
}
428414

429-
fn format_table_output(&self, md: bool) -> Table {
415+
fn format_table_output(&self) -> Table {
430416
let mut table = Table::new();
431-
if md {
417+
if shell::is_markdown() {
432418
table.load_preset(ASCII_MARKDOWN);
433419
} else {
434420
table.apply_modifier(UTF8_ROUND_CORNERS);

crates/common/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ pub mod fs;
2929
pub mod mapping_slots;
3030
mod preprocessor;
3131
pub mod provider;
32-
pub mod reports;
3332
pub mod retry;
3433
pub mod selectors;
3534
pub mod serde_helpers;

crates/common/src/reports.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

crates/forge/src/cmd/test/summary.rs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@ use crate::cmd::test::TestOutcome;
22
use comfy_table::{
33
Cell, Color, Row, Table, modifiers::UTF8_ROUND_CORNERS, presets::ASCII_MARKDOWN,
44
};
5-
use foundry_common::{
6-
reports::{ReportKind, report_kind},
7-
shell,
8-
};
5+
use foundry_common::shell;
96
use foundry_evm::executors::invariant::InvariantMetrics;
107
use itertools::Itertools;
118
use serde_json::json;
129
use std::{collections::HashMap, fmt::Display};
1310

1411
/// Represents a test summary report.
1512
pub struct TestSummaryReport {
16-
/// The kind of report to generate.
17-
report_kind: ReportKind,
1813
/// Whether the report should be detailed.
1914
is_detailed: bool,
2015
/// The test outcome to report.
@@ -23,32 +18,17 @@ pub struct TestSummaryReport {
2318

2419
impl TestSummaryReport {
2520
pub fn new(is_detailed: bool, outcome: TestOutcome) -> Self {
26-
Self { report_kind: report_kind(), is_detailed, outcome }
21+
Self { is_detailed, outcome }
2722
}
2823
}
2924

3025
impl Display for TestSummaryReport {
3126
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
32-
match self.report_kind {
33-
ReportKind::Text => {
34-
writeln!(
35-
f,
36-
"\n{}",
37-
&self.format_table_output(&self.is_detailed, &self.outcome, false)
38-
)?;
39-
}
40-
ReportKind::JSON => {
41-
writeln!(f, "{}", &self.format_json_output(&self.is_detailed, &self.outcome))?;
42-
}
43-
ReportKind::Markdown => {
44-
writeln!(
45-
f,
46-
"\n{}",
47-
&self.format_table_output(&self.is_detailed, &self.outcome, true)
48-
)?;
49-
}
27+
if shell::is_json() {
28+
writeln!(f, "{}", &self.format_json_output(&self.is_detailed, &self.outcome))?;
29+
} else {
30+
writeln!(f, "\n{}", &self.format_table_output(&self.is_detailed, &self.outcome))?;
5031
}
51-
5232
Ok(())
5333
}
5434
}
@@ -81,9 +61,9 @@ impl TestSummaryReport {
8161
serde_json::to_string_pretty(&output).unwrap()
8262
}
8363

84-
fn format_table_output(&self, is_detailed: &bool, outcome: &TestOutcome, md: bool) -> Table {
64+
fn format_table_output(&self, is_detailed: &bool, outcome: &TestOutcome) -> Table {
8565
let mut table = Table::new();
86-
if md {
66+
if shell::is_markdown() {
8767
table.load_preset(ASCII_MARKDOWN);
8868
} else {
8969
table.apply_modifier(UTF8_ROUND_CORNERS);

crates/forge/src/gas_report.rs

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use crate::{
66
};
77
use alloy_primitives::map::HashSet;
88
use comfy_table::{Cell, Color, Table, modifiers::UTF8_ROUND_CORNERS, presets::ASCII_MARKDOWN};
9-
use foundry_common::{
10-
TestFunctionExt, calc,
11-
reports::{ReportKind, report_kind},
12-
};
9+
use foundry_common::{TestFunctionExt, calc, shell};
1310
use foundry_evm::traces::CallKind;
1411

1512
use serde::{Deserialize, Serialize};
@@ -21,8 +18,6 @@ use std::{collections::BTreeMap, fmt::Display};
2118
pub struct GasReport {
2219
/// Whether to report any contracts.
2320
report_any: bool,
24-
/// What kind of report to generate.
25-
report_kind: ReportKind,
2621
/// Contracts to generate the report for.
2722
report_for: HashSet<String>,
2823
/// Contracts to ignore when generating the report.
@@ -43,14 +38,7 @@ impl GasReport {
4338
let report_for = report_for.into_iter().collect::<HashSet<_>>();
4439
let ignore = ignore.into_iter().collect::<HashSet<_>>();
4540
let report_any = report_for.is_empty() || report_for.contains("*");
46-
Self {
47-
report_any,
48-
report_kind: report_kind(),
49-
report_for,
50-
ignore,
51-
include_tests,
52-
..Default::default()
53-
}
41+
Self { report_any, report_for, ignore, include_tests, ..Default::default() }
5442
}
5543

5644
/// Whether the given contract should be reported.
@@ -155,31 +143,17 @@ impl GasReport {
155143

156144
impl Display for GasReport {
157145
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
158-
match self.report_kind {
159-
ReportKind::Text => {
160-
for (name, contract) in &self.contracts {
161-
if contract.functions.is_empty() {
162-
trace!(name, "gas report contract without functions");
163-
continue;
164-
}
165-
166-
let table = self.format_table_output(contract, name, false);
167-
writeln!(f, "\n{table}")?;
146+
if shell::is_json() {
147+
writeln!(f, "{}", &self.format_json_output())?;
148+
} else {
149+
for (name, contract) in &self.contracts {
150+
if contract.functions.is_empty() {
151+
trace!(name, "gas report contract without functions");
152+
continue;
168153
}
169-
}
170-
ReportKind::JSON => {
171-
writeln!(f, "{}", &self.format_json_output())?;
172-
}
173-
ReportKind::Markdown => {
174-
for (name, contract) in &self.contracts {
175-
if contract.functions.is_empty() {
176-
trace!(name, "gas report contract without functions");
177-
continue;
178-
}
179154

180-
let table = self.format_table_output(contract, name, true);
181-
writeln!(f, "\n{table}")?;
182-
}
155+
let table = self.format_table_output(contract, name);
156+
writeln!(f, "\n{table}")?;
183157
}
184158
}
185159

@@ -224,9 +198,9 @@ impl GasReport {
224198
.unwrap()
225199
}
226200

227-
fn format_table_output(&self, contract: &ContractInfo, name: &str, md: bool) -> Table {
201+
fn format_table_output(&self, contract: &ContractInfo, name: &str) -> Table {
228202
let mut table = Table::new();
229-
if md {
203+
if shell::is_markdown() {
230204
table.load_preset(ASCII_MARKDOWN);
231205
} else {
232206
table.apply_modifier(UTF8_ROUND_CORNERS);

0 commit comments

Comments
 (0)