nom icon indicating copy to clipboard operation
nom copied to clipboard

`convert_error` works for `&str` but for `&[u8]` not

Open ventaquil opened this issue 2 years ago • 6 comments

Prerequisites

Tested in Rust Playground (state for a day 04/01/2023).

  • Rust version : 1.66.0
  • nom version : 7.1.1

Description

I am working on parser that will work on bytes - so as an input I am using &[u8] instead of &str. I met some problems with using convert_error function.

Tried to cast VerboseError<&[u8]> into something like VerboseError<Vec<u8>> (or to cast into hex-encoded bytes) but it's not working too.

Maybe just I am doing something wrong?

Test case

&str

use nom::Finish;
use nom::bytes::complete::take;
use nom::error::{context, convert_error};

fn main() {
    let input = "abc";
    match context("str", take(4usize))(input).finish() {
        Ok((_, _)) => {},
        Err(error) => eprintln!("{}", convert_error(input, error)),
    }
}

Output

   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 0.94s
     Running `target/debug/playground`
0: at line 1, in Eof:
abc
^

1: at line 1, in str:
abc
^

&[u8]

use nom::Finish;
use nom::bytes::complete::take;
use nom::error::{context, convert_error};

fn main() {
    let input = b"abc";
    match context("&[u8]", take(4usize))(&input[..]).finish() {
        Ok((_, _)) => {},
        Err(error) => eprintln!("{}", convert_error(&input[..], error)),
    }
}

Output

Compiling playground v0.0.1 (/playground)
error[[E0271]](https://doc.rust-lang.org/stable/error-index.html#E0271): type mismatch resolving `<&[u8] as Deref>::Target == str`
   --> src/main.rs:15:39
    |
15  |         Err(error) => eprintln!("{}", convert_error(&input[..], error)),
    |                                       ^^^^^^^^^^^^^ expected `str`, found slice `[u8]`
    |
note: required by a bound in `convert_error`
   --> /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/nom-7.1.1/src/error.rs:251:42
    |
251 | pub fn convert_error<I: core::ops::Deref<Target = str>>(
    |                                          ^^^^^^^^^^^^ required by this bound in `convert_error`

For more information about this error, try `rustc --explain E0271`.
error: could not compile `playground` due to previous error

ventaquil avatar Jan 04 '23 19:01 ventaquil

Another case is unclear error message.

0: at line 1, in Eof:
abc
^

1: at line 1, in str:
abc
^

Shouldn't it be something like this?

0: at line 1, in str:
abc
^
expected 4 bytes, found 3

ventaquil avatar Jan 04 '23 19:01 ventaquil

Did you figure this out?

SkyLeite avatar Jun 12 '24 23:06 SkyLeite

Not at all. I just stopped using convert_error and casting to VerboseError<T>.

ventaquil avatar Jun 13 '24 08:06 ventaquil

I'm on the same boat, is there a good way of writing user-friendly errors for bytes that you could share?

kennedyoliveira avatar Jul 16 '24 02:07 kennedyoliveira

The convert_error also doesn't work with the nom_locate::LocatedSpan<&str>, because it dereferences to &str while the function requires it to dereference to str.

Is this project dead?

PonasKovas avatar Aug 11 '24 18:08 PonasKovas

Not dead, but fixing convert_error is not the priority right now. Every type, every field is public so you can write the conversion function you need, not everything needs to be included in the library right now

Geal avatar Aug 11 '24 19:08 Geal