From 7c81558a8b4ff22d39178f4930069e378a79ec02 Mon Sep 17 00:00:00 2001 From: Adam Curtis Date: Sun, 23 Apr 2023 13:05:55 -0400 Subject: [PATCH] feat(source): Allow inner source type of a NamedSource to be borrowed --- README.md | 2 +- src/lib.rs | 2 +- src/named_source.rs | 21 ++++++++------- tests/graphical.rs | 56 +++++++++++++++++++-------------------- tests/narrated.rs | 30 ++++++++++----------- tests/test_derive_attr.rs | 12 ++++----- tests/test_json.rs | 30 ++++++++++----------- 7 files changed, 78 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index c12734d..0780148 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ struct MyBad { // The Source that we're gonna be printing snippets out of. // This can be a String if you don't have or care about file names. #[source_code] - src: NamedSource, + src: NamedSource, // Snippets and highlights can be included in the diagnostic! #[label("This bit here")] bad_bit: SourceSpan, diff --git a/src/lib.rs b/src/lib.rs index b18239a..94dd2f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -108,7 +108,7 @@ //! // The Source that we're gonna be printing snippets out of. //! // This can be a String if you don't have or care about file names. //! #[source_code] -//! src: NamedSource, +//! src: NamedSource, //! // Snippets and highlights can be included in the diagnostic! //! #[label("This bit here")] //! bad_bit: SourceSpan, diff --git a/src/named_source.rs b/src/named_source.rs index 31ad1d1..8b0b75a 100644 --- a/src/named_source.rs +++ b/src/named_source.rs @@ -3,12 +3,12 @@ use crate::{MietteError, MietteSpanContents, 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 /// `name` returned by the `SourceCode`. -pub struct NamedSource { - source: Box, +pub struct NamedSource { + source: S, name: String, } -impl std::fmt::Debug for NamedSource { +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) @@ -17,12 +17,15 @@ impl std::fmt::Debug for NamedSource { } } -impl NamedSource { +impl NamedSource { /// Create a new `NamedSource` using a regular [`SourceCode`] and giving /// its returned [`SpanContents`] a name. - pub fn new(name: impl AsRef, source: impl SourceCode + Send + Sync + 'static) -> Self { + pub fn new(name: impl AsRef, source: S) -> Self + where + S: Send + Sync, + { Self { - source: Box::new(source), + source, name: name.as_ref().to_string(), } } @@ -34,12 +37,12 @@ impl NamedSource { /// Returns a reference the inner [`SourceCode`] type for this /// `NamedSource`. - pub fn inner(&self) -> &(dyn SourceCode + 'static) { - &*self.source + pub fn inner(&self) -> &S { + &self.source } } -impl SourceCode for NamedSource { +impl SourceCode for NamedSource { fn read_span<'a>( &'a self, span: &crate::SourceSpan, diff --git a/tests/graphical.rs b/tests/graphical.rs index 34dc433..d8a3963 100644 --- a/tests/graphical.rs +++ b/tests/graphical.rs @@ -41,7 +41,7 @@ fn empty_source() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -74,7 +74,7 @@ fn single_line_highlight_span_full_line() { #[diagnostic(severity(Error))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource<&'static str>, #[label("This bit here")] bad_bit: SourceSpan, } @@ -106,7 +106,7 @@ fn single_line_with_wide_char() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -143,7 +143,7 @@ fn single_line_with_two_tabs() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -182,7 +182,7 @@ fn single_line_with_tab_in_middle() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -221,7 +221,7 @@ fn single_line_highlight() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -293,7 +293,7 @@ fn single_line_highlight_offset_zero() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -329,7 +329,7 @@ fn single_line_higlight_offset_end_of_line() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -365,7 +365,7 @@ fn single_line_higlight_include_end_of_line() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -402,7 +402,7 @@ fn single_line_higlight_include_end_of_line_crlf() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -439,7 +439,7 @@ fn single_line_highlight_with_empty_span() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -476,7 +476,7 @@ fn single_line_highlight_no_label() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label] highlight: SourceSpan, } @@ -512,7 +512,7 @@ fn single_line_highlight_at_line_start() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -549,7 +549,7 @@ fn multiple_same_line_highlights() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "x"] highlight1: SourceSpan, #[label = "y"] @@ -594,7 +594,7 @@ fn multiple_same_line_highlights_with_tabs_in_middle() -> Result<(), MietteError #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "x"] highlight1: SourceSpan, #[label = "y"] @@ -641,7 +641,7 @@ fn multiline_highlight_adjacent() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "these two lines"] highlight: SourceSpan, } @@ -677,7 +677,7 @@ fn multiline_highlight_flyby() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "block 1"] highlight1: SourceSpan, #[label = "block 2"] @@ -728,7 +728,7 @@ fn multiline_highlight_no_label() -> Result<(), MietteError> { #[source] source: Inner, #[source_code] - src: NamedSource, + src: NamedSource, #[label = "block 1"] highlight1: SourceSpan, #[label] @@ -792,7 +792,7 @@ fn multiple_multiline_highlights_adjacent() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "this bit here"] highlight1: SourceSpan, #[label = "also this bit"] @@ -838,7 +838,7 @@ fn multiple_multiline_highlights_overlapping_lines() -> Result<(), MietteError> #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "this bit here"] highlight1: SourceSpan, #[label = "also this bit"] @@ -866,7 +866,7 @@ fn multiple_multiline_highlights_overlapping_offsets() -> Result<(), MietteError #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "this bit here"] highlight1: SourceSpan, #[label = "also this bit"] @@ -948,7 +948,7 @@ fn related() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, #[related] @@ -1004,7 +1004,7 @@ fn related_source_code_propagation() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, #[related] @@ -1064,7 +1064,7 @@ fn related_severity() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, #[related] @@ -1081,7 +1081,7 @@ fn related_severity() -> Result<(), MietteError> { )] Error { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, }, @@ -1094,7 +1094,7 @@ fn related_severity() -> Result<(), MietteError> { )] Warning { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, }, @@ -1107,7 +1107,7 @@ fn related_severity() -> Result<(), MietteError> { )] Advice { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, }, @@ -1190,7 +1190,7 @@ fn zero_length_eol_span() { #[diagnostic(severity(Error))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource<&'static str>, #[label("This bit here")] bad_bit: SourceSpan, } diff --git a/tests/narrated.rs b/tests/narrated.rs index 0bdb41d..52acd13 100644 --- a/tests/narrated.rs +++ b/tests/narrated.rs @@ -29,7 +29,7 @@ fn single_line_with_wide_char() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -65,7 +65,7 @@ fn single_line_highlight() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -101,7 +101,7 @@ fn single_line_highlight_offset_zero() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -136,7 +136,7 @@ fn single_line_highlight_with_empty_span() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -172,7 +172,7 @@ fn single_line_highlight_no_label() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label] highlight: SourceSpan, } @@ -208,7 +208,7 @@ fn single_line_highlight_at_line_start() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -244,7 +244,7 @@ fn multiple_same_line_highlights() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "x"] highlight1: SourceSpan, #[label = "y"] @@ -288,7 +288,7 @@ fn multiline_highlight_adjacent() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "these two lines"] highlight: SourceSpan, } @@ -325,7 +325,7 @@ fn multiline_highlight_flyby() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "block 1"] highlight1: SourceSpan, #[label = "block 2"] @@ -378,7 +378,7 @@ fn multiline_highlight_no_label() -> Result<(), MietteError> { #[source] source: Inner, #[source_code] - src: NamedSource, + src: NamedSource, #[label = "block 1"] highlight1: SourceSpan, #[label] @@ -444,7 +444,7 @@ fn multiple_multiline_highlights_adjacent() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "this bit here"] highlight1: SourceSpan, #[label = "also this bit"] @@ -492,7 +492,7 @@ fn multiple_multiline_highlights_overlapping_lines() -> Result<(), MietteError> #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "this bit here"] highlight1: SourceSpan, #[label = "also this bit"] @@ -520,7 +520,7 @@ fn multiple_multiline_highlights_overlapping_offsets() -> Result<(), MietteError #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "this bit here"] highlight1: SourceSpan, #[label = "also this bit"] @@ -559,7 +559,7 @@ fn related() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, #[related] @@ -614,7 +614,7 @@ fn related_source_code_propagation() -> Result<(), MietteError> { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, #[related] diff --git a/tests/test_derive_attr.rs b/tests/test_derive_attr.rs index 5ee950a..f1b0f3d 100644 --- a/tests/test_derive_attr.rs +++ b/tests/test_derive_attr.rs @@ -10,7 +10,7 @@ fn enum_uses_base_attr() { enum MyBad { Only { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, }, @@ -32,7 +32,7 @@ fn enum_uses_variant_attr() { #[diagnostic(code(error::on::variant))] Only { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, }, @@ -55,7 +55,7 @@ fn multiple_attrs_allowed_on_item() { enum MyBad { Only { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, }, @@ -79,7 +79,7 @@ fn multiple_attrs_allowed_on_variant() { #[diagnostic(help("try doing it correctly"))] Only { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, }, @@ -104,7 +104,7 @@ fn attrs_can_be_split_between_item_and_variants() { #[diagnostic(url("https://example.com/foo/bar"))] Only { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, }, @@ -130,7 +130,7 @@ fn attr_not_required() { enum MyBad { Only { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, }, diff --git a/tests/test_json.rs b/tests/test_json.rs index 5bd14cb..6c3ef2c 100644 --- a/tests/test_json.rs +++ b/tests/test_json.rs @@ -20,7 +20,7 @@ mod json_report_handler { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -66,7 +66,7 @@ mod json_report_handler { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -112,7 +112,7 @@ mod json_report_handler { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -158,7 +158,7 @@ mod json_report_handler { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -204,7 +204,7 @@ mod json_report_handler { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label] highlight: SourceSpan, } @@ -249,7 +249,7 @@ mod json_report_handler { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, } @@ -295,7 +295,7 @@ mod json_report_handler { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "x"] highlight1: SourceSpan, #[label = "y"] @@ -361,7 +361,7 @@ mod json_report_handler { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "these two lines"] highlight: SourceSpan, } @@ -407,7 +407,7 @@ mod json_report_handler { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "block 1"] highlight1: SourceSpan, #[label = "block 2"] @@ -472,7 +472,7 @@ mod json_report_handler { #[source] source: Inner, #[source_code] - src: NamedSource, + src: NamedSource, #[label = "block 1"] highlight1: SourceSpan, #[label] @@ -546,7 +546,7 @@ mod json_report_handler { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "this bit here"] highlight1: SourceSpan, #[label = "also this bit"] @@ -602,7 +602,7 @@ mod json_report_handler { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "this bit here"] highlight1: SourceSpan, #[label = "also this bit"] @@ -658,7 +658,7 @@ mod json_report_handler { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label = "this bit here"] highlight1: SourceSpan, #[label = "also this bit"] @@ -742,7 +742,7 @@ mod json_report_handler { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, #[related] @@ -836,7 +836,7 @@ mod json_report_handler { #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] struct MyBad { #[source_code] - src: NamedSource, + src: NamedSource, #[label("this bit here")] highlight: SourceSpan, #[related]