Skip to content

Commit

Permalink
feat(protocol): reference-based DiagnosticReport!
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Aug 17, 2021
1 parent b7deb2d commit f390520
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 59 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ You can derive a Diagnostic from any `std::error::Error` type.
`thiserror` is a great way to define them so, and plays extremely nicely with `miette`!
*/
use std::sync::Arc;
use miette::Diagnostic;
use thiserror::Error;

Expand All @@ -62,7 +61,7 @@ use thiserror::Error;
help("try doing it better next time?"),
)]
struct MyBad {
src: Arc<String>,
src: String,
// Snippets and highlights can be included in the diagnostic!
#[snippet(src, "This is the part that broke")]
snip: SourceSpan,
Expand Down Expand Up @@ -101,7 +100,7 @@ fn pretend_this_is_main() -> Result<(), MyBad> {
let len = src.len();

Err(MyBad {
src: Arc::new(src),
src,
snip: ("bad_file.rs", 0, len).into(),
bad_bit: ("this bit here", 9, 3).into(),
})
Expand Down
25 changes: 12 additions & 13 deletions miette-derive/src/snippets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,12 @@ impl Snippets {
.map(|msg| match msg {
MemberOrString::String(str) => {
quote! {
message: std::option::Option::Some(#str.into()),
message: std::option::Option::Some(#str),
}
}
MemberOrString::Member(m) => {
quote! {
message: std::option::Option::Some(self.#m.clone()),
message: std::option::Option::Some(self.#m.as_ref()),
}
}
})
Expand All @@ -194,21 +194,20 @@ impl Snippets {
// Source field
let src_ident = &snippet.source;
let src_ident = quote! {
// TODO: I don't like this. Think about it more and maybe improve protocol?
source: self.#src_ident.clone(),
source: &self.#src_ident,
};

// Context
let context = &snippet.snippet;
let context = quote! {
context: self.#context.clone(),
context: &self.#context,
};

// Highlights
let highlights = snippet.highlights.iter().map(|highlight| {
let Highlight { highlight } = highlight;
quote! {
self.#highlight.clone()
&self.#highlight
}
});
let highlights = quote! {
Expand All @@ -229,7 +228,7 @@ impl Snippets {
});
Some(quote! {
#[allow(unused_variables)]
fn snippets(&self) -> std::option::Option<std::boxed::Box<dyn std::iter::Iterator<Item = miette::DiagnosticSnippet>>> {
fn snippets(&self) -> std::option::Option<std::boxed::Box<dyn std::iter::Iterator<Item = miette::DiagnosticSnippet> + '_>> {
Some(Box::new(vec![
#(#snippets),*
].into_iter()))
Expand All @@ -248,7 +247,7 @@ impl Snippets {
.map(|msg| match msg {
MemberOrString::String(str) => {
quote! {
message: std::option::Option::Some(#str.into()),
message: std::option::Option::Some(#str),
}
}
MemberOrString::Member(m) => {
Expand All @@ -259,7 +258,7 @@ impl Snippets {
}
};
quote! {
message: std::option::Option::Some(#m.clone()),
message: std::option::Option::Some(#m.as_ref()),
}
}
})
Expand All @@ -278,7 +277,7 @@ impl Snippets {
};
let src_ident = quote! {
// TODO: I don't like this. Think about it more and maybe improve protocol?
source: #src_ident.clone(),
source: #src_ident,
};

// Context
Expand All @@ -289,7 +288,7 @@ impl Snippets {
}
};
let context = quote! {
context: #context.clone(),
context: #context,
};

// Highlights
Expand All @@ -302,7 +301,7 @@ impl Snippets {
}
};
quote! {
#m.clone()
#m
}
});
let highlights = quote! {
Expand Down Expand Up @@ -346,7 +345,7 @@ impl Snippets {
});
Some(quote! {
#[allow(unused_variables)]
fn snippets(&self) -> std::option::Option<std::boxed::Box<dyn std::iter::Iterator<Item = miette::DiagnosticSnippet>>> {
fn snippets(&self) -> std::option::Option<std::boxed::Box<dyn std::iter::Iterator<Item = miette::DiagnosticSnippet> + '_>> {
match self {
#(#variant_arms)*
_ => std::option::Option::None,
Expand Down
14 changes: 7 additions & 7 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ that you can implement to get access to miette's (and related library's) full
reporting and such features.
*/

use std::{fmt::Display, sync::Arc};
use std::fmt::Display;

use crate::MietteError;

Expand Down Expand Up @@ -36,7 +36,7 @@ pub trait Diagnostic: std::error::Error {

/// Additional contextual snippets. This is typically used for adding
/// marked-up source file output the way compilers often do.
fn snippets(&self) -> Option<Box<dyn Iterator<Item = DiagnosticSnippet>>> {
fn snippets(&self) -> Option<Box<dyn Iterator<Item = DiagnosticSnippet> + '_>> {
None
}
}
Expand Down Expand Up @@ -171,17 +171,17 @@ impl<'a> SpanContents for MietteSpanContents<'a> {
A snippet from a [Source] to be displayed with a message and possibly some highlights.
*/
#[derive(Clone, Debug)]
pub struct DiagnosticSnippet {
pub struct DiagnosticSnippet<'a> {
/// Explanation of this specific diagnostic snippet.
pub message: Option<String>,
pub message: Option<&'a str>,
/// A [Source] that can be used to read the actual text of a source.
pub source: Arc<dyn Source>,
pub source: &'a (dyn Source),
/// The primary [SourceSpan] where this diagnostic is located.
pub context: SourceSpan,
pub context: &'a SourceSpan,
/// Additional [SourceSpan]s that mark specific sections of the span, for
/// example, to underline specific text within the larger span. They're
/// paired with labels that should be applied to those sections.
pub highlights: Option<Vec<SourceSpan>>,
pub highlights: Option<Vec<&'a SourceSpan>>,
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl MietteReporter {
writeln!(f)?;
let context_data = snippet
.source
.read_span(&snippet.context)
.read_span(snippet.context)
.map_err(|_| fmt::Error)?;
let context = std::str::from_utf8(context_data.data()).expect("Bad utf8 detected");
let mut line = context_data.line();
Expand Down Expand Up @@ -180,7 +180,8 @@ impl DiagnosticReporter for JokeReporter {
"miette, her eyes enormous: you {} miette? you {}? oh! oh! jail for mother! jail for mother for One Thousand Years!!!!",
diagnostic.code(),
diagnostic.snippets().map(|snippets| {
snippets.map(|snippet| snippet.message).collect::<Option<Vec<String>>>()
snippets.map(|snippet| snippet.message.map(|x| x.to_owned()))
.collect::<Option<Vec<String>>>()
}).flatten().map(|x| x.join(", ")).unwrap_or_else(||"try and cause miette to panic".into())
)?;

Expand Down
16 changes: 6 additions & 10 deletions tests/derive.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use miette::{Diagnostic, Severity, SourceSpan};
use thiserror::Error;

Expand Down Expand Up @@ -166,11 +164,9 @@ fn test_snippet_named_struct() {
#[diagnostic(code(foo::bar::baz))]
struct Foo {
// The actual "source code" our contexts will be using. This can be
// reused by multiple contexts!
//
// The `Arc` is so you don't have to clone the entire thing into this
// Diagnostic. We just need to be able to read it~
src: Arc<String>,
// reused by multiple contexts, and just needs to implement
// miette::Source!
src: String,

// The "snippet" span. This is the span that will be displayed to
// users. It should be a big enough slice of the Source to provide
Expand Down Expand Up @@ -215,7 +211,7 @@ fn test_snippet_unnamed_struct() {
#[error("welp")]
#[diagnostic(code(foo::bar::baz))]
struct Foo(
Arc<String>,
String,
#[snippet(0, "hi")] SourceSpan,
#[highlight(1)] SourceSpan,
#[highlight(1)] SourceSpan,
Expand All @@ -235,7 +231,7 @@ fn test_snippet_enum() {
enum Foo {
#[diagnostic(code(foo::a))]
A {
src: Arc<String>,
src: String,
#[snippet(src, "my_snippet.rs", "hi this is where the thing went wrong")]
snip: SourceSpan,
#[highlight(snip, "var 1")]
Expand All @@ -249,7 +245,7 @@ fn test_snippet_enum() {
},
#[diagnostic(code(foo::b))]
B(
Arc<String>,
String,
#[snippet(0, "my_snippet.rs", "hi")] SourceSpan,
#[highlight(1, "var 1")] SourceSpan,
#[highlight(1, "var 2")] SourceSpan,
Expand Down
46 changes: 22 additions & 24 deletions tests/reporter.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use std::{fmt, sync::Arc};
use std::fmt;

use miette::{Diagnostic, DiagnosticReporter, DiagnosticSnippet, MietteError, MietteReporter};
use miette::{
Diagnostic, DiagnosticReporter, DiagnosticSnippet, MietteError, MietteReporter, SourceSpan,
};
use thiserror::Error;

#[derive(Error)]
#[error("oops!")]
struct MyBad {
snippets: Vec<DiagnosticSnippet>,
message: String,
src: String,
ctx: SourceSpan,
highlight: SourceSpan,
}

impl fmt::Debug for MyBad {
Expand All @@ -24,35 +29,28 @@ impl Diagnostic for MyBad {
Some(Box::new(&"try doing it better next time?"))
}

fn snippets(&self) -> Option<Box<dyn Iterator<Item = DiagnosticSnippet>>> {
Some(Box::new(self.snippets.clone().into_iter()))
fn snippets(&self) -> Option<Box<dyn Iterator<Item = DiagnosticSnippet> + '_>> {
Some(Box::new(
vec![DiagnosticSnippet {
message: Some(self.message.as_ref()),
source: &self.src,
context: &self.ctx,
highlights: Some(vec![&self.highlight]),
}]
.into_iter(),
))
}
}

#[test]
fn basic() -> Result<(), MietteError> {
let err = MyBad {
snippets: Vec::new(),
};
let out = format!("{:?}", err);
assert_eq!(
"Error[oops::my::bad]: oops!\n\n﹦try doing it better next time?\n".to_string(),
out
);
Ok(())
}

#[test]
fn fancy() -> Result<(), MietteError> {
let src = "source\n text\n here".to_string();
let len = src.len();
let err = MyBad {
snippets: vec![DiagnosticSnippet {
message: Some("This is the part that broke".into()),
source: Arc::new(src),
highlights: Some(vec![("this bit here", 9, 4).into()]),
context: ("bad_file.rs", 0, len).into(),
}],
message: "This is the part that broke".into(),
src,
ctx: ("bad_file.rs", 0, len).into(),
highlight: ("this bit here", 9, 4).into(),
};
let out = format!("{:?}", err);
// println!("{}", out);
Expand Down

0 comments on commit f390520

Please sign in to comment.