From 53074d3488e1404331fc1ca3c5e068ac57e9a852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Tue, 17 Aug 2021 17:54:29 -0700 Subject: [PATCH] feat(protocol): add Source impls for Cow and Arc --- src/protocol.rs | 2 +- src/source_impls.rs | 20 ++++++++++++++++++++ tests/derive.rs | 14 +++++++------- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/protocol.rs b/src/protocol.rs index d1d728c..23a7af8 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -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, diff --git a/src/source_impls.rs b/src/source_impls.rs index c42731e..ab355cf 100644 --- a/src/source_impls.rs +++ b/src/source_impls.rs @@ -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 Source for Arc { + fn read_span<'a>( + &'a self, + span: &SourceSpan, + ) -> Result, MietteError> { + self.as_ref().read_span(span) + } +} + +impl Source for Cow<'_, T> { + fn read_span<'a>( + &'a self, + span: &SourceSpan, + ) -> Result, MietteError> { + self.as_ref().read_span(span) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/tests/derive.rs b/tests/derive.rs index b964b13..b373326 100644 --- a/tests/derive.rs +++ b/tests/derive.rs @@ -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, ), } }