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