use crate::{MietteError, MietteSpanContents, SourceCode, SpanContents};
pub struct NamedSource {
source: Box<dyn SourceCode + 'static>,
name: String,
}
impl std::fmt::Debug for NamedSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("NamedSource")
.field("name", &self.name)
.field("source", &"<redacted>");
Ok(())
}
}
impl NamedSource {
pub fn new(name: impl AsRef<str>, source: impl SourceCode + Send + Sync + 'static) -> Self {
Self {
source: Box::new(source),
name: name.as_ref().to_string(),
}
}
pub fn inner(&self) -> &(dyn SourceCode + 'static) {
&*self.source
}
}
impl SourceCode for NamedSource {
fn read_span<'a>(
&'a self,
span: &crate::SourceSpan,
context_lines_before: usize,
context_lines_after: usize,
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError> {
let contents = self
.inner()
.read_span(span, context_lines_before, context_lines_after)?;
Ok(Box::new(MietteSpanContents::new_named(
self.name.clone(),
contents.data(),
*contents.span(),
contents.line(),
contents.column(),
contents.line_count(),
)))
}
}