Skip to content

Commit

Permalink
fix(protocol): protocol improvements after getting feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Aug 5, 2021
1 parent 9ba2389 commit e955321
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<dyn '_ + Iterator<Item = &'_ str>>> {
Some(Box::new(vec!["try doing it better next time?"].into_iter()))
}

fn snippets(&self) -> Option<&[DiagnosticSnippet]> {
Expand Down
12 changes: 6 additions & 6 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ 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.
fn severity(&self) -> Severity;

/// 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<Box<dyn '_ + Iterator<Item = &'_ str>>> {
None
}

Expand Down Expand Up @@ -89,15 +89,15 @@ 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<Box<dyn SpanContents<'a> + '_>, MietteError>;
-> Result<Box<dyn SpanContents + 'a>, MietteError>;
}

/**
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.
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<&str>>()
.join(" ")
)?;
writeln!(
Expand Down
6 changes: 3 additions & 3 deletions tests/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ 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"
}

fn severity(&self) -> Severity {
Severity::Error
}

fn help(&self) -> Option<&[&str]> {
Some(&["try doing it better next time?"])
fn help(&self) -> Option<Box<dyn '_ + Iterator<Item = &'_ str>>> {
Some(Box::new(vec!["try doing it better next time?"].into_iter()))
}

fn snippets(&self) -> Option<&[DiagnosticSnippet]> {
Expand Down

0 comments on commit e955321

Please sign in to comment.