sharksforarms / deku

Declarative binary reading and writing: bit-level, symmetric, serialization/deserialization
Apache License 2.0
1.05k stars 54 forks source link

How to properly debug Deku input data? #401

Closed peterdk closed 5 months ago

peterdk commented 5 months ago

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.

peterdk commented 5 months ago

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

sharksforarms commented 5 months ago

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: https://github.com/sharksforarms/deku/blob/c905a644f2ef091757d696e5a6c5839c98e7424b/src/lib.rs#L296-L316

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.

peterdk commented 5 months ago

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

sharksforarms commented 5 months ago

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