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