From e955321cbd67372dfebb71a829ddb89baf9b169a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Thu, 5 Aug 2021 09:12:41 -0700 Subject: [PATCH] fix(protocol): protocol improvements after getting feedback --- README.md | 4 ++-- src/protocol.rs | 12 ++++++------ src/reporter.rs | 3 ++- tests/reporter.rs | 6 +++--- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 51f9800..1265129 100644 --- a/README.md +++ b/README.md @@ -74,8 +74,8 @@ impl Diagnostic for MyBad { Severity::Error } - fn help(&self) -> Option<&[&str]> { - Some(&["try doing it better next time?"]) + fn help(&self) -> Option>> { + Some(Box::new(vec!["try doing it better next time?"].into_iter())) } fn snippets(&self) -> Option<&[DiagnosticSnippet]> { diff --git a/src/protocol.rs b/src/protocol.rs index 6980b13..61b8a82 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -12,13 +12,13 @@ use crate::MietteError; Adds rich metadata to your Error that can be used by [DiagnosticReporter] to print really nice and human-friendly error messages. */ -pub trait Diagnostic: std::error::Error + Send + Sync + 'static { +pub trait Diagnostic: std::error::Error + Send + Sync { /// Unique diagnostic code that can be used to look up more information /// about this Diagnostic. Ideally also globally unique, and documented in /// the toplevel crate's documentation for easy searching. Rust path /// format (`foo::bar::baz`) is recommended, but more classic codes like /// `E0123` or Enums will work just fine. - fn code(&self) -> &(dyn Display + 'static); + fn code(&self) -> &(dyn Display); /// Diagnostic severity. This may be used by [DiagnosticReporter]s to change the /// display format of this diagnostic. @@ -26,7 +26,7 @@ pub trait Diagnostic: std::error::Error + Send + Sync + 'static { /// Additional help text related to this Diagnostic. Do you have any /// advice for the poor soul who's just run into this issue? - fn help(&self) -> Option<&[&str]> { + fn help(&self) -> Option>> { None } @@ -89,7 +89,7 @@ support Sources which are gigabytes or larger in size. pub trait Source: std::fmt::Debug + Send + Sync + 'static { /// Read the bytes for a specific span from this Source. fn read_span<'a>(&'a self, span: &SourceSpan) - -> Result + '_>, MietteError>; + -> Result, MietteError>; } /** @@ -97,7 +97,7 @@ Contents of a [Source] covered by [SourceSpan]. Includes line and column information to optimize highlight calculations. */ -pub trait SpanContents<'a> { +pub trait SpanContents { /// Reference to the data inside the associated span, in bytes. fn data(&self) -> &[u8]; /// The 0-indexed line in the associated [Source] where the data begins. @@ -127,7 +127,7 @@ impl<'a> MietteSpanContents<'a> { } } -impl<'a> SpanContents<'a> for MietteSpanContents<'a> { +impl<'a> SpanContents for MietteSpanContents<'a> { fn data(&self) -> &[u8] { self.data } diff --git a/src/reporter.rs b/src/reporter.rs index e5636fb..5dd1c6b 100644 --- a/src/reporter.rs +++ b/src/reporter.rs @@ -162,7 +162,8 @@ impl DiagnosticReporter for JokeReporter { diagnostic, diagnostic .help() - .unwrap_or_else(|| &["have you tried not failing?"]) + .unwrap_or_else(|| Box::new(vec!["have you tried not failing?"].into_iter())) + .collect::>() .join(" ") )?; writeln!( diff --git a/tests/reporter.rs b/tests/reporter.rs index 82ea0ea..ccacd28 100644 --- a/tests/reporter.rs +++ b/tests/reporter.rs @@ -18,7 +18,7 @@ impl fmt::Debug for MyBad { } impl Diagnostic for MyBad { - fn code(&self) -> &(dyn std::fmt::Display + 'static) { + fn code(&self) -> &(dyn std::fmt::Display) { &"oops::my::bad" } @@ -26,8 +26,8 @@ impl Diagnostic for MyBad { Severity::Error } - fn help(&self) -> Option<&[&str]> { - Some(&["try doing it better next time?"]) + fn help(&self) -> Option>> { + Some(Box::new(vec!["try doing it better next time?"].into_iter())) } fn snippets(&self) -> Option<&[DiagnosticSnippet]> {