Parsing binary data

Can anyone recommend crates for parsing simple binary data? I’d like something more declarative than manually writing out byteorder reads, if possible. I only need packed structs containing simple types: integers of various lengths, bitfields, arrays and other structures. Also length prefixed strings but they’re rare enough that doing it manually isn’t too bad.

In Python I’m using ctypes structures so something similar would ease the learning curve for me but I’m not married to that system if there’s something better.

You might have a look at nom

If you have a very simple struct case, ::bincode may be faster to set up than ::nom, but the latter is far more flexible.

Thanks to you both.

I think nom would seem to be the better option in my case. It looks a bit more complex than I need but that might just be my unfamiliarity with the syntax.

If your data can be represented as a C struct, then you could also write a structure definition marked with #[repr(C)] and then transmute_copy bytes. It may be more natural if data originates from C world. Also you could use bindgen to automatically generate the struct definition from header file.

I’ve had great success using scroll for this exact purpose. It provides a #[derive(Pread)] that makes using it with your own structs really simple. You can see an example of its use in my minidump crate:

I created a parser for network packets from OpenTTD. It uses serde to define the model and structure separately. You might take some inspiration from it.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.