From 5744e089841c9e0c50f63ee4b5342186713009f8 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Thu, 28 Apr 2022 22:59:13 -0400 Subject: [PATCH] tamer: diagnostic::report: Hoist gutter output into Section The `Section` itself is now responsible for outputting the gutter, which puts us in a position to be able to apply consistent formatting without having to propagate width data to every line variant. --- tamer/src/diagnose/report.rs | 37 ++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/tamer/src/diagnose/report.rs b/tamer/src/diagnose/report.rs index c782d0d4..144e8428 100644 --- a/tamer/src/diagnose/report.rs +++ b/tamer/src/diagnose/report.rs @@ -351,13 +351,9 @@ impl<'d> Display for Section<'d> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, " {heading}\n", heading = self.heading)?; - for line in self.body.iter() { - // Let each line have control over its own newline so that it - // can fully suppress itself if it's not relevant. - line.fmt(f)?; - } - - Ok(()) + self.body.iter().try_for_each(|line| { + write!(f, " {delim}{line}\n", delim = line.gutter_delim()) + }) } } @@ -497,7 +493,7 @@ impl<'d> SpanLabel<'d> { impl<'d> Display for SpanLabel<'d> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let Self(level, label) = self; - write!(f, " = {level}: {label}") + write!(f, " {level}: {label}") } } @@ -511,6 +507,23 @@ enum SectionLine<'d> { } impl<'d> SectionLine<'d> { + /// Delimiter used to separate the gutter from the body. + /// + /// For example: + /// + /// ```text + /// | + /// 12 | source line + /// | ^^^^^^ + /// = note: notice the delim change for this footnote + /// ``` + fn gutter_delim(&self) -> char { + match self { + Self::Footnote(..) => '=', + _ => '|', + } + } + fn into_footnote(self) -> Option { match self { Self::SourceLinePadding => None, @@ -527,10 +540,10 @@ impl<'d> SectionLine<'d> { impl<'d> Display for SectionLine<'d> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - Self::SourceLinePadding => write!(f, " |\n"), - Self::SourceLine(line) => write!(f, " |{line}\n"), - Self::SourceLineMark(mark) => write!(f, " |{mark}\n"), - Self::Footnote(label) => write!(f, "{label}\n"), + Self::SourceLinePadding => Ok(()), + Self::SourceLine(line) => line.fmt(f), + Self::SourceLineMark(mark) => mark.fmt(f), + Self::Footnote(label) => label.fmt(f), } } }