mirror of https://github.com/zkat/miette.git
feat(protocol): add Source impls for Cow and Arc
This commit is contained in:
parent
49151bb095
commit
53074d3488
|
|
@ -112,7 +112,7 @@ If you can read it, you can source it,
|
|||
and it's not necessary to read the whole thing--meaning you should be able to
|
||||
support Sources which are gigabytes or larger in size.
|
||||
*/
|
||||
pub trait Source: std::fmt::Debug + Send + Sync + 'static {
|
||||
pub trait Source: std::fmt::Debug + Send + Sync {
|
||||
/// Read the bytes for a specific span from this Source.
|
||||
fn read_span<'a>(
|
||||
&'a self,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
/*!
|
||||
Default trait implementations for [Source].
|
||||
*/
|
||||
use std::{borrow::Cow, sync::Arc};
|
||||
|
||||
use crate::{MietteError, MietteSpanContents, Source, SourceSpan, SpanContents};
|
||||
|
||||
impl Source for String {
|
||||
|
|
@ -43,6 +45,24 @@ impl Source for String {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Source> Source for Arc<T> {
|
||||
fn read_span<'a>(
|
||||
&'a self,
|
||||
span: &SourceSpan,
|
||||
) -> Result<Box<dyn SpanContents + 'a>, MietteError> {
|
||||
self.as_ref().read_span(span)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Source + Clone> Source for Cow<'_, T> {
|
||||
fn read_span<'a>(
|
||||
&'a self,
|
||||
span: &SourceSpan,
|
||||
) -> Result<Box<dyn SpanContents + 'a>, MietteError> {
|
||||
self.as_ref().read_span(span)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
|||
|
|
@ -232,11 +232,11 @@ fn test_snippet_enum() {
|
|||
#[diagnostic(code(foo::a))]
|
||||
A {
|
||||
src: String,
|
||||
#[snippet(src, "my_snippet.rs", "hi this is where the thing went wrong")]
|
||||
#[snippet(src, "hi this is where the thing went wrong")]
|
||||
snip: SourceSpan,
|
||||
#[highlight(snip, "var 1")]
|
||||
#[highlight(snip)]
|
||||
var1: SourceSpan,
|
||||
#[highlight(snip, "var 2")]
|
||||
#[highlight(snip)]
|
||||
var2: SourceSpan,
|
||||
filename: String,
|
||||
second_message: String,
|
||||
|
|
@ -246,15 +246,15 @@ fn test_snippet_enum() {
|
|||
#[diagnostic(code(foo::b))]
|
||||
B(
|
||||
String,
|
||||
#[snippet(0, "my_snippet.rs", "hi")] SourceSpan,
|
||||
#[highlight(1, "var 1")] SourceSpan,
|
||||
#[snippet(0, "hi")] SourceSpan,
|
||||
#[highlight(1)] SourceSpan,
|
||||
#[highlight(1, "var 2")] SourceSpan,
|
||||
// referenced source name
|
||||
String,
|
||||
String,
|
||||
#[snippet(0, 4, 5)] SourceSpan,
|
||||
#[highlight(6, "var 3")] SourceSpan,
|
||||
#[highlight(6, "var 4")] SourceSpan,
|
||||
#[highlight(6)] SourceSpan,
|
||||
#[highlight(6)] SourceSpan,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue