Remove lifetime parameter from SpanContents

This commit is contained in:
The-Minecraft-Scientist 2024-11-21 22:45:04 -08:00
parent 2902a2337c
commit f40ea26aef
4 changed files with 59 additions and 29 deletions

View File

@ -165,7 +165,7 @@ impl NarratableReportHandler {
.map(|label| {
source.read_span(label.inner(), self.context_lines, self.context_lines)
})
.collect::<Result<Vec<Box<dyn SpanContents<'_>>>, MietteError>>()
.collect::<Result<Vec<Box<dyn SpanContents>>, MietteError>>()
.map_err(|_| fmt::Error)?;
let mut contexts = Vec::new();
for (right, right_conts) in labels.iter().cloned().zip(contents.iter()) {
@ -286,7 +286,7 @@ impl NarratableReportHandler {
&'a self,
source: &'a dyn SourceCode,
context_span: &'a SourceSpan,
) -> Result<(Box<dyn SpanContents<'a> + 'a>, Vec<Line>), fmt::Error> {
) -> Result<(Box<dyn SpanContents + 'a>, Vec<Line>), fmt::Error> {
let context_data = source
.read_span(context_span, self.context_lines, self.context_lines)
.map_err(|_| fmt::Error)?;

View File

@ -1,4 +1,4 @@
use crate::{MietteError, MietteSpanContents, SourceCode, SpanContents};
use crate::{MietteError, SourceCode, SpanContents};
/// Utility struct for when you have a regular [`SourceCode`] type that doesn't
/// implement `name`. For example [`String`]. Or if you want to override the
@ -51,6 +51,43 @@ impl<S: SourceCode + 'static> NamedSource<S> {
self
}
}
/// Utility struct used by [`NamedSource`] to attach a file name to an inner [`SpanContents`] value
#[derive(Debug)]
pub struct NamedSpanContents<T: ?Sized> {
inner: Box<T>,
name: Box<str>,
language: Option<Box<str>>,
}
impl<T: SpanContents + ?Sized> SpanContents for NamedSpanContents<T> {
#[inline]
fn data(&self) -> &[u8] {
self.inner.data()
}
#[inline]
fn span(&self) -> &crate::SourceSpan {
self.inner.span()
}
#[inline]
fn line(&self) -> usize {
self.inner.line()
}
#[inline]
fn column(&self) -> usize {
self.inner.column()
}
#[inline]
fn line_count(&self) -> usize {
self.inner.line_count()
}
#[inline]
fn name(&self) -> Option<&str> {
Some(&self.name)
}
#[inline]
fn language(&self) -> Option<&str> {
self.language.as_deref()
}
}
impl<S: SourceCode + 'static> SourceCode for NamedSource<S> {
fn read_span<'a>(
@ -58,21 +95,14 @@ impl<S: SourceCode + 'static> SourceCode for NamedSource<S> {
span: &crate::SourceSpan,
context_lines_before: usize,
context_lines_after: usize,
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError> {
) -> Result<Box<dyn SpanContents + 'a>, MietteError> {
let inner_contents =
self.inner()
.read_span(span, context_lines_before, context_lines_after)?;
let mut contents = MietteSpanContents::new_named(
self.name.clone(),
inner_contents.data(),
*inner_contents.span(),
inner_contents.line(),
inner_contents.column(),
inner_contents.line_count(),
);
if let Some(language) = &self.language {
contents = contents.with_language(language);
}
Ok(Box::new(contents))
Ok(Box::new(NamedSpanContents {
inner: inner_contents,
name: self.name.clone().into_boxed_str(),
language: self.language.as_ref().map(|v| v.clone().into_boxed_str()),
}))
}
}

View File

@ -252,7 +252,7 @@ pub trait SourceCode: Send + Sync {
span: &SourceSpan,
context_lines_before: usize,
context_lines_after: usize,
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError>;
) -> Result<Box<dyn SpanContents + 'a>, MietteError>;
}
/// A labeled [`SourceSpan`].
@ -434,9 +434,9 @@ Contents of a [`SourceCode`] covered by [`SourceSpan`].
Includes line and column information to optimize highlight calculations.
*/
pub trait SpanContents<'a> {
pub trait SpanContents {
/// Reference to the data inside the associated span, in bytes.
fn data(&self) -> &'a [u8];
fn data(&self) -> &[u8];
/// [`SourceSpan`] representing the span covered by this `SpanContents`.
fn span(&self) -> &SourceSpan;
/// An optional (file?) name for the container of this `SpanContents`.
@ -530,8 +530,8 @@ impl<'a> MietteSpanContents<'a> {
}
}
impl<'a> SpanContents<'a> for MietteSpanContents<'a> {
fn data(&self) -> &'a [u8] {
impl<'a> SpanContents for MietteSpanContents<'a> {
fn data(&self) -> &[u8] {
self.data
}
fn span(&self) -> &SourceSpan {

View File

@ -98,7 +98,7 @@ impl SourceCode for [u8] {
span: &SourceSpan,
context_lines_before: usize,
context_lines_after: usize,
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError> {
) -> Result<Box<dyn SpanContents + 'a>, MietteError> {
let contents = context_info(self, span, context_lines_before, context_lines_after)?;
Ok(Box::new(contents))
}
@ -110,7 +110,7 @@ impl<'src> SourceCode for &'src [u8] {
span: &SourceSpan,
context_lines_before: usize,
context_lines_after: usize,
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError> {
) -> Result<Box<dyn SpanContents + 'a>, MietteError> {
<[u8] as SourceCode>::read_span(self, span, context_lines_before, context_lines_after)
}
}
@ -121,7 +121,7 @@ impl SourceCode for Vec<u8> {
span: &SourceSpan,
context_lines_before: usize,
context_lines_after: usize,
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError> {
) -> Result<Box<dyn SpanContents + 'a>, MietteError> {
<[u8] as SourceCode>::read_span(self, span, context_lines_before, context_lines_after)
}
}
@ -132,7 +132,7 @@ impl SourceCode for str {
span: &SourceSpan,
context_lines_before: usize,
context_lines_after: usize,
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError> {
) -> Result<Box<dyn SpanContents + 'a>, MietteError> {
<[u8] as SourceCode>::read_span(
self.as_bytes(),
span,
@ -149,7 +149,7 @@ impl<'s> SourceCode for &'s str {
span: &SourceSpan,
context_lines_before: usize,
context_lines_after: usize,
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError> {
) -> Result<Box<dyn SpanContents + 'a>, MietteError> {
<str as SourceCode>::read_span(self, span, context_lines_before, context_lines_after)
}
}
@ -160,7 +160,7 @@ impl SourceCode for String {
span: &SourceSpan,
context_lines_before: usize,
context_lines_after: usize,
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError> {
) -> Result<Box<dyn SpanContents + 'a>, MietteError> {
<str as SourceCode>::read_span(self, span, context_lines_before, context_lines_after)
}
}
@ -171,7 +171,7 @@ impl<T: ?Sized + SourceCode> SourceCode for Arc<T> {
span: &SourceSpan,
context_lines_before: usize,
context_lines_after: usize,
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError> {
) -> Result<Box<dyn SpanContents + 'a>, MietteError> {
self.as_ref()
.read_span(span, context_lines_before, context_lines_after)
}
@ -191,7 +191,7 @@ where
span: &SourceSpan,
context_lines_before: usize,
context_lines_after: usize,
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError> {
) -> Result<Box<dyn SpanContents + 'a>, MietteError> {
self.as_ref()
.read_span(span, context_lines_before, context_lines_after)
}