rust-bakery / nom-bufreader

BufReader adapter for nom parsers
MIT License
9 stars 5 forks source link

Error: Implement From where Into. #7

Closed cheako closed 2 years ago

cheako commented 2 years ago

Am I doing something wrong to want this?

error[E0277]: `?` couldn't convert the error to `nom_bufreader::Error<err::Error>`
   --> crates/tor-socksproto/src/handshake.rs:33:15
    |
33  |         .await?
    |               ^ the trait `From<nom_bufreader::Error<nom::error::Error<&[u8]>>>` is not implemented for `nom_bufreader::Error<err::Error>`
    |
    = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
    = help: the following implementations were found:
              <nom_bufreader::Error<E> as From<std::io::Error>>
              <nom_bufreader::Error<err::Error> as From<err::Error>>
    = note: required because of the requirements on the impl of `FromResidual<std::result::Result<Infallible, nom_bufreader::Error<nom::error::Error<&[u8]>>>>` for `std::result::Result<(nom_bufreader::async_bufreader::BufReader<S>, WriteHalf<S>, SocksRequest), nom_bufreader::Error<err::Error>>`
note: required by `from_residual`
   --> /home/cheako/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/try_trait.rs:339:5
    |
339 |     fn from_residual(residual: R) -> Self;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

impl From<nom::error::Error<&[u8]>> for Error should be enough? How about this? I'm not really setup for a PR.

impl <F, E: Into<F>> From<Error<E>> for Error<F> {
    fn from(this: Error<E>) -> Self {
        match this {
            Error::Error(x) => Error::Error(x.into()),
            Error::Failure(x) => Error::Failure(x.into()),
            x => x,
        }
    }
}
cheako commented 2 years ago

I figured out that the errors I generate should be "inside" errors. What I have to figure out is how do I pass nom_bufreader::Error<nom::error::Error<&[u8]>> up the call chain? I need to turn it into an owned value.

cheako commented 2 years ago

Turns out, String works as a stop gap.