Skip to content

Commit

Permalink
feat(protocol): Simplify protocol return values further
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Switched to Box instead of & for help/code return values.
  • Loading branch information
zkat committed Aug 11, 2021
1 parent a04239c commit 02dd1f8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub trait Diagnostic: std::error::Error {
/// 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);
fn code<'a>(&'a self) -> Box<dyn Display + 'a>;

/// Diagnostic severity. This may be used by [DiagnosticReporter]s to change the
/// display format of this diagnostic.
Expand All @@ -30,7 +30,7 @@ pub trait Diagnostic: std::error::Error {

/// 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<&(dyn Display)> {
fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
None
}

Expand Down
4 changes: 3 additions & 1 deletion src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ impl DiagnosticReporter for JokeReporter {
"me, with {} {}: {}",
sev,
diagnostic,
diagnostic.help().unwrap_or(&"have you tried not failing?")
diagnostic
.help()
.unwrap_or_else(|| Box::new(&"have you tried not failing?"))
)?;
writeln!(
f,
Expand Down
8 changes: 4 additions & 4 deletions tests/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ impl fmt::Debug for MyBad {
}

impl Diagnostic for MyBad {
fn code(&self) -> &(dyn std::fmt::Display) {
&"oops::my::bad"
fn code(&self) -> Box<dyn std::fmt::Display> {
Box::new(&"oops::my::bad")
}

fn help(&self) -> Option<&(dyn std::fmt::Display)> {
Some(&"try doing it better next time?")
fn help(&self) -> Option<Box<dyn std::fmt::Display>> {
Some(Box::new(&"try doing it better next time?"))
}

fn snippets(&self) -> Option<Box<dyn Iterator<Item = DiagnosticSnippet>>> {
Expand Down

0 comments on commit 02dd1f8

Please sign in to comment.