Skip to content

Commit

Permalink
Improve display format
Browse files Browse the repository at this point in the history
- changing the internal logic of the function a little bit to reduce DRY.
- Using peeakable to reduce stuttering.

Messages
  • Loading branch information
MBerguer committed Feb 4, 2025
1 parent 857d5d2 commit 10f2ce5
Showing 1 changed file with 25 additions and 39 deletions.
64 changes: 25 additions & 39 deletions crates/zksync/compilers/src/artifacts/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! zksolc error from std json output
use foundry_compilers_artifacts_solc::error::{Severity, SourceLocation};

use core::iter::Peekable;
use foundry_compilers_artifacts_solc::serde_helpers;
use serde::{Deserialize, Serialize};
use std::{fmt, ops::Range};
Expand Down Expand Up @@ -43,58 +44,40 @@ impl Error {
self.severity.is_info()
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// TODO: Adding short msg for zksolc results in duplicate error messages.
// Check if this is always the case or if it would be useful to
// add it sometimes.
//let mut short_msg = self.message.trim();
let fmtd_msg = self.formatted_message.as_deref().unwrap_or("");

/*
if short_msg.is_empty() {
// if the message is empty, try to extract the first line from the formatted message
if let Some(first_line) = fmtd_msg.lines().next() {
// this is something like `ParserError: <short_message>`
if let Some((_, s)) = first_line.split_once(':') {
short_msg = s.trim_start();
} else {
short_msg = first_line;
}
}
}
*/

// Error (XXXX): Error Message
// Format the severity level
styled(f, self.severity.color().bold(), |f| self.fmt_severity(f))?;
//fmt_msg(f, short_msg)?;

let mut lines = fmtd_msg.lines();
let mut lines = fmtd_msg.lines().peekable();

/*
// skip the first line if it contains the same message as the one we just formatted,
// unless it also contains a source location, in which case the entire error message is an
// old style error message, like:
// path/to/file:line:column: ErrorType: message
if lines.clone().next().map_or(false, |l| {
l.contains(short_msg) && l.bytes().filter(|b| *b == b':').count() < 3
// Skip the first line if it contains the same message as severity,
// unless it includes a source location (denoted by 3+ colons) something like:
// path/to/file:line:column: ErrorType: message
if lines.peek().map_or(false, |l| {
l.contains(self.severity.to_string().as_str()) &&
l.bytes().filter(|b| *b == b':').count() < 3
}) {
let _ = lines.next();
lines.next();
}
*/

// format the main source location
// Format the main source location
fmt_source_location(f, &mut lines)?;

// format remaining lines as secondary locations
// Process additional message lines
while let Some(line) = lines.next() {
f.write_str("\n")?;
// Use carriage return instead of newline to refresh the same line
f.write_str("\r")?;

if let Some((note, msg)) = line.split_once(':') {
styled(f, Self::secondary_style(), |f| f.write_str(note))?;
fmt_msg(f, msg)?;
} else {
f.write_str(line)?;
match line.split_once(':') {
Some((note, msg)) => {
styled(f, Self::secondary_style(), |f| f.write_str(note))?;
fmt_msg(f, msg)?;
}
None => f.write_str(line)?,
}

fmt_source_location(f, &mut lines)?;
Expand Down Expand Up @@ -157,7 +140,10 @@ fn fmt_msg(f: &mut fmt::Formatter<'_>, msg: &str) -> fmt::Result {
})
}

fn fmt_source_location(f: &mut fmt::Formatter<'_>, lines: &mut std::str::Lines<'_>) -> fmt::Result {
fn fmt_source_location(
f: &mut fmt::Formatter<'_>,
lines: &mut Peekable<std::str::Lines<'_>>,
) -> fmt::Result {
// --> source
if let Some(line) = lines.next() {
f.write_str("\n")?;
Expand Down

0 comments on commit 10f2ce5

Please sign in to comment.