Skip to content

Commit

Permalink
Introduce a WithColor::{Yes,No} argument helper for error rendering
Browse files Browse the repository at this point in the history
If we're already always using the comment to specify the argument name,
we might as well use a named enum to convey the meaning directly.

This is opt-in - we can continue to pass regular boolean by using the
`Into<bool>` rather than accepting only `WithColor` arguments.
  • Loading branch information
Xanewok committed Dec 2, 2023
1 parent 582b02f commit 88c654d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 16 deletions.
24 changes: 22 additions & 2 deletions crates/codegen/parser/runtime/src/parse_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ pub struct ParseError {
pub(crate) tokens_that_would_have_allowed_more_progress: Vec<TokenKind>,
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum WithColor {
Yes,
No,
}

impl From<WithColor> for bool {
fn from(value: WithColor) -> Self {
match value {
WithColor::Yes => true,
WithColor::No => false,
}
}
}

impl ParseError {
pub fn text_range(&self) -> &TextRange {
return &self.text_range;
Expand All @@ -28,8 +43,13 @@ impl ParseError {
.collect();
}

pub fn to_error_report(&self, source_id: &str, source: &str, with_color: bool) -> String {
return render_error_report(self, source_id, source, with_color);
pub fn to_error_report(
&self,
source_id: &str,
source: &str,
with_color: impl Into<bool>,
) -> String {
return render_error_report(self, source_id, source, with_color.into());
}
}

Expand Down
24 changes: 22 additions & 2 deletions crates/solidity/outputs/cargo/crate/src/generated/parse_error.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/solidity/outputs/cargo/crate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{fs, path::PathBuf};
use anyhow::{Context, Result};
use clap::{Parser as ClapParser, Subcommand};
use semver::Version;
use slang_solidity::{kinds::ProductionKind, language::Language};
use slang_solidity::{kinds::ProductionKind, language::Language, parse_error::WithColor};

// Below are dependencies used by the API `lib.rs`, but not the CLI "main.rs".
// However, we need to add a fake usage to suppress Cargo warnings about unused dependencies.
Expand Down Expand Up @@ -63,7 +63,7 @@ fn execute_parse_command(file_path_string: String, version: Version, json: bool)

let errors = output.errors();
for error in errors {
let report = error.to_error_report(&file_path_string, &input, /* with_color */ true);
let report = error.to_error_report(&file_path_string, &input, WithColor::Yes);
eprintln!("{report}");
}

Expand Down
6 changes: 2 additions & 4 deletions crates/solidity/outputs/cargo/tests/src/cst_output/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::str::FromStr;

use anyhow::Result;
use infra_utils::{cargo::CargoWorkspace, codegen::Codegen, paths::PathExtensions};
use slang_solidity::{kinds::ProductionKind, language::Language};
use slang_solidity::{kinds::ProductionKind, language::Language, parse_error::WithColor};
use solidity_testing_utils::cst_snapshots::CstSnapshots;
use strum_macros::Display;

Expand Down Expand Up @@ -46,9 +46,7 @@ pub fn run(parser_name: &str, test_name: &str) -> Result<()> {
let errors = output
.errors()
.iter()
.map(|error| {
error.to_error_report(source_id, &source, /* with_color */ false)
})
.map(|error| error.to_error_report(source_id, &source, WithColor::No))
.collect();

let cursor = output.create_tree_cursor();
Expand Down
24 changes: 22 additions & 2 deletions crates/solidity/outputs/npm/crate/src/generated/parse_error.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions crates/solidity/testing/sanctuary/src/reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use anyhow::Result;
use indicatif::ProgressBar;
use semver::Version;
use slang_solidity::parse_output::ParseOutput;
use slang_solidity::{parse_error::WithColor, parse_output::ParseOutput};

pub struct Reporter {
progress_bar: ProgressBar,
Expand Down Expand Up @@ -82,9 +82,7 @@ impl Reporter {
let reports: Vec<_> = errors
.iter()
.take(remaining)
.map(|error| {
error.to_error_report(source_id, source, /* with_color */ true)
})
.map(|error| error.to_error_report(source_id, source, WithColor::Yes))
.collect();

self.progress_bar.suspend(|| {
Expand Down

0 comments on commit 88c654d

Please sign in to comment.