mirror of https://github.com/zkat/miette.git
docs: update readme
This commit is contained in:
parent
8fdf05b523
commit
698bb0eff5
85
README.md
85
README.md
|
|
@ -10,7 +10,7 @@ protocols that allow you to hook into its error reporting facilities, and even
|
||||||
write your own error reports! It lets you define error types that can print out
|
write your own error reports! It lets you define error types that can print out
|
||||||
like this (or in any format you like!):
|
like this (or in any format you like!):
|
||||||
|
|
||||||
```console
|
```sh
|
||||||
Error: Error[oops::my::bad]: oops it broke!
|
Error: Error[oops::my::bad]: oops it broke!
|
||||||
|
|
||||||
[bad_file.rs] This is the part that broke:
|
[bad_file.rs] This is the part that broke:
|
||||||
|
|
@ -46,37 +46,29 @@ $ cargo add miette
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
/*
|
/*
|
||||||
First, you implement a regular `std::error::Error` type.
|
You can derive a Diagnostic from any `std::error::Error` type.
|
||||||
|
|
||||||
`thiserror` is a great way to do so, and plays extremely nicely with `miette`!
|
`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;
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Error)]
|
#[derive(Error, Diagnostic)]
|
||||||
#[error("oops it broke!")]
|
#[error("oops it broke!")]
|
||||||
|
#[diagnostic(
|
||||||
|
code(oops::my::bad),
|
||||||
|
severity(Warning),
|
||||||
|
help("try doing it better next time?"),
|
||||||
|
)]
|
||||||
struct MyBad {
|
struct MyBad {
|
||||||
snippets: Vec<DiagnosticSnippet>,
|
src: Arc<String>,
|
||||||
}
|
filename: String,
|
||||||
|
// Snippets and highlights can be included in the diagnostic!
|
||||||
/*
|
#[snippet(src, filename, "This is the part that broke")]
|
||||||
Next, we have to implement the `Diagnostic` trait for it:
|
snip: SourceSpan,
|
||||||
*/
|
#[highlight(snip, "this bit here")]
|
||||||
|
bad_bit: SourceSpan,
|
||||||
use miette::{Diagnostic, Severity, DiagnosticSnippet};
|
|
||||||
|
|
||||||
impl Diagnostic for MyBad {
|
|
||||||
fn code(&self) -> Box<dyn std::fmt::Display> {
|
|
||||||
Box::new("oops::my::bad")
|
|
||||||
}
|
|
||||||
|
|
||||||
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>>> {
|
|
||||||
Some(Box::new(self.snippets.clone().into_iter()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -102,40 +94,25 @@ impl fmt::Debug for MyBad {
|
||||||
/*
|
/*
|
||||||
Now we can use `miette`~
|
Now we can use `miette`~
|
||||||
*/
|
*/
|
||||||
use std::sync::Arc;
|
|
||||||
use miette::{MietteError, SourceSpan};
|
use miette::{MietteError, SourceSpan};
|
||||||
|
|
||||||
fn make_my_error() -> MyBad {
|
fn pretend_this_is_main() -> Result<(), MyBad> {
|
||||||
// You can use plain strings as a `Source`, bu the protocol is fully extensible!
|
// You can use plain strings as a `Source`, bu the protocol is fully extensible!
|
||||||
let src = "source\n text\n here".to_string();
|
let src = "source\n text\n here".to_string();
|
||||||
let len = src.len();
|
let len = src.len();
|
||||||
|
|
||||||
// The Rust runtime will use `{:?}` (Debug) to print any error you return
|
Err(MyBad {
|
||||||
// from `main`!
|
src: Arc::new(src),
|
||||||
MyBad {
|
filename: "bad_file.rs".into(),
|
||||||
// Snippets are **fully optional**, but in some use cases can provide
|
snip: SourceSpan {
|
||||||
// additional contextual detail for users!
|
start: 0.into(),
|
||||||
//
|
end: (len - 1).into(),
|
||||||
// This is all you need to write to get `rustc`-style, rich error reports!
|
},
|
||||||
//
|
bad_bit: SourceSpan {
|
||||||
// See the docs for `DiagnosticSnippet` to learn more about how to
|
start: 9.into(),
|
||||||
// construct these objects!
|
end: 12.into(),
|
||||||
snippets: vec![DiagnosticSnippet {
|
},
|
||||||
message: Some("This is the part that broke".into()),
|
})
|
||||||
source_name: "bad_file.rs".into(),
|
|
||||||
source: Arc::new(src),
|
|
||||||
context: SourceSpan {
|
|
||||||
start: 0.into(),
|
|
||||||
end: (len - 1).into(),
|
|
||||||
},
|
|
||||||
highlights: Some(vec![
|
|
||||||
("this bit here".into(), SourceSpan {
|
|
||||||
start: 9.into(),
|
|
||||||
end: 12.into(),
|
|
||||||
})
|
|
||||||
]),
|
|
||||||
}],
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue