olson-sean-k / wax

Opinionated and portable globs that can be matched against paths and directory trees.
https://glob.guide
MIT License
115 stars 10 forks source link

Question: `miette` integration #15

Closed virtualritz closed 2 years ago

virtualritz commented 2 years ago

Could you give some sample code on how to get the output shown in the README, e.g.:

Error: glob::rule

  x malformed glob expression: adjacent zero-or-more wildcards `*` or `$`
   ,----
 1 | doc/**/*{.md,.tex,*.txt}
   :        |^^^^^^^^|^^^^^^^
   :        |        | `-- here
   :        |        `-- in this alternative
   :        `-- here
   `----

I added the resp. feature flag to wax but the error message I get doesn't contain the diagnostics. I am probably missing something obvious.

olson-sean-k commented 2 years ago

I believe the recommended way to get diagnostic output with miette is to use miette::Result and convert any non-Diagnostic errors into a miette::Result via miette::IntoDiagnostic.

I think something like this should work:

use miette::{IntoDiagnostic, Result};
use wax::Glob;

use crate::Whoops; // Some non-`Diagnostic` error.

fn get() -> Result<Glob<'static>> {
    let glob = Glob::new("**")?;
    if glob.has_semantic_literals() {
        // Explicit conversion via `into_diagnostic`.
        return Err(Whoops::new("whoops")).into_diagnostic();
    }
    Ok(glob)
}

fn main() -> Result<()> {
    // NOTE: Using `unwrap` and friends will not print the diagnostic. The error must propagate to the handler.
    let glob = get()?;
    Ok(())
}

I'll take a look at miette's documentation to see if there's something that I can link to from the README.

virtualritz commented 2 years ago

Maybe I am missing something. I tried this:

use miette::Result;
use wax::Glob;

fn get() -> Result<Glob<'static>> {
    let glob = Glob::new("doc/**/*{.md,.tex,*.txt}")?;
    Ok(glob)
}

fn main() -> Result<()> {
    let glob = get()?;
    Ok(())
}

Carg.toml has:

miette = { version = "4.2.0", features = ["fancy"] }
wax = { version = "0.4.0", features = ["diagnostics"] }

This doesn't build:

error[E0277]: the trait bound `GlobError<'_>: Diagnostic` is not satisfied
 --> src/main.rs:5:53
  |
5 |     let glob = Glob::new("doc/**/*{.md,.tex,*.txt}")?;
  |                                                     ^ the trait `Diagnostic` is not implemented for `GlobError<'_>`
  |
  = note: required because of the requirements on the impl of `From<GlobError<'_>>` for `ErrReport`
  = note: required because of the requirements on the impl of `FromResidual<Result<Infallible, GlobError<'_>>>` for `Result<Glob<'_>, ErrReport>`
olson-sean-k commented 2 years ago

I think the issue there is that Wax version 0.4.0 uses a miette dependency specification of ^3.0.0, so there are distinct (and incompatible) Diagnostic traits. It should work if miette is downgraded to 3.x.y. There's an open PR to upgrade Wax's miette dependency and I plan to land it before the next release.

virtualritz commented 2 years ago

Yes, using miette 3.2.0 makes it build and work as expected. Sorry about the noise. :)