Skip to content

Commit b909634

Browse files
committed
Create coverage ui and use it in binary
commit-id:437da9d3
1 parent eebac1b commit b909634

File tree

8 files changed

+97
-23
lines changed

8 files changed

+97
-23
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ version = "0.4.0"
1111
edition = "2024"
1212

1313
[workspace.dependencies]
14+
cairo-lang-sierra = "2.10.1"
15+
cairo-lang-sierra-to-casm = "2.10.1"
16+
cairo-lang-starknet-classes = "2.10.1"
17+
cairo-annotations = "0.2.2"
18+
1419
anyhow = "1.0.96"
1520
assert_fs = "1.1.2"
1621
camino = "1.1.9"
1722
clap = { version = "4.5.30", features = ["derive"] }
1823
criterion = "0.5"
19-
cairo-annotations = "0.2.2"
20-
cairo-lang-sierra = "2.10.1"
21-
cairo-lang-sierra-to-casm = "2.10.1"
22-
cairo-lang-starknet-classes = "2.10.1"
24+
console = "0.15.10"
2325
itertools = "0.14.0"
2426
ignore = "0.4.23"
2527
serde = "1.0.218"

crates/cairo-coverage/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition.workspace = true
55

66
[dependencies]
77
cairo-coverage-core = { path = "../cairo-coverage-core" }
8+
console.workspace = true
89
camino.workspace = true
910
anyhow.workspace = true
1011
clap.workspace = true

crates/cairo-coverage/src/args/run.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ pub enum IncludedComponent {
3434
fn parse_trace_file(path: &str) -> Result<Utf8PathBuf> {
3535
let trace_file = Utf8PathBuf::from(path);
3636

37-
ensure!(trace_file.exists(), "Trace file does not exist");
38-
ensure!(trace_file.is_file(), "Trace file is not a file");
37+
ensure!(trace_file.exists(), "trace file does not exist");
38+
ensure!(trace_file.is_file(), "trace file is not a file");
3939
ensure!(
4040
matches!(trace_file.extension(), Some("json")),
41-
"Trace file must have a JSON extension"
41+
"trace file must have a JSON extension"
4242
);
4343

4444
Ok(trace_file)
@@ -47,8 +47,8 @@ fn parse_trace_file(path: &str) -> Result<Utf8PathBuf> {
4747
fn parse_project_path(path: &str) -> Result<Utf8PathBuf> {
4848
let project_path = Utf8PathBuf::from(path);
4949

50-
ensure!(project_path.exists(), "Project path does not exist");
51-
ensure!(project_path.is_dir(), "Project path is not a directory");
50+
ensure!(project_path.exists(), "project path does not exist");
51+
ensure!(project_path.is_dir(), "project path is not a directory");
5252

5353
Ok(project_path)
5454
}

crates/cairo-coverage/src/commands/clean.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::args::clean::CleanArgs;
2+
use crate::ui;
23
use anyhow::{Context, Result};
34
use std::fs;
45
use walkdir::WalkDir;
@@ -13,7 +14,7 @@ pub fn run(
1314
) -> Result<()> {
1415
let target_file_name = files_to_delete
1516
.file_name()
16-
.context("Failed to obtain the file name from `files_to_delete`.")?;
17+
.context("failed to obtain the file name from `--files-to-delete`")?;
1718

1819
WalkDir::new(root_dir)
1920
.into_iter()
@@ -24,16 +25,17 @@ pub fn run(
2425

2526
if let Some(file_name) = path.file_name() {
2627
if file_name == target_file_name {
27-
println!("Deleting file: {}", path.display());
28+
let path_display = path.display();
29+
ui::msg(format!("deleting file: {path_display}"));
2830
fs::remove_file(path)
29-
.with_context(|| format!("Failed to delete file: {}", path.display()))?;
31+
.with_context(|| format!("failed to delete file: {path_display}"))?;
3032
}
3133
}
3234

3335
Ok(())
3436
})?;
3537

36-
println!("Cleanup complete.");
38+
ui::msg("cleanup complete");
3739
Ok(())
3840
}
3941

crates/cairo-coverage/src/commands/run.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ pub fn run(
2525
.append(true)
2626
.create(true)
2727
.open(&output_path)
28-
.context(format!("Failed to open output file at path: {output_path}"))?
28+
.context(format!("failed to open output file at path: {output_path}"))?
2929
.write_all(lcov.as_bytes())
30-
.context("Failed to write to output file")?;
30+
.context("failed to write to output file")?;
3131

3232
Ok(())
3333
}

crates/cairo-coverage/src/main.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
use crate::args::{CairoCoverageArgs, Command};
22
use anyhow::Result;
3+
34
use clap::Parser;
5+
use std::process::ExitCode;
46

57
mod args;
68
mod commands;
9+
mod ui;
10+
11+
fn main() -> ExitCode {
12+
if let Err(error) = main_inner() {
13+
ui::error(error);
14+
ExitCode::FAILURE
15+
} else {
16+
ExitCode::SUCCESS
17+
}
18+
}
719

8-
fn main() -> Result<()> {
20+
fn main_inner() -> Result<()> {
921
let cairo_coverage_args = CairoCoverageArgs::parse();
1022

11-
let command = match cairo_coverage_args.command {
12-
Some(command) => command,
13-
// TODO:
14-
// * In 0.5.0 add deprecation warning
15-
// * In 0.6.0 remove the default command
16-
None => Command::Run(cairo_coverage_args.run_args.unwrap_or_else(|| {
23+
// TODO:
24+
// * In 0.6.0 remove the default command
25+
let command = if let Some(command) = cairo_coverage_args.command {
26+
command
27+
} else {
28+
ui::warning("running `cairo-coverage` without a subcommand is deprecated");
29+
ui::help("consider using `cairo-coverage run`");
30+
31+
Command::Run(cairo_coverage_args.run_args.unwrap_or_else(|| {
1732
unreachable!("`run_args` should be set when no subcommand is provided")
18-
})),
33+
}))
1934
};
2035

2136
commands::run(command)

crates/cairo-coverage/src/ui.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//! UI utilities for the Cairo coverage tool.
2+
//! All human-oriented messaging must use this module to communicate with the user.
3+
//! Messages should be lowercased and should not end with a period.
4+
use console::style;
5+
use std::fmt::Display;
6+
7+
/// Print a warning message.
8+
pub fn warning(message: impl Display) {
9+
let tag = style("warning").yellow();
10+
println!("{tag}: {message}");
11+
}
12+
13+
/// Print an error message.
14+
pub fn error(message: impl Display) {
15+
let tag = style("error").red();
16+
println!("{tag}: {message}");
17+
}
18+
19+
/// Print a help message.
20+
pub fn help(message: impl Display) {
21+
let tag = style("help").bold();
22+
println!("{tag}: {message}");
23+
}
24+
25+
/// Print a message.
26+
pub fn msg(message: impl Display) {
27+
println!("{message}");
28+
}

0 commit comments

Comments
 (0)