diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4fafac3..233b6b4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,14 +10,18 @@ jobs: name: Check fmt & build docs runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v4 - name: Install Rust - uses: actions-rs/toolchain@v1 + uses: dtolnay/rust-toolchain@master with: - profile: minimal toolchain: stable components: rustfmt - override: true + - name: Install cargo-readme + run: cargo install cargo-readme + - name: Check doc consistency + shell: bash + run: diff -q README.md <(cargo readme) + || { echo "::error::Update lib.rs then use cargo-readme to update README.md"; exit 1; } - name: rustfmt run: cargo fmt --all -- --check - name: docs @@ -28,40 +32,53 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - rust: [1.56.0, stable] + features: [fancy, syntect-highlighter] + rust: [1.70.0, stable] os: [ubuntu-latest, macOS-latest, windows-latest] + exclude: + - features: syntect-highlighter + rust: 1.70.0 steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v4 - name: Install Rust - uses: actions-rs/toolchain@v1 + uses: dtolnay/rust-toolchain@master with: - profile: minimal toolchain: ${{ matrix.rust }} components: clippy - override: true - name: Clippy run: cargo clippy --all -- -D warnings - name: Run tests if: matrix.rust == 'stable' - run: cargo test --all --verbose --features fancy + run: cargo test --all --verbose --features ${{matrix.features}} - name: Run tests - if: matrix.rust == '1.56.0' - run: cargo test --all --verbose --features fancy no-format-args-capture + if: matrix.rust == '1.70.0' + run: cargo test --all --verbose --features ${{matrix.features}} no-format-args-capture + + wasm: + name: Check Wasm build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Install Rust + uses: dtolnay/rust-toolchain@master + with: + toolchain: stable + targets: wasm32-unknown-unknown + - name: Check wasm target + run: cargo check --target wasm32-unknown-unknown --features fancy-no-syscall miri: name: Miri runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v4 - name: Install Rust - uses: actions-rs/toolchain@v1 + uses: dtolnay/rust-toolchain@master with: - profile: minimal toolchain: nightly components: miri,rust-src - override: true - name: Run tests with miri env: MIRIFLAGS: -Zmiri-disable-isolation -Zmiri-strict-provenance @@ -75,13 +92,10 @@ jobs: os: [ubuntu-latest, macOS-latest, windows-latest] steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v4 - name: Install Rust - uses: actions-rs/toolchain@v1 + uses: dtolnay/rust-toolchain@master with: - profile: minimal toolchain: nightly - override: true - name: Run minimal version build - run: cargo build -Z minimal-versions --all-features - + run: cargo build -Z direct-minimal-versions --features fancy,no-format-args-capture diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b842de..b179baa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,109 @@ # `miette` Release Changelog + +## 7.2.0 (2024-03-07) + +### Features + +* **wasm:** add feature "fancy-no-syscall" for wasm targets (#349) ([328bf379](https://github.com/zkat/miette/commit/328bf3792213fc0bed94e72a39acb722b65141dd)) + +### Bug Fixes + +* **label-collections:** Label collection fixes and cleanup (#343) ([75fea093](https://github.com/zkat/miette/commit/75fea0935e495d0215518c80d32dd820910982e3)) +* **invalid span:** skip the snippet when read_span fails (#347) ([7d9dfc6e](https://github.com/zkat/miette/commit/7d9dfc6e8e591f9606c3da55bd8465962358b20f)) +* **redundant-import:** fix a warning and CI failure in nightly (#348) ([6ea86a22](https://github.com/zkat/miette/commit/6ea86a2248854acf88df345814b6c97d31b8b4d9)) + + +## 7.1.0 (2024-02-16) + +### Features + +* **derive:** enable more boxed types to be #[diagnostic_source] (#338) ([c2f06f6c](https://github.com/zkat/miette/commit/c2f06f6cca15cbdd083dbff3d46b7729056ac6a4)) +* **source:** derive common traits for NamedSource, SourceSpan, and SourceOffset (#340) ([6f09250c](https://github.com/zkat/miette/commit/6f09250cca14561f07fba899a8e6d3c0df14230e)) +* **collection:** add support for collection of labels (#341) ([03060245](https://github.com/zkat/miette/commit/03060245d816a53a33209e6b7e1c3c42948e9962)) + +### Bug Fixes + +* **tests:** revert test-breaking changes of e5c7ae4 (#339) ([6e829f8c](https://github.com/zkat/miette/commit/6e829f8c0ce2fc7bb2fc4041e6a6072f12db1f71)) + + +## 7.0.0 (2024-02-05) + +This is a small breaking release on the heels of 6.0 because I neglected to +bump owo-colors. I figured it's a good time to do it, before 6.0 gets more +widely used. + +### Features + +* **fancy:** Add option to change the link display text (#335) ([c7144ee5](https://github.com/zkat/miette/commit/c7144ee513bf8f06c5f7d89c45436802994a51fc)) +* **deps:** bump dependencies ([a4011d17](https://github.com/zkat/miette/commit/a4011d174c40acbba5b0176db7cb71ec5ca0cb49)) + * **BREAKING CHANGE**: This bumps owo-colors to 4.0, which is a breaking change because we expose its styles as part of the graphical renderer API + + +## 6.0.1 (2024-02-04) + +### Bug Fixes + +* **graphical:** oops. Fix theme issue ([8b46679c](https://github.com/zkat/miette/commit/8b46679c3647e1455d91b4c68743c619fb3f3eb3)) +* **fmt:** remove nightly-only fmt flags ([1fa7f524](https://github.com/zkat/miette/commit/1fa7f5241fb91d2e5bad9b0e26bcc7cd5f9011f1)) +* **highlighter:** ugh, missed another spot ([ab7c066e](https://github.com/zkat/miette/commit/ab7c066e7675d8c7ecb956000d278fc31f3bc6a1)) + + +## 6.0.0 (2024-02-04) + +The long-awaited 6.0 release of `miette` is here, with TONS of goodies, not +least of which is syntax highlighting support! + +It also comes with a few breaking changes so make sure to check below and +update your code as needed! + +### Features + +* **labels:** Add support for primary label in specifying line/col information (#291) ([db0b7e40](https://github.com/zkat/miette/commit/db0b7e403a5ae52ae360991b6508490d8c579886)) +* **derive:** Allow optional sources in derive (#301) ([88d00e0e](https://github.com/zkat/miette/commit/88d00e0e20bf95e03b8f81dcd5adf38c917e190e)) +* **derive:** Make `miette-derive` be able to be turned off (#304) ([c7ba5b7e](https://github.com/zkat/miette/commit/c7ba5b7e52e05991cecd3ca925c710bbe49850b9)) +* **graphical:** Expose additional `textwrap` options (#321) ([fd77257c](https://github.com/zkat/miette/commit/fd77257cee0f5d03aa7dccb4ba8cbaa40c1a88c6)) +* **graphical:** support rendering labels that contain newlines (#318) ([865d67c8](https://github.com/zkat/miette/commit/865d67c8dda119ddd03ac43be22f4fa272a9f433)) +* **graphical:** Add `wrap_lines: bool` option allowing wrapping be disabled entirely (#328) ([b0744462](https://github.com/zkat/miette/commit/b0744462adbbfbb6d845f382db36be883c7f3c45)) +* **graphical:** render disjoint snippets separately for cleaner output (#324) ([19c22143](https://github.com/zkat/miette/commit/19c22143cb544616046784e35c5e78cc5b881289)) +* **deps:** Bump terminal-size to v0.3.0 (#308) ([c0a298e5](https://github.com/zkat/miette/commit/c0a298e5a8d699acf9fcd61b5d5fa4f6279a47ab)) + * **BREAKING CHANGE**: This requires an MSRV bump to 1.70.0. +* **source-code:** Don't override provided source code (#300) ([0d5c2ce7](https://github.com/zkat/miette/commit/0d5c2ce7536b0ea205346595d8a00d00bfb6cbd2)) + * **BREAKING CHANGE**: Source code is no longer overridden if it was provided by the diagnostic's own `source_code()` impl. +* **source:** use `usize` for length (#265) ([fad0e76a](https://github.com/zkat/miette/commit/fad0e76ad2e19d5cac13cf8324338aca0d623d93)) + * **BREAKING CHANGE**: This changes `SourceSpan`'s length type to `usize`. +* **source:** Allow inner source type of a NamedSource to be borrowed (#254) ([1df3b1a5](https://github.com/zkat/miette/commit/1df3b1a537f2e54cd40ec45f5cd851337a22e95a)) + * **BREAKING CHANGE**: This makes the `NamedSource` type generic over its `Source` type, instead of boxing it. +* **highlighting:** add syntax highlighting support with syntect crate (#313) ([e65d0a78](https://github.com/zkat/miette/commit/e65d0a78cc639653f061a45d8ce35b1a3551ade7)) +* **deps:** remove is-terminal dep in favor of `std::io::IsTerminal` ([e5c7ae46](https://github.com/zkat/miette/commit/e5c7ae469e40a8bc102e1fca3b8fd4b2ec137696)) +* **deps:** remove once_cell dep in favor of `std::sync::OnceLock` ([4c48584f](https://github.com/zkat/miette/commit/4c48584f304414c6924bede3308b455cfef60749)) + * **BREAKING CHANGE**: This requires an MSRV bump to 1.70.0. +* **deps:** bump some semver-breaking deps to newer versions ([29d000f2](https://github.com/zkat/miette/commit/29d000f201b259a056867a2876384f97653a6e9e)) +* **MSRV:** Actually bump the MSRV to 1.70.0 ([ab59a7bc](https://github.com/zkat/miette/commit/ab59a7bc9bceace5761a862ee2ebff3e5943b12f)) + +### Bug Fixes + +* **misc:** Improve ci and fix clippy (#290) ([cc81382a](https://github.com/zkat/miette/commit/cc81382a6070dd226a20e4a39518d88e957ac0e1)) +* **tests:** Fix `cargo test` with default features. (#294) ([1f448e47](https://github.com/zkat/miette/commit/1f448e47751d0f914134b0e9138fdb1a5a95d55c)) +* **clippy:** Add missing semicolons where nothing is returned. (#293) ([06b34823](https://github.com/zkat/miette/commit/06b348230aaf153b8b050322f05e5d185351d2d1)) +* **graphical:** Extend error text span to whole code points (#312) ([a8b4ae01](https://github.com/zkat/miette/commit/a8b4ae012aa0cf03b53a18f013c2b3f76c5040e7)) +* **formatting:** Fix formatting bug when an empty span is not aligned to a char boundary (#314) ([3d6f903d](https://github.com/zkat/miette/commit/3d6f903df0e7c9d0eb9a1fdbbf0028bab5496429)) +* **docs:** add example to README and docs fixing #96 (#319) ([251d6d59](https://github.com/zkat/miette/commit/251d6d59292397458328ef57fb7957faedafd019)) +* **graphical:** rendering bug on small spans in large spans (#316) ([7ff4f874](https://github.com/zkat/miette/commit/7ff4f874d693a665af4df40f4e94505013e3e262)) +* **graphical:** render cause chains for inner errors (#330) ([cb2ae2e1](https://github.com/zkat/miette/commit/cb2ae2e18b446a5e90885faf8a30b5672c307df8)) +* **handler:** remove the two extra `is_terminal` sys call from `MietteHandlerOpts::build` (#325) ([f1dc89c0](https://github.com/zkat/miette/commit/f1dc89c07640445d224b61ef96c6b25fcdf62dee)) + +### Documentation + +* **README:** Move import of `NamedResult` to where it is used (#309) ([d37ada87](https://github.com/zkat/miette/commit/d37ada876a5831d3f47622274e334c9a24aa5d2b)) + + +## 5.10.0 (2023-07-16) + +### Features + +* **protocol:** add StdError impl for Box (#273) ([2e3e5c9d](https://github.com/zkat/miette/commit/2e3e5c9d15e234495369e9b47d032644dd5664ad)) + ## 5.9.0 (2023-05-18) diff --git a/Cargo.toml b/Cargo.toml index 160556f..057bd9e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "miette" -version = "5.9.0" +version = "7.2.0" authors = ["Kat Marchán "] description = "Fancy diagnostic reporting library and protocol for us mere mortals who aren't compiler hackers." categories = ["rust-patterns"] @@ -9,53 +9,61 @@ documentation = "https://docs.rs/miette" license = "Apache-2.0" readme = "README.md" edition = "2018" -rust-version = "1.56.0" +rust-version = "1.70.0" exclude = ["images/", "tests/", "miette-derive/"] [dependencies] -thiserror = "1.0.40" -miette-derive = { path = "miette-derive", version = "=5.9.0" } -once_cell = "1.8.0" -unicode-width = "0.1.9" +thiserror = "1.0.56" +miette-derive = { path = "miette-derive", version = "=7.2.0", optional = true } +unicode-width = "0.1.11" +cfg-if = "1.0.0" -owo-colors = { version = "3.0.0", optional = true } -is-terminal = { version = "0.4.0", optional = true } -textwrap = { version = "0.15.0", optional = true } -supports-hyperlinks = { version = "2.0.0", optional = true } -supports-color = { version = "2.0.0", optional = true } -supports-unicode = { version = "2.0.0", optional = true } -backtrace = { version = "0.3.61", optional = true } -terminal_size = { version = "0.1.17", optional = true } +owo-colors = { version = "4.0.0", optional = true } +textwrap = { version = "0.16.0", optional = true } +supports-hyperlinks = { version = "3.0.0", optional = true } +supports-color = { version = "3.0.0", optional = true } +supports-unicode = { version = "3.0.0", optional = true } +backtrace = { version = "0.3.69", optional = true } +terminal_size = { version = "0.3.0", optional = true } backtrace-ext = { version = "0.2.1", optional = true } -serde = { version = "1.0.162", features = ["derive"], optional = true } +serde = { version = "1.0.196", features = ["derive"], optional = true } +syntect = { version = "5.1.0", optional = true } [dev-dependencies] -semver = "1.0.4" +semver = "1.0.21" # Eyre devdeps futures = { version = "0.3", default-features = false } -indenter = "0.3.0" +indenter = "0.3.3" rustversion = "1.0" -trybuild = { version = "1.0.19", features = ["diff"] } -syn = { version = "2.0", features = ["full"] } -regex = "1.5" +trybuild = { version = "1.0.89", features = ["diff"] } +syn = { version = "2.0.48", features = ["full"] } +regex = "1.10" lazy_static = "1.4" -serde_json = "1.0.64" +serde_json = "1.0.113" +strip-ansi-escapes = "0.2.0" [features] -default = [] +default = ["derive"] +derive = ["miette-derive"] no-format-args-capture = [] -fancy-no-backtrace = [ +fancy-base = [ "owo-colors", - "is-terminal", "textwrap", +] +fancy-no-syscall = [ + "fancy-base", +] +fancy-no-backtrace = [ + "fancy-base", "terminal_size", "supports-hyperlinks", "supports-color", "supports-unicode", ] fancy = ["fancy-no-backtrace", "backtrace", "backtrace-ext"] +syntect-highlighter = ["fancy-no-backtrace", "syntect"] [workspace] members = ["miette-derive"] diff --git a/README.md b/README.md index f49dd69..ec74050 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ You run miette? You run her code like the software? Oh. Oh! Error code for coder! Error code for One Thousand Lines! -## About +### About `miette` is a diagnostic library for Rust. It includes a series of traits/protocols that allow you to hook into its error reporting facilities, @@ -13,18 +13,20 @@ can print out like this (or in any format you like!): 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:
 \
-Error: Received some bad JSON from the source. Unable to parse.
-    Caused by: missing field `foo` at line 1 column 1700
+diagnostic error code: oops::my::bad (link)
+Error: oops!
 \
-Begin snippet for https://api.nuget.org/v3/registration5-gz-semver2/json.net/index.json starting
-at line 1, column 1659
+Begin snippet for bad_file.rs starting
+at line 2, column 3
 \
-snippet line 1: gs":["json"],"title":"","version":"1.0.0"},"packageContent":"https://api.nuget.o
-    highlight starting at line 1, column 1699: last parsing location
+snippet line 1: source
 \
-diagnostic help: This is a bug. It might be in ruget, or it might be in the
-source you're using, but it's definitely a bug and should be reported.
-diagnostic error code: ruget::api::bad_json
+snippet line 2:  text
+    highlight starting at line 1, column 3: This bit here
+\
+snippet line 3: here
+\
+diagnostic help: try doing it better next time?
 > **NOTE: You must enable the `"fancy"` crate feature to get fancy report @@ -32,7 +34,7 @@ output like in the screenshots above.** You should only do this in your toplevel crate, as the fancy feature pulls in a number of dependencies that libraries and such might not want. -## Table of Contents +### Table of Contents - [About](#about) - [Features](#features) @@ -44,14 +46,18 @@ libraries and such might not want. - [... in `main()`](#-in-main) - [... diagnostic code URLs](#-diagnostic-code-urls) - [... snippets](#-snippets) + - [... help text](#-help-text) + - [... severity level](#-severity-level) - [... multiple related errors](#-multiple-related-errors) - [... delayed source code](#-delayed-source-code) - [... handler options](#-handler-options) - [... dynamic diagnostics](#-dynamic-diagnostics) + - [... syntax highlighting](#-syntax-highlighting) + - [... collection of labels](#-collection-of-labels) - [Acknowledgements](#acknowledgements) - [License](#license) -## Features +### Features - Generic [`Diagnostic`] protocol, compatible (and dependent on) [`std::error::Error`]. @@ -76,7 +82,7 @@ the following features: - Cause chain printing - Turns diagnostic codes into links in [supported terminals](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda). -## Installing +### Installing ```sh $ cargo add miette @@ -88,7 +94,7 @@ If you want to use the fancy printer in all these screenshots: $ cargo add miette --features fancy ``` -## Example +### Example ```rust /* @@ -110,7 +116,7 @@ struct MyBad { // The Source that we're gonna be printing snippets out of. // This can be a String if you don't have or care about file names. #[source_code] - src: NamedSource, + src: NamedSource, // Snippets and highlights can be included in the diagnostic! #[label("This bit here")] bad_bit: SourceSpan, @@ -174,9 +180,9 @@ snippet line 3: here diagnostic help: try doing it better next time? "> -## Using +### Using -### ... in libraries +#### ... in libraries `miette` is _fully compatible_ with library usage. Consumers who don't know about, or don't want, `miette` features can safely use its error types as @@ -191,7 +197,7 @@ the trait directly, just like with `std::error::Error`. ```rust // lib/error.rs -use miette::Diagnostic; +use miette::{Diagnostic, SourceSpan}; use thiserror::Error; #[derive(Error, Diagnostic, Debug)] @@ -203,6 +209,18 @@ pub enum MyLibError { #[error("Oops it blew up")] #[diagnostic(code(my_lib::bad_code))] BadThingHappened, + + #[error(transparent)] + // Use `#[diagnostic(transparent)]` to wrap another [`Diagnostic`]. You won't see labels otherwise + #[diagnostic(transparent)] + AnotherError(#[from] AnotherError), +} + +#[derive(Error, Diagnostic, Debug)] +#[error("another error")] +pub struct AnotherError { + #[label("here")] + pub at: SourceSpan } ``` @@ -210,7 +228,7 @@ Then, return this error type from all your fallible public APIs. It's a best practice to wrap any "external" error types in your error `enum` instead of using something like [`Report`] in a library. -### ... in application code +#### ... in application code Application code tends to work a little differently than libraries. You don't always need or care to define dedicated error wrappers for errors @@ -252,8 +270,7 @@ pub fn some_tool() -> Result { } ``` -To construct your own simple adhoc error use the [`miette!`] macro: - +To construct your own simple adhoc error use the [miette!] macro: ```rust // my_app/lib/my_internal_file.rs use miette::{miette, IntoDiagnostic, Result, WrapErr}; @@ -266,8 +283,9 @@ pub fn some_tool() -> Result { .map_err(|_| miette!("Invalid version {}", version))?) } ``` +There are also similar [bail!] and [ensure!] macros. -### ... in `main()` +#### ... in `main()` `main()` is just like any other part of your application-internal code. Use `Result` as your return value, and it will pretty-print your diagnostics @@ -297,7 +315,24 @@ enabled: miette = { version = "X.Y.Z", features = ["fancy"] } ``` -### ... diagnostic code URLs +Another way to display a diagnostic is by printing them using the debug formatter. +This is, in fact, what returning diagnostics from main ends up doing. +To do it yourself, you can write the following: + +```rust +use miette::{IntoDiagnostic, Result}; +use semver::Version; + +fn just_a_random_function() { + let version_result: Result = "1.2.x".parse().into_diagnostic(); + match version_result { + Err(e) => println!("{:?}", e), + Ok(version) => println!("{}", version), + } +} +``` + +#### ... diagnostic code URLs `miette` supports providing a URL for individual diagnostics. This URL will be displayed as an actual link in supported terminals, like so: @@ -350,7 +385,7 @@ use thiserror::Error; struct MyErr; ``` -### ... snippets +#### ... snippets Along with its general error handling and reporting features, `miette` also includes facilities for adding error spans/annotations/labels to your @@ -434,7 +469,20 @@ let err = Foo { }; ``` -### ... multiple related errors +#### ... severity level +`miette` provides a way to set the severity level of a diagnostic. + +```rust +use miette::Diagnostic; +use thiserror::Error; + +#[derive(Debug, Diagnostic, Error)] +#[error("welp")] +#[diagnostic(severity(Warning))] +struct Foo; +``` + +#### ... multiple related errors `miette` supports collecting multiple errors into a single diagnostic, and printing them all together nicely. @@ -454,7 +502,7 @@ struct MyError { } ``` -### ... delayed source code +#### ... delayed source code Sometimes it makes sense to add source code to the error message later. One option is to use [`with_source_code()`](Report::with_source_code) @@ -537,7 +585,7 @@ fn main() -> miette::Result<()> { } ``` -### ... Diagnostic-based error sources. +#### ... Diagnostic-based error sources. When one uses the `#[source]` attribute on a field, that usually comes from `thiserror`, and implements a method for @@ -570,7 +618,7 @@ struct MyError { struct OtherError; ``` -### ... handler options +#### ... handler options [`MietteHandler`] is the default handler, and is very customizable. In most cases, you can simply use [`MietteHandlerOpts`] to tweak its behavior @@ -586,15 +634,17 @@ miette::set_hook(Box::new(|_| { .unicode(false) .context_lines(3) .tab_width(4) + .break_words(true) .build(), ) })) + ``` See the docs for [`MietteHandlerOpts`] for more details on what you can customize! -### ... dynamic diagnostics +#### ... dynamic diagnostics If you... - ...don't know all the possible errors upfront @@ -603,9 +653,10 @@ then you may want to use [`miette!`], [`diagnostic!`] macros or [`MietteDiagnostic`] directly to create diagnostic on the fly. ```rust + let source = "2 + 2 * 2 = 8".to_string(); let report = miette!( - labels = vec[ + labels = vec![ LabeledSpan::at(12..13, "this should be 6"), ], help = "'*' has greater precedence than '+'", @@ -614,26 +665,108 @@ let report = miette!( println!("{:?}", report) ``` -## Acknowledgements +#### ... syntax highlighting + +`miette` can be configured to highlight syntax in source code snippets. + + + +To use the built-in highlighting functionality, you must enable the +`syntect-highlighter` crate feature. When this feature is enabled, `miette` will +automatically use the [`syntect`] crate to highlight the `#[source_code]` +field of your [`Diagnostic`]. + +Syntax detection with [`syntect`] is handled by checking 2 methods on the [`SpanContents`] trait, in order: +* [language()](SpanContents::language) - Provides the name of the language + as a string. For example `"Rust"` will indicate Rust syntax highlighting. + You can set the language of the [`SpanContents`] produced by a + [`NamedSource`] via the [`with_language`](NamedSource::with_language) + method. +* [name()](SpanContents::name) - In the absence of an explicitly set + language, the name is assumed to contain a file name or file path. + The highlighter will check for a file extension at the end of the name and + try to guess the syntax from that. + +If you want to use a custom highlighter, you can provide a custom +implementation of the [`Highlighter`](highlighters::Highlighter) +trait to [`MietteHandlerOpts`] by calling the +[`with_syntax_highlighting`](MietteHandlerOpts::with_syntax_highlighting) +method. See the [`highlighters`] module docs for more details. + +#### ... collection of labels + +When the number of labels is unknown, you can use a collection of `SourceSpan` +(or any type convertible into `SourceSpan`). For this, add the `collection` +parameter to `label` and use any type than can be iterated over for the field. + +```rust +#[derive(Debug, Diagnostic, Error)] +#[error("oops!")] +struct MyError { + #[label("main issue")] + primary_span: SourceSpan, + + #[label(collection, "related to this")] + other_spans: Vec>, +} + +let report: miette::Report = MyError { + primary_span: (6, 9).into(), + other_spans: vec![19..26, 30..41], +}.into(); + +println!("{:?}", report.with_source_code("About something or another or yet another ...".to_string())); +``` + +A collection can also be of `LabeledSpan` if you want to have different text +for different labels. Labels with no text will use the one from the `label` +attribute + +```rust +#[derive(Debug, Diagnostic, Error)] +#[error("oops!")] +struct MyError { + #[label("main issue")] + primary_span: SourceSpan, + + #[label(collection, "related to this")] + other_spans: Vec, // LabeledSpan +} + +let report: miette::Report = MyError { + primary_span: (6, 9).into(), + other_spans: vec![ + LabeledSpan::new(None, 19, 7), // Use default text `related to this` + LabeledSpan::new(Some("and also this".to_string()), 30, 11), // Use specific text + ], +}.into(); + +println!("{:?}", report.with_source_code("About something or another or yet another ...".to_string())); +``` + +### MSRV + +This crate requires rustc 1.70.0 or later. + +### Acknowledgements `miette` was not developed in a void. It owes enormous credit to various other projects and their authors: -- [`anyhow`](http://crates.io/crates/anyhow) and - [`color-eyre`](https://crates.io/crates/color-eyre): these two - enormously influential error handling libraries have pushed forward the - experience of application-level error handling and error reporting. - `miette`'s `Report` type is an attempt at a very very rough version of - their `Report` types. -- [`thiserror`](https://crates.io/crates/thiserror) for setting the - standard for library-level error definitions, and for being the - inspiration behind `miette`'s derive macro. +- [`anyhow`](http://crates.io/crates/anyhow) and [`color-eyre`](https://crates.io/crates/color-eyre): + these two enormously influential error handling libraries have pushed + forward the experience of application-level error handling and error + reporting. `miette`'s `Report` type is an attempt at a very very rough + version of their `Report` types. +- [`thiserror`](https://crates.io/crates/thiserror) for setting the standard + for library-level error definitions, and for being the inspiration behind + `miette`'s derive macro. - `rustc` and [@estebank](https://github.com/estebank) for their state-of-the-art work in compiler diagnostics. - [`ariadne`](https://crates.io/crates/ariadne) for pushing forward how _pretty_ these diagnostics can really look! -## License +### License `miette` is released to the Rust community under the [Apache license 2.0](./LICENSE). @@ -652,7 +785,7 @@ under the Apache License. Some code is taken from [`MietteHandler`]: https://docs.rs/miette/latest/miette/struct.MietteHandler.html [`MietteDiagnostic`]: https://docs.rs/miette/latest/miette/struct.MietteDiagnostic.html [`Report`]: https://docs.rs/miette/latest/miette/struct.Report.html -[`ReportHandler`]: https://docs.rs/miette/latest/miette/struct.ReportHandler.html +[`ReportHandler`]: https://docs.rs/miette/latest/miette/trait.ReportHandler.html [`Result`]: https://docs.rs/miette/latest/miette/type.Result.html -[`SourceCode`]: https://docs.rs/miette/latest/miette/struct.SourceCode.html +[`SourceCode`]: https://docs.rs/miette/latest/miette/trait.SourceCode.html [`SourceSpan`]: https://docs.rs/miette/latest/miette/struct.SourceSpan.html diff --git a/README.tpl b/README.tpl index d598eb8..f1126f1 100644 --- a/README.tpl +++ b/README.tpl @@ -12,7 +12,7 @@ [`MietteHandler`]: https://docs.rs/miette/latest/miette/struct.MietteHandler.html [`MietteDiagnostic`]: https://docs.rs/miette/latest/miette/struct.MietteDiagnostic.html [`Report`]: https://docs.rs/miette/latest/miette/struct.Report.html -[`ReportHandler`]: https://docs.rs/miette/latest/miette/struct.ReportHandler.html +[`ReportHandler`]: https://docs.rs/miette/latest/miette/trait.ReportHandler.html [`Result`]: https://docs.rs/miette/latest/miette/type.Result.html -[`SourceCode`]: https://docs.rs/miette/latest/miette/struct.SourceCode.html +[`SourceCode`]: https://docs.rs/miette/latest/miette/trait.SourceCode.html [`SourceSpan`]: https://docs.rs/miette/latest/miette/struct.SourceSpan.html diff --git a/clippy.toml b/clippy.toml index 0d369b5..1645c19 100644 --- a/clippy.toml +++ b/clippy.toml @@ -1 +1 @@ -msrv = "1.56.0" +msrv = "1.70.0" diff --git a/miette-derive/Cargo.toml b/miette-derive/Cargo.toml index 461a328..e8082a0 100644 --- a/miette-derive/Cargo.toml +++ b/miette-derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "miette-derive" -version = "5.9.0" +version = "7.2.0" authors = ["Kat Marchán "] edition = "2018" license = "Apache-2.0" @@ -11,6 +11,6 @@ repository = "https://github.com/zkat/miette" proc-macro = true [dependencies] -proc-macro2 = "1.0" -quote = "1.0" -syn = "2.0.11" +proc-macro2 = "1.0.78" +quote = "1.0.35" +syn = "2.0.48" diff --git a/miette-derive/src/label.rs b/miette-derive/src/label.rs index e0bc70a..ab2ceac 100644 --- a/miette-derive/src/label.rs +++ b/miette-derive/src/label.rs @@ -16,14 +16,23 @@ use crate::{ pub struct Labels(Vec