Skip to content

Commit

Permalink
feat(snippets): Overhauled how snippets handle labels, sources, and m…
Browse files Browse the repository at this point in the history
…essages, including the derive macro

BREAKING CHANGE: this will probably break a lot of your stuff. hopefully rustc helps
  • Loading branch information
zkat committed Aug 23, 2021
1 parent 2d886c0 commit 61283e9
Show file tree
Hide file tree
Showing 11 changed files with 392 additions and 347 deletions.
41 changes: 26 additions & 15 deletions README.md
Expand Up @@ -30,7 +30,7 @@ diagnostic error code: ruget::api::bad_json
- [Features](#features)
- [Installing](#installing)
- [Example](#example)
- [Usage](#usage)
- [Using](#using)
- [... in libraries](#-in-libraries)
- [... in application code](#-in-application-code)
- [... in `main()`](#-in-main)
Expand Down Expand Up @@ -85,11 +85,12 @@ use thiserror::Error;
)]
struct MyBad {
// The Source that we're gonna be printing snippets out of.
src: String,
// This can be a String if you don't have or care about file names.
src: NamedSource,
// Snippets and highlights can be included in the diagnostic!
#[snippet(src, "This is the part that broke")]
#[snippet(src, message("This is the part that broke"))]
snip: SourceSpan,
#[highlight(snip)]
#[highlight(snip, label("This bit here"))]
bad_bit: SourceSpan,
}

Expand All @@ -100,17 +101,17 @@ Use this DiagnosticResult type (or its expanded version) as the return type
throughout your app (but NOT your libraries! Those should always return concrete
types!).
*/
use miette::DiagnosticResult;
use miette::{DiagnosticResult, NamedSource};
fn this_fails() -> DiagnosticResult<()> {
// You can use plain strings as a `Source`, or anything that implements
// the one-method `Source` trait.
let src = "source\n text\n here".to_string();
let len = src.len();

Err(MyBad {
src,
snip: ("bad_file.rs", 0, len).into(),
bad_bit: ("this bit here", 9, 4).into(),
src: NamedSource::new("bad_file.rs", src),
snip: (0, len).into(),
bad_bit: (9, 4).into(),
})?;

Ok(())
Expand Down Expand Up @@ -149,7 +150,7 @@ diagnostic help: try doing it better next time?
diagnostic error code: oops::my::bad
">

## Usage
## Using

### ... in libraries

Expand Down Expand Up @@ -244,6 +245,7 @@ use miette::Diagnostic;
use thiserror::Error;

#[derive(Error, Diagnostic, Debug)]
#[error("kaboom")]
#[diagnostic(
code(my_app::my_error),
// You can do formatting!
Expand All @@ -268,6 +270,7 @@ use thiserror::Error;
// Will link users to https://docs.rs/my_crate/0.0.0/my_crate/struct.MyErr.html
url(docsrs)
)]
#[error("kaboom")]
struct MyErr;
```

Expand Down Expand Up @@ -297,17 +300,25 @@ pub struct MyErrorType {
// The `Source` that miette will use.
src: String,

// A snippet that points to `src`, our `Source`. The filename can be
// provided at the callsite.
#[snippet(src, "This is the snippet")]
// A snippet that points to `src`, our `Source`.
#[snippet(
src,
message("This is the snippet")
)]
snip: SourceSpan,

// A highlight for the `snip` snippet we defined above. This will
// underline/mark the specific code inside the larger snippet context.
//
// The label is provided using `SourceSpan`'s label.
#[highlight(snip)]
#[highlight(snip, label("This is the highlight"))]
err_span: SourceSpan,

// You can add as many snippets as you want against the same Source.
// They'll be rendered sequentially.
#[snippet(
src,
message("This is a warning")
)]
snip2: SourceSpan,
}
```

Expand Down
2 changes: 1 addition & 1 deletion miette-derive/src/diagnostic.rs
Expand Up @@ -166,7 +166,7 @@ impl Diagnostic {
let code_body = code.gen_struct();
let help_body = help.as_ref().and_then(|x| x.gen_struct(fields));
let sev_body = severity.as_ref().and_then(|x| x.gen_struct());
let snip_body = snippets.as_ref().and_then(|x| x.gen_struct());
let snip_body = snippets.as_ref().and_then(|x| x.gen_struct(fields));
let url_body = url.as_ref().and_then(|x| x.gen_struct(ident, fields));

quote! {
Expand Down

0 comments on commit 61283e9

Please sign in to comment.