duesee / abnf

A nom-based ABNF parser.
Apache License 2.0
17 stars 3 forks source link

`Error` is not implemented for `ParseError` #13

Closed xtofs closed 3 years ago

xtofs commented 3 years ago

For the rulelist function to work together with the ? operator the abnf::error::ParseError struct needs to implement some traits.

At the moment using it with lead to an error:

the trait bound `ParseError: std::error::Error` is not satisfiedrustc(E0277)

Not sure how to do it myself but some comments can be found here see https://stackoverflow.com/questions/59568278/why-does-the-operator-report-the-error-the-trait-bound-noneerror-error-is-no and/or https://doc.rust-lang.org/src/std/io/error.rs.html#549

duesee commented 3 years ago

Could you please provide a minimal example?

xtofs commented 3 years ago
use abnf::rulelist;
use std::error::Error;

fn main() -> std::result::Result<(), Box<dyn Error>> {
    let input = std::fs::read_to_string("foo.abnf")?;
    println!("{:#?}", input);

    let rules =  rulelist("a = b / c\nc = *(d e)\n") ?;
    println!("{:#?}", rules);

    Ok(())
}
$ cargo build
   Compiling abnf-repro v0.1.0 (~/abnf-repro)
error[E0277]: the trait bound `abnf::error::ParseError: std::error::Error` is not satisfied
 --> src/main.rs:8:35
  |
8 |     let rules =  rulelist(&input) ?;
  |                                   ^ the trait `std::error::Error` is not implemented for `abnf::error::ParseError`
  |
  = note: required because of the requirements on the impl of `From<abnf::error::ParseError>` for `Box<dyn std::error::Error>`
  = note: required by `from`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `abnf-repro`

To learn more, run the command again with --verbose.

version used:

[dependencies]
abnf = "0.10.0"
duesee commented 3 years ago

Ah, I see. By using the ?-operator, you basically tell Rust to convert ParseError into Box<dyn Error>. This is only possible when ParseError implements Error. However,

the trait `std::error::Error` is not implemented for `abnf::error::ParseError

tells you that this is not the case. Would you like to implement std::error::Error for ParseError in src/error.rs and open a Pull Request? This should be easy to do, because you don't need to implement any trait-method. If you are not in a position to do it, I can do it myself tomorrow.

duesee commented 3 years ago

ParseError now implements Error (see 6fa4978fb9a4b81e8ea1d984983fbf0728d1c539).