diff --git a/crates/codegen/parser/runtime/src/parse_error.rs b/crates/codegen/parser/runtime/src/parse_error.rs index 1a4aed476c..a58d1967f4 100644 --- a/crates/codegen/parser/runtime/src/parse_error.rs +++ b/crates/codegen/parser/runtime/src/parse_error.rs @@ -11,6 +11,21 @@ pub struct ParseError { pub(crate) tokens_that_would_have_allowed_more_progress: Vec, } +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum WithColor { + Yes, + No, +} + +impl From 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; @@ -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, + ) -> String { + return render_error_report(self, source_id, source, with_color.into()); } } diff --git a/crates/solidity/outputs/cargo/crate/src/generated/parse_error.rs b/crates/solidity/outputs/cargo/crate/src/generated/parse_error.rs index 9d997bc81e..02fb5f9ee6 100644 --- a/crates/solidity/outputs/cargo/crate/src/generated/parse_error.rs +++ b/crates/solidity/outputs/cargo/crate/src/generated/parse_error.rs @@ -13,6 +13,21 @@ pub struct ParseError { pub(crate) tokens_that_would_have_allowed_more_progress: Vec, } +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum WithColor { + Yes, + No, +} + +impl From 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; @@ -30,8 +45,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, + ) -> String { + return render_error_report(self, source_id, source, with_color.into()); } } diff --git a/crates/solidity/outputs/cargo/crate/src/main.rs b/crates/solidity/outputs/cargo/crate/src/main.rs index 8339f6cfc5..d921d2718a 100644 --- a/crates/solidity/outputs/cargo/crate/src/main.rs +++ b/crates/solidity/outputs/cargo/crate/src/main.rs @@ -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. @@ -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}"); } diff --git a/crates/solidity/outputs/cargo/tests/src/cst_output/runner.rs b/crates/solidity/outputs/cargo/tests/src/cst_output/runner.rs index 53bf4693f6..c57d797938 100644 --- a/crates/solidity/outputs/cargo/tests/src/cst_output/runner.rs +++ b/crates/solidity/outputs/cargo/tests/src/cst_output/runner.rs @@ -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; @@ -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(); diff --git a/crates/solidity/outputs/npm/crate/src/generated/parse_error.rs b/crates/solidity/outputs/npm/crate/src/generated/parse_error.rs index 9d997bc81e..02fb5f9ee6 100644 --- a/crates/solidity/outputs/npm/crate/src/generated/parse_error.rs +++ b/crates/solidity/outputs/npm/crate/src/generated/parse_error.rs @@ -13,6 +13,21 @@ pub struct ParseError { pub(crate) tokens_that_would_have_allowed_more_progress: Vec, } +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum WithColor { + Yes, + No, +} + +impl From 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; @@ -30,8 +45,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, + ) -> String { + return render_error_report(self, source_id, source, with_color.into()); } } diff --git a/crates/solidity/testing/sanctuary/src/reporting.rs b/crates/solidity/testing/sanctuary/src/reporting.rs index f34b85bbf9..3841a32664 100644 --- a/crates/solidity/testing/sanctuary/src/reporting.rs +++ b/crates/solidity/testing/sanctuary/src/reporting.rs @@ -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, @@ -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(|| {