• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![deny(missing_docs, missing_debug_implementations, nonstandard_style)]
2 #![warn(unreachable_pub, rust_2018_idioms)]
3 //! You run miette? You run her code like the software? Oh. Oh! Error code for
4 //! coder! Error code for One Thousand Lines!
5 //!
6 //! ## About
7 //!
8 //! `miette` is a diagnostic library for Rust. It includes a series of
9 //! traits/protocols that allow you to hook into its error reporting facilities,
10 //! and even write your own error reports! It lets you define error types that
11 //! can print out like this (or in any format you like!):
12 //!
13 //! <img src="https://raw.githubusercontent.com/zkat/miette/main/images/serde_json.png" alt="Hi! miette also includes a screen-reader-oriented diagnostic printer that's enabled in various situations, such as when you use NO_COLOR or CLICOLOR settings, or on CI. This behavior is also fully configurable and customizable. For example, this is what this particular diagnostic will look like when the narrated printer is enabled:
14 //! \
15 //! Error: Received some bad JSON from the source. Unable to parse.
16 //!     Caused by: missing field `foo` at line 1 column 1700
17 //! \
18 //! Begin snippet for https://api.nuget.org/v3/registration5-gz-semver2/json.net/index.json starting
19 //! at line 1, column 1659
20 //! \
21 //! snippet line 1: gs&quot;:[&quot;json&quot;],&quot;title&quot;:&quot;&quot;,&quot;version&quot;:&quot;1.0.0&quot;},&quot;packageContent&quot;:&quot;https://api.nuget.o
22 //!     highlight starting at line 1, column 1699: last parsing location
23 //! \
24 //! diagnostic help: This is a bug. It might be in ruget, or it might be in the
25 //! source you're using, but it's definitely a bug and should be reported.
26 //! diagnostic error code: ruget::api::bad_json
27 //! " />
28 //!
29 //! > **NOTE: You must enable the `"fancy"` crate feature to get fancy report
30 //! output like in the screenshots above.** You should only do this in your
31 //! toplevel crate, as the fancy feature pulls in a number of dependencies that
32 //! libraries and such might not want.
33 //!
34 //! ## Table of Contents <!-- omit in toc -->
35 //!
36 //! - [About](#about)
37 //! - [Features](#features)
38 //! - [Installing](#installing)
39 //! - [Example](#example)
40 //! - [Using](#using)
41 //!   - [... in libraries](#-in-libraries)
42 //!   - [... in application code](#-in-application-code)
43 //!   - [... in `main()`](#-in-main)
44 //!   - [... diagnostic code URLs](#-diagnostic-code-urls)
45 //!   - [... snippets](#-snippets)
46 //!   - [... multiple related errors](#-multiple-related-errors)
47 //!   - [... delayed source code](#-delayed-source-code)
48 //!   - [... handler options](#-handler-options)
49 //!   - [... dynamic diagnostics](#-dynamic-diagnostics)
50 //!   - [... syntax highlighting](#-syntax-highlighting)
51 //! - [Acknowledgements](#acknowledgements)
52 //! - [License](#license)
53 //!
54 //! ## Features
55 //!
56 //! - Generic [`Diagnostic`] protocol, compatible (and dependent on)
57 //!   [`std::error::Error`].
58 //! - Unique error codes on every [`Diagnostic`].
59 //! - Custom links to get more details on error codes.
60 //! - Super handy derive macro for defining diagnostic metadata.
61 //! - Replacements for [`anyhow`](https://docs.rs/anyhow)/[`eyre`](https://docs.rs/eyre)
62 //!   types [`Result`], [`Report`] and the [`miette!`] macro for the
63 //!   `anyhow!`/`eyre!` macros.
64 //! - Generic support for arbitrary [`SourceCode`]s for snippet data, with
65 //!   default support for `String`s included.
66 //!
67 //! The `miette` crate also comes bundled with a default [`ReportHandler`] with
68 //! the following features:
69 //!
70 //! - Fancy graphical [diagnostic output](#about), using ANSI/Unicode text
71 //! - single- and multi-line highlighting support
72 //! - Screen reader/braille support, gated on [`NO_COLOR`](http://no-color.org/),
73 //!   and other heuristics.
74 //! - Fully customizable graphical theming (or overriding the printers
75 //!   entirely).
76 //! - Cause chain printing
77 //! - Turns diagnostic codes into links in [supported terminals](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda).
78 //!
79 //! ## Installing
80 //!
81 //! ```sh
82 //! $ cargo add miette
83 //! ```
84 //!
85 //! If you want to use the fancy printer in all these screenshots:
86 //!
87 //! ```sh
88 //! $ cargo add miette --features fancy
89 //! ```
90 //!
91 //! ## Example
92 //!
93 //! ```rust
94 //! /*
95 //! You can derive a `Diagnostic` from any `std::error::Error` type.
96 //!
97 //! `thiserror` is a great way to define them, and plays nicely with `miette`!
98 //! */
99 //! use miette::{Diagnostic, SourceSpan};
100 //! use thiserror::Error;
101 //!
102 //! #[derive(Error, Debug, Diagnostic)]
103 //! #[error("oops!")]
104 //! #[diagnostic(
105 //!     code(oops::my::bad),
106 //!     url(docsrs),
107 //!     help("try doing it better next time?")
108 //! )]
109 //! struct MyBad {
110 //!     // The Source that we're gonna be printing snippets out of.
111 //!     // This can be a String if you don't have or care about file names.
112 //!     #[source_code]
113 //!     src: NamedSource<String>,
114 //!     // Snippets and highlights can be included in the diagnostic!
115 //!     #[label("This bit here")]
116 //!     bad_bit: SourceSpan,
117 //! }
118 //!
119 //! /*
120 //! Now let's define a function!
121 //!
122 //! Use this `Result` type (or its expanded version) as the return type
123 //! throughout your app (but NOT your libraries! Those should always return
124 //! concrete types!).
125 //! */
126 //! use miette::{NamedSource, Result};
127 //! fn this_fails() -> Result<()> {
128 //!     // You can use plain strings as a `Source`, or anything that implements
129 //!     // the one-method `Source` trait.
130 //!     let src = "source\n  text\n    here".to_string();
131 //!     let len = src.len();
132 //!
133 //!     Err(MyBad {
134 //!         src: NamedSource::new("bad_file.rs", src),
135 //!         bad_bit: (9, 4).into(),
136 //!     })?;
137 //!
138 //!     Ok(())
139 //! }
140 //!
141 //! /*
142 //! Now to get everything printed nicely, just return a `Result<()>`
143 //! and you're all set!
144 //!
145 //! Note: You can swap out the default reporter for a custom one using
146 //! `miette::set_hook()`
147 //! */
148 //! fn pretend_this_is_main() -> Result<()> {
149 //!     // kaboom~
150 //!     this_fails()?;
151 //!
152 //!     Ok(())
153 //! }
154 //! ```
155 //!
156 //! And this is the output you'll get if you run this program:
157 //!
158 //! <img src="https://raw.githubusercontent.com/zkat/miette/main/images/single-line-example.png" alt="
159 //! Narratable printout:
160 //! \
161 //! Error: Types mismatched for operation.
162 //!     Diagnostic severity: error
163 //! Begin snippet starting at line 1, column 1
164 //! \
165 //! snippet line 1: 3 + &quot;5&quot;
166 //!     label starting at line 1, column 1: int
167 //!     label starting at line 1, column 1: doesn't support these values.
168 //!     label starting at line 1, column 1: string
169 //! diagnostic help: Change int or string to be the right types and try again.
170 //! diagnostic code: nu::parser::unsupported_operation
171 //! For more details, see https://docs.rs/nu-parser/0.1.0/nu-parser/enum.ParseError.html#variant.UnsupportedOperation">
172 //!
173 //! ## Using
174 //!
175 //! ### ... in libraries
176 //!
177 //! `miette` is _fully compatible_ with library usage. Consumers who don't know
178 //! about, or don't want, `miette` features can safely use its error types as
179 //! regular [`std::error::Error`].
180 //!
181 //! We highly recommend using something like [`thiserror`](https://docs.rs/thiserror)
182 //! to define unique error types and error wrappers for your library.
183 //!
184 //! While `miette` integrates smoothly with `thiserror`, it is _not required_.
185 //! If you don't want to use the [`Diagnostic`] derive macro, you can implement
186 //! the trait directly, just like with `std::error::Error`.
187 //!
188 //! ```rust
189 //! // lib/error.rs
190 //! use miette::{Diagnostic, SourceSpan};
191 //! use thiserror::Error;
192 //!
193 //! #[derive(Error, Diagnostic, Debug)]
194 //! pub enum MyLibError {
195 //!     #[error(transparent)]
196 //!     #[diagnostic(code(my_lib::io_error))]
197 //!     IoError(#[from] std::io::Error),
198 //!
199 //!     #[error("Oops it blew up")]
200 //!     #[diagnostic(code(my_lib::bad_code))]
201 //!     BadThingHappened,
202 //!
203 //!     #[error(transparent)]
204 //!     // Use `#[diagnostic(transparent)]` to wrap another [`Diagnostic`]. You won't see labels otherwise
205 //!     #[diagnostic(transparent)]
206 //!     AnotherError(#[from] AnotherError),
207 //! }
208 //!
209 //! #[derive(Error, Diagnostic, Debug)]
210 //! #[error("another error")]
211 //! pub struct AnotherError {
212 //!    #[label("here")]
213 //!    pub at: SourceSpan
214 //! }
215 //! ```
216 //!
217 //! Then, return this error type from all your fallible public APIs. It's a best
218 //! practice to wrap any "external" error types in your error `enum` instead of
219 //! using something like [`Report`] in a library.
220 //!
221 //! ### ... in application code
222 //!
223 //! Application code tends to work a little differently than libraries. You
224 //! don't always need or care to define dedicated error wrappers for errors
225 //! coming from external libraries and tools.
226 //!
227 //! For this situation, `miette` includes two tools: [`Report`] and
228 //! [`IntoDiagnostic`]. They work in tandem to make it easy to convert regular
229 //! `std::error::Error`s into [`Diagnostic`]s. Additionally, there's a
230 //! [`Result`] type alias that you can use to be more terse.
231 //!
232 //! When dealing with non-`Diagnostic` types, you'll want to
233 //! `.into_diagnostic()` them:
234 //!
235 //! ```rust
236 //! // my_app/lib/my_internal_file.rs
237 //! use miette::{IntoDiagnostic, Result};
238 //! use semver::Version;
239 //!
240 //! pub fn some_tool() -> Result<Version> {
241 //!     Ok("1.2.x".parse().into_diagnostic()?)
242 //! }
243 //! ```
244 //!
245 //! `miette` also includes an `anyhow`/`eyre`-style `Context`/`WrapErr` traits
246 //! that you can import to add ad-hoc context messages to your `Diagnostic`s, as
247 //! well, though you'll still need to use `.into_diagnostic()` to make use of
248 //! it:
249 //!
250 //! ```rust
251 //! // my_app/lib/my_internal_file.rs
252 //! use miette::{IntoDiagnostic, Result, WrapErr};
253 //! use semver::Version;
254 //!
255 //! pub fn some_tool() -> Result<Version> {
256 //!     Ok("1.2.x"
257 //!         .parse()
258 //!         .into_diagnostic()
259 //!         .wrap_err("Parsing this tool's semver version failed.")?)
260 //! }
261 //! ```
262 //!
263 //! To construct your own simple adhoc error use the [miette!] macro:
264 //! ```rust
265 //! // my_app/lib/my_internal_file.rs
266 //! use miette::{miette, IntoDiagnostic, Result, WrapErr};
267 //! use semver::Version;
268 //!
269 //! pub fn some_tool() -> Result<Version> {
270 //!     let version = "1.2.x";
271 //!     Ok(version
272 //!         .parse()
273 //!         .map_err(|_| miette!("Invalid version {}", version))?)
274 //! }
275 //! ```
276 //! There are also similar [bail!] and [ensure!] macros.
277 //!
278 //! ### ... in `main()`
279 //!
280 //! `main()` is just like any other part of your application-internal code. Use
281 //! `Result` as your return value, and it will pretty-print your diagnostics
282 //! automatically.
283 //!
284 //! > **NOTE:** You must enable the `"fancy"` crate feature to get fancy report
285 //! output like in the screenshots here.** You should only do this in your
286 //! toplevel crate, as the fancy feature pulls in a number of dependencies that
287 //! libraries and such might not want.
288 //!
289 //! ```rust
290 //! use miette::{IntoDiagnostic, Result};
291 //! use semver::Version;
292 //!
293 //! fn pretend_this_is_main() -> Result<()> {
294 //!     let version: Version = "1.2.x".parse().into_diagnostic()?;
295 //!     println!("{}", version);
296 //!     Ok(())
297 //! }
298 //! ```
299 //!
300 //! Please note: in order to get fancy diagnostic rendering with all the pretty
301 //! colors and arrows, you should install `miette` with the `fancy` feature
302 //! enabled:
303 //!
304 //! ```toml
305 //! miette = { version = "X.Y.Z", features = ["fancy"] }
306 //! ```
307 //!
308 //! Another way to display a diagnostic is by printing them using the debug formatter.
309 //! This is, in fact, what returning diagnostics from main ends up doing.
310 //! To do it yourself, you can write the following:
311 //!
312 //! ```rust
313 //! use miette::{IntoDiagnostic, Result};
314 //! use semver::Version;
315 //!
316 //! fn just_a_random_function() {
317 //!     let version_result: Result<Version> = "1.2.x".parse().into_diagnostic();
318 //!     match version_result {
319 //!         Err(e) => println!("{:?}", e),
320 //!         Ok(version) => println!("{}", version),
321 //!     }
322 //! }
323 //! ```
324 //!
325 //! ### ... diagnostic code URLs
326 //!
327 //! `miette` supports providing a URL for individual diagnostics. This URL will
328 //! be displayed as an actual link in supported terminals, like so:
329 //!
330 //! <img
331 //! src="https://raw.githubusercontent.com/zkat/miette/main/images/code_linking.png"
332 //! alt=" Example showing the graphical report printer for miette
333 //! pretty-printing an error code. The code is underlined and followed by text
334 //! saying to 'click here'. A hover tooltip shows a full-fledged URL that can be
335 //! Ctrl+Clicked to open in a browser.
336 //! \
337 //! This feature is also available in the narratable printer. It will add a line
338 //! after printing the error code showing a plain URL that you can visit.
339 //! ">
340 //!
341 //! To use this, you can add a `url()` sub-param to your `#[diagnostic]`
342 //! attribute:
343 //!
344 //! ```rust
345 //! use miette::Diagnostic;
346 //! use thiserror::Error;
347 //!
348 //! #[derive(Error, Diagnostic, Debug)]
349 //! #[error("kaboom")]
350 //! #[diagnostic(
351 //!     code(my_app::my_error),
352 //!     // You can do formatting!
353 //!     url("https://my_website.com/error_codes#{}", self.code().unwrap())
354 //! )]
355 //! struct MyErr;
356 //! ```
357 //!
358 //! Additionally, if you're developing a library and your error type is exported
359 //! from your crate's top level, you can use a special `url(docsrs)` option
360 //! instead of manually constructing the URL. This will automatically create a
361 //! link to this diagnostic on `docs.rs`, so folks can just go straight to your
362 //! (very high quality and detailed!) documentation on this diagnostic:
363 //!
364 //! ```rust
365 //! use miette::Diagnostic;
366 //! use thiserror::Error;
367 //!
368 //! #[derive(Error, Diagnostic, Debug)]
369 //! #[diagnostic(
370 //!     code(my_app::my_error),
371 //!     // Will link users to https://docs.rs/my_crate/0.0.0/my_crate/struct.MyErr.html
372 //!     url(docsrs)
373 //! )]
374 //! #[error("kaboom")]
375 //! struct MyErr;
376 //! ```
377 //!
378 //! ### ... snippets
379 //!
380 //! Along with its general error handling and reporting features, `miette` also
381 //! includes facilities for adding error spans/annotations/labels to your
382 //! output. This can be very useful when an error is syntax-related, but you can
383 //! even use it to print out sections of your own source code!
384 //!
385 //! To achieve this, `miette` defines its own lightweight [`SourceSpan`] type.
386 //! This is a basic byte-offset and length into an associated [`SourceCode`]
387 //! and, along with the latter, gives `miette` all the information it needs to
388 //! pretty-print some snippets! You can also use your own `Into<SourceSpan>`
389 //! types as label spans.
390 //!
391 //! The easiest way to define errors like this is to use the
392 //! `derive(Diagnostic)` macro:
393 //!
394 //! ```rust
395 //! use miette::{Diagnostic, SourceSpan};
396 //! use thiserror::Error;
397 //!
398 //! #[derive(Diagnostic, Debug, Error)]
399 //! #[error("oops")]
400 //! #[diagnostic(code(my_lib::random_error))]
401 //! pub struct MyErrorType {
402 //!     // The `Source` that miette will use.
403 //!     #[source_code]
404 //!     src: String,
405 //!
406 //!     // This will underline/mark the specific code inside the larger
407 //!     // snippet context.
408 //!     #[label = "This is the highlight"]
409 //!     err_span: SourceSpan,
410 //!
411 //!     // You can add as many labels as you want.
412 //!     // They'll be rendered sequentially.
413 //!     #[label("This is bad")]
414 //!     snip2: (usize, usize), // `(usize, usize)` is `Into<SourceSpan>`!
415 //!
416 //!     // Snippets can be optional, by using Option:
417 //!     #[label("some text")]
418 //!     snip3: Option<SourceSpan>,
419 //!
420 //!     // with or without label text
421 //!     #[label]
422 //!     snip4: Option<SourceSpan>,
423 //! }
424 //! ```
425 //!
426 //! #### ... help text
427 //! `miette` provides two facilities for supplying help text for your errors:
428 //!
429 //! The first is the `#[help()]` format attribute that applies to structs or
430 //! enum variants:
431 //!
432 //! ```rust
433 //! use miette::Diagnostic;
434 //! use thiserror::Error;
435 //!
436 //! #[derive(Debug, Diagnostic, Error)]
437 //! #[error("welp")]
438 //! #[diagnostic(help("try doing this instead"))]
439 //! struct Foo;
440 //! ```
441 //!
442 //! The other is by programmatically supplying the help text as a field to
443 //! your diagnostic:
444 //!
445 //! ```rust
446 //! use miette::Diagnostic;
447 //! use thiserror::Error;
448 //!
449 //! #[derive(Debug, Diagnostic, Error)]
450 //! #[error("welp")]
451 //! #[diagnostic()]
452 //! struct Foo {
453 //!     #[help]
454 //!     advice: Option<String>, // Can also just be `String`
455 //! }
456 //!
457 //! let err = Foo {
458 //!     advice: Some("try doing this instead".to_string()),
459 //! };
460 //! ```
461 //!
462 //! ### ... multiple related errors
463 //!
464 //! `miette` supports collecting multiple errors into a single diagnostic, and
465 //! printing them all together nicely.
466 //!
467 //! To do so, use the `#[related]` tag on any `IntoIter` field in your
468 //! `Diagnostic` type:
469 //!
470 //! ```rust
471 //! use miette::Diagnostic;
472 //! use thiserror::Error;
473 //!
474 //! #[derive(Debug, Error, Diagnostic)]
475 //! #[error("oops")]
476 //! struct MyError {
477 //!     #[related]
478 //!     others: Vec<MyError>,
479 //! }
480 //! ```
481 //!
482 //! ### ... delayed source code
483 //!
484 //! Sometimes it makes sense to add source code to the error message later.
485 //! One option is to use [`with_source_code()`](Report::with_source_code)
486 //! method for that:
487 //!
488 //! ```rust,no_run
489 //! use miette::{Diagnostic, SourceSpan};
490 //! use thiserror::Error;
491 //!
492 //! #[derive(Diagnostic, Debug, Error)]
493 //! #[error("oops")]
494 //! #[diagnostic()]
495 //! pub struct MyErrorType {
496 //!     // Note: label but no source code
497 //!     #[label]
498 //!     err_span: SourceSpan,
499 //! }
500 //!
501 //! fn do_something() -> miette::Result<()> {
502 //!     // This function emits actual error with label
503 //!     return Err(MyErrorType {
504 //!         err_span: (7..11).into(),
505 //!     })?;
506 //! }
507 //!
508 //! fn main() -> miette::Result<()> {
509 //!     do_something().map_err(|error| {
510 //!         // And this code provides the source code for inner error
511 //!         error.with_source_code(String::from("source code"))
512 //!     })
513 //! }
514 //! ```
515 //!
516 //! Also source code can be provided by a wrapper type. This is especially
517 //! useful in combination with `related`, when multiple errors should be
518 //! emitted at the same time:
519 //!
520 //! ```rust,no_run
521 //! use miette::{Diagnostic, Report, SourceSpan};
522 //! use thiserror::Error;
523 //!
524 //! #[derive(Diagnostic, Debug, Error)]
525 //! #[error("oops")]
526 //! #[diagnostic()]
527 //! pub struct InnerError {
528 //!     // Note: label but no source code
529 //!     #[label]
530 //!     err_span: SourceSpan,
531 //! }
532 //!
533 //! #[derive(Diagnostic, Debug, Error)]
534 //! #[error("oops: multiple errors")]
535 //! #[diagnostic()]
536 //! pub struct MultiError {
537 //!     // Note source code by no labels
538 //!     #[source_code]
539 //!     source_code: String,
540 //!     // The source code above is used for these errors
541 //!     #[related]
542 //!     related: Vec<InnerError>,
543 //! }
544 //!
545 //! fn do_something() -> Result<(), Vec<InnerError>> {
546 //!     Err(vec![
547 //!         InnerError {
548 //!             err_span: (0..6).into(),
549 //!         },
550 //!         InnerError {
551 //!             err_span: (7..11).into(),
552 //!         },
553 //!     ])
554 //! }
555 //!
556 //! fn main() -> miette::Result<()> {
557 //!     do_something().map_err(|err_list| MultiError {
558 //!         source_code: "source code".into(),
559 //!         related: err_list,
560 //!     })?;
561 //!     Ok(())
562 //! }
563 //! ```
564 //!
565 //! ### ... Diagnostic-based error sources.
566 //!
567 //! When one uses the `#[source]` attribute on a field, that usually comes
568 //! from `thiserror`, and implements a method for
569 //! [`std::error::Error::source`]. This works in many cases, but it's lossy:
570 //! if the source of the diagnostic is a diagnostic itself, the source will
571 //! simply be treated as an `std::error::Error`.
572 //!
573 //! While this has no effect on the existing _reporters_, since they don't use
574 //! that information right now, APIs who might want this information will have
575 //! no access to it.
576 //!
577 //! If it's important for you for this information to be available to users,
578 //! you can use `#[diagnostic_source]` alongside `#[source]`. Not that you
579 //! will likely want to use _both_:
580 //!
581 //! ```rust
582 //! use miette::Diagnostic;
583 //! use thiserror::Error;
584 //!
585 //! #[derive(Debug, Diagnostic, Error)]
586 //! #[error("MyError")]
587 //! struct MyError {
588 //!     #[source]
589 //!     #[diagnostic_source]
590 //!     the_cause: OtherError,
591 //! }
592 //!
593 //! #[derive(Debug, Diagnostic, Error)]
594 //! #[error("OtherError")]
595 //! struct OtherError;
596 //! ```
597 //!
598 //! ### ... handler options
599 //!
600 //! [`MietteHandler`] is the default handler, and is very customizable. In
601 //! most cases, you can simply use [`MietteHandlerOpts`] to tweak its behavior
602 //! instead of falling back to your own custom handler.
603 //!
604 //! Usage is like so:
605 //!
606 //! ```rust,ignore
607 //! miette::set_hook(Box::new(|_| {
608 //!     Box::new(
609 //!         miette::MietteHandlerOpts::new()
610 //!             .terminal_links(true)
611 //!             .unicode(false)
612 //!             .context_lines(3)
613 //!             .tab_width(4)
614 //!             .break_words(true)
615 //!             .build(),
616 //!     )
617 //! }))
618 //!
619 //! # .unwrap()
620 //! ```
621 //!
622 //! See the docs for [`MietteHandlerOpts`] for more details on what you can
623 //! customize!
624 //!
625 //! ### ... dynamic diagnostics
626 //!
627 //! If you...
628 //! - ...don't know all the possible errors upfront
629 //! - ...need to serialize/deserialize errors
630 //! then you may want to use [`miette!`], [`diagnostic!`] macros or
631 //! [`MietteDiagnostic`] directly to create diagnostic on the fly.
632 //!
633 //! ```rust,ignore
634 //! # use miette::{miette, LabeledSpan, Report};
635 //!
636 //! let source = "2 + 2 * 2 = 8".to_string();
637 //! let report = miette!(
638 //!   labels = vec[
639 //!       LabeledSpan::at(12..13, "this should be 6"),
640 //!   ],
641 //!   help = "'*' has greater precedence than '+'",
642 //!   "Wrong answer"
643 //! ).with_source_code(source);
644 //! println!("{:?}", report)
645 //! ```
646 //!
647 //! ### ... syntax highlighting
648 //!
649 //! `miette` can be configured to highlight syntax in source code snippets.
650 //!
651 //! <!-- TODO: screenshot goes here once default Theme is decided -->
652 //!
653 //! To use the built-in highlighting functionality, you must enable the
654 //! `syntect-highlighter` crate feature. When this feature is enabled, `miette` will
655 //! automatically use the [`syntect`] crate to highlight the `#[source_code]`
656 //! field of your [`Diagnostic`].
657 //!
658 //! Syntax detection with [`syntect`] is handled by checking 2 methods on the [`SpanContents`] trait, in order:
659 //! * [language()](SpanContents::language) - Provides the name of the language
660 //!   as a string. For example `"Rust"` will indicate Rust syntax highlighting.
661 //!   You can set the language of the [`SpanContents`] produced by a
662 //!   [`NamedSource`] via the [`with_language`](NamedSource::with_language)
663 //!   method.
664 //! * [name()](SpanContents::name) - In the absence of an explicitly set
665 //!   language, the name is assumed to contain a file name or file path.
666 //!   The highlighter will check for a file extension at the end of the name and
667 //!   try to guess the syntax from that.
668 //!
669 //! If you want to use a custom highlighter, you can provide a custom
670 //! implementation of the [`Highlighter`](highlighters::Highlighter)
671 //! trait to [`MietteHandlerOpts`] by calling the
672 //! [`with_syntax_highlighting`](MietteHandlerOpts::with_syntax_highlighting)
673 //! method. See the [`highlighters`] module docs for more details.
674 //!
675 //! ## MSRV
676 //!
677 //! This crate requires rustc 1.70.0 or later.
678 //!
679 //! ## Acknowledgements
680 //!
681 //! `miette` was not developed in a void. It owes enormous credit to various
682 //! other projects and their authors:
683 //!
684 //! - [`anyhow`](http://crates.io/crates/anyhow) and [`color-eyre`](https://crates.io/crates/color-eyre):
685 //!   these two enormously influential error handling libraries have pushed
686 //!   forward the experience of application-level error handling and error
687 //!   reporting. `miette`'s `Report` type is an attempt at a very very rough
688 //!   version of their `Report` types.
689 //! - [`thiserror`](https://crates.io/crates/thiserror) for setting the standard
690 //!   for library-level error definitions, and for being the inspiration behind
691 //!   `miette`'s derive macro.
692 //! - `rustc` and [@estebank](https://github.com/estebank) for their
693 //!   state-of-the-art work in compiler diagnostics.
694 //! - [`ariadne`](https://crates.io/crates/ariadne) for pushing forward how
695 //!   _pretty_ these diagnostics can really look!
696 //!
697 //! ## License
698 //!
699 //! `miette` is released to the Rust community under the [Apache license
700 //! 2.0](./LICENSE).
701 //!
702 //! It also includes code taken from [`eyre`](https://github.com/yaahc/eyre),
703 //! and some from [`thiserror`](https://github.com/dtolnay/thiserror), also
704 //! under the Apache License. Some code is taken from
705 //! [`ariadne`](https://github.com/zesterer/ariadne), which is MIT licensed.
706 #[cfg(feature = "derive")]
707 pub use miette_derive::*;
708 
709 pub use error::*;
710 pub use eyreish::*;
711 #[cfg(feature = "fancy-no-backtrace")]
712 pub use handler::*;
713 pub use handlers::*;
714 pub use miette_diagnostic::*;
715 pub use named_source::*;
716 #[cfg(feature = "fancy")]
717 pub use panic::*;
718 pub use protocol::*;
719 
720 mod chain;
721 mod diagnostic_chain;
722 mod error;
723 mod eyreish;
724 #[cfg(feature = "fancy-no-backtrace")]
725 mod handler;
726 mod handlers;
727 #[cfg(feature = "fancy-no-backtrace")]
728 pub mod highlighters;
729 #[doc(hidden)]
730 pub mod macro_helpers;
731 mod miette_diagnostic;
732 mod named_source;
733 #[cfg(feature = "fancy")]
734 mod panic;
735 mod protocol;
736 mod source_impls;
737