nom
nom copied to clipboard
`convert_error` works for `&str` but for `&[u8]` not
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
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
Did you figure this out?
Not at all. I just stopped using convert_error
and casting to VerboseError<T>
.
I'm on the same boat, is there a good way of writing user-friendly errors for bytes that you could share?
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?
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