sharksforarms / deku

Declarative binary reading and writing: bit-level, symmetric, serialization/deserialization

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to properly debug Deku input data?

peterdk opened this issue · comments

I am new to Deku, and have a existing library that I am modifying, since it fails to parse a small set of input files (edge cases, since many thousands succeed).

I am wondering, how can I get some context from Deku, when it fails to parse a certain struct? I for example would want to know the position of the parser in the input Vec<u8>, and preferably the surrounding bytes.

Is this info available somehow?

I now get:

Foo::try_from(input).unwrap() => Error: Parse("Could not match enum variant id = 33 on enum `Bar`")

So I do know the input Bar value is 33, but there is no context, and I am having a very hard time debugging. There are a lot of elements that are used in this lib with Deku, also Vec's of these elements. So it's not straightforward and I'm wondering if I should just rewrite this library to manual parsing, so I at least get more insight in the parsing process and can actually debug.

Hopefully I am wrong and I can use Deku, it seems a nice library.

And is for example the partial succesful output of the parser available when it fails? So the parsed structures until point of failure?

Deku does have a logging feature which can be enabled to get tracing output, this may be helpful if you're trying to debug, there are some instructions here:

deku/src/lib.rs

Lines 296 to 316 in c905a64

# Debugging decoders with the `logging` feature.
If you are having trouble understanding what causes a Deku parse error, you may find the `logging`
feature useful.
To use it, you will need to:
- enable the `logging` Cargo feature for your Deku dependency
- import the `log` crate and a compatible logging library
For example, to log with `env_logger`, the dependencies in your `Cargo.toml` might look like:
```text
deku = { version = "*", features = ["logging"] }
log = "*"
env_logger = "*"
```
Then you'd call `env_logger::init()` or `env_logger::try_init()` prior to doing Deku decoding.
Deku uses the `trace` logging level, so if you run your application with `RUST_LOG=trace` in your
environment, you will see logging messages as Deku does its deserialising.

At the moment there is not a good facility for getting feedback in the form of errors on partial successful output or indexes into the input. This is a welcome improvement to the library.

Thanks, that's at least a entry point into understanding the issues I face. I'll try it out.

Let us know if that works for you or if you need more help, closing for now