dtolnay/anyhow

the following trait bounds were not satisfied: `(): anyhow::context::ext::StdError`

tekumara opened this issue · 1 comments

use anyhow::Context;

fn to_file_path(uri: &url::Url) -> anyhow::Result<std::path::PathBuf, anyhow::Error> {
    uri.to_file_path()
        .context("Cannot convert uri {} to file path", uri)?
}
error[[E0599]](https://doc.rust-lang.org/stable/error_codes/E0599.html): the method `context` exists for enum `Result<PathBuf, ()>`, but its trait bounds were not satisfied
 --> src/lib.rs:5:10
  |
4 | /     uri.to_file_path()
5 | |         .context("Cannot convert uri {} to file path", uri)?
  | |         -^^^^^^^ method cannot be called on `Result<PathBuf, ()>` due to unsatisfied trait bounds
  | |_________|
  | 
 --> /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/core/src/result.rs:502:1
  |
  = note: doesn't satisfy `Result<PathBuf, ()>: anyhow::Context<PathBuf, ()>`
  |
  = note: the following trait bounds were not satisfied:
          `(): anyhow::context::ext::StdError`
          which is required by `Result<PathBuf, ()>: anyhow::Context<PathBuf, ()>`

playground

Since () does not implement the std::error::Error trait we need to do this instead:

    uri.to_file_path()
        .map_err(|_| anyhow!("Cannot convert uri {} to file path", uri))?;