Skip to content

Commit

Permalink
feat(protocol): add Source impls for Cow and Arc
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Aug 18, 2021
1 parent 49151bb commit 53074d3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions src/source_impls.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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::*;
Expand Down
14 changes: 7 additions & 7 deletions tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
),
}
}

0 comments on commit 53074d3

Please sign in to comment.