zesterer / ariadne

A fancy diagnostics & error reporting crate
https://crates.io/crates/ariadne
MIT License
1.61k stars 72 forks source link

FileCache doesn't seem to work? #40

Open trombonehero opened 1 year ago

trombonehero commented 1 year ago

I'm attempting to use ariadne::FileCache with Report::write (or print or eprint), but it doesn't seem to work for me.FileCache implements Cache<Path> but not Cache<PathBuf>, and as std::path::Path isn't sized, I can't pass a Path directly into Report::build or a Label. Using &Path in the following minimal example:

fn example() {
    use ariadne::*;

    let mut fc = FileCache::default();
    let path = std::path::PathBuf::from("example.txt").as_path();

    let mut builder = Report::build(ReportKind::Error, path, 10)
        .with_message("message")
        .with_label(Label::new((path, 10..20)).with_message("something bad"));

    builder.finish().eprint(fc).unwrap();
}

yields the following error:

error[E0277]: the trait bound `ariadne::FileCache: Cache<&Path>` is not satisfied
   --> src/parsing.rs:19:29
    |
19  |     builder.finish().eprint(fc).unwrap();
    |                      ------ ^^ the trait `Cache<&Path>` is not implemented for `ariadne::FileCache`
    |                      |
    |                      required by a bound introduced by this call
    |
    = help: the trait `Cache<Path>` is implemented for `ariadne::FileCache`
note: required by a bound in `ariadne::Report::<S>::eprint`
   --> /home/jon/.cargo/registry/src/github.com-1ecc6299db9ec823/ariadne-0.1.5/src/lib.rs:159:22
    |
159 |     pub fn eprint<C: Cache<S::SourceId>>(&self, cache: C) -> io::Result<()> {
    |                      ^^^^^^^^^^^^^^^^^^ required by this bound in `ariadne::Report::<S>::eprint`

For more information about this error, try `rustc --explain E0277`.

This is using ariadne 0.1.5.

Thanks!

zesterer commented 1 year ago

Argh, this is a frustrating oversight. For now, FnCache or a custom implementation of Cache should be sufficient. This API does require a pretty significant redesign though. I'll see whether I get the opportunity to do this over the next week!

ggriffiniii commented 1 year ago

I just stumbled on this same issue. If nothing else can FileCache be removed until a solution exists. It would have saved me a lot of time. As it was I saw what was clearly what I wanted and was banging my head against the wall trying to figure out how to get it to work, where I would have just used the FnCache if that was the only option.

ggriffiniii commented 1 year ago

Also it looks like FnCache doesn't work with Path/PathBuf either due to the requirement for Display that Path doesn't meet. So a custom implementation is required to work with Path/PathBuf. Not a big deal, just pointing it out.

zesterer commented 1 year ago

I'm slowly gearing up to a crate rewrite, so I'll add this to the list of things to fix when this happens. Thanks for the reminder!