From ec934883659e712c6648e1069640c7cb0b0dc90f Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 26 Apr 2022 10:22:31 -0400 Subject: [PATCH] tamer: diagnost::resolver::ResolvedSpan: Clear methods for all data This (a) makes it clear the intent of these methods and (b) will allow introducing a trait for mocking it. DEV-12151 --- tamer/src/diagnose/report.rs | 4 ++-- tamer/src/diagnose/resolver.rs | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/tamer/src/diagnose/report.rs b/tamer/src/diagnose/report.rs index 109bccc0..fdccf6fe 100644 --- a/tamer/src/diagnose/report.rs +++ b/tamer/src/diagnose/report.rs @@ -134,7 +134,7 @@ impl MaybeResolvedSpan { fn header(&self) -> SpanHeader { match self { Self::Resolved(rspan) => { - SpanHeader(rspan.span.ctx(), HeaderLineNum::Resolved(&rspan)) + SpanHeader(rspan.ctx(), HeaderLineNum::Resolved(&rspan)) } Self::Unresolved(span, _) => { @@ -260,7 +260,7 @@ struct HeaderColNum<'s>(&'s ResolvedSpan); impl<'s> Display for HeaderColNum<'s> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let Self(rspan) = self; - let span = rspan.span; + let span = rspan.unresolved_span(); match rspan.col_num() { Some(col) => write!(f, ":{}", col), diff --git a/tamer/src/diagnose/resolver.rs b/tamer/src/diagnose/resolver.rs index 5e1b0eab..1e687eab 100644 --- a/tamer/src/diagnose/resolver.rs +++ b/tamer/src/diagnose/resolver.rs @@ -122,7 +122,7 @@ impl AsRef> for NonEmptyVec { #[derive(Debug, PartialEq, Eq)] pub struct ResolvedSpan { /// The original [`Span`] whose resolution was requested. - pub span: Span, + span: Span, /// The lines of source code that correspond to this [`Span`], /// if known. @@ -135,17 +135,35 @@ pub struct ResolvedSpan { } impl ResolvedSpan { + /// Line number representing the offset of the [`Span`]. pub fn line_num(&self) -> NonZeroU32 { self.lines.first().num } + /// Column number(s) relative to the beginning of the line. + /// + /// The column may not be able to be resolved if the line contains + /// invalid UTF-8 data. pub fn col_num(&self) -> Option { self.lines.first().column } + /// A [`Span`] representing the first line. + /// + /// Note that many spans have only one line associated with them. pub fn first_line_span(&self) -> Span { self.lines.first().span } + + /// [`Context`] of the [`Span`] used for resolution. + pub fn ctx(&self) -> Context { + self.span.ctx() + } + + /// The original [`Span`] before resolution. + pub fn unresolved_span(&self) -> Span { + self.span + } } /// Source column offsets.