Skip to content

How do I parse a binary format consisting of a hierarchy of chunks with different types? #122

Answered by jam1garner
philpax asked this question in Q&A
Discussion options

You must be logged in to vote

This (sections/chunks in a file with a chunk length in bytes) is a usecase I want to make more ergonomic, maybe some way to use the take io::Read adapter.... (see #121)

Anyways, how to deal with it in existing binrw, a few options:

  1. (Preferred imo) big enum with per-chunktype magic on each variant, and maybe a bit of repetition in the body
  2. br(repr(u32)) enum for type, then when you want to do per-type parsing you can use pre_assert on each variant of the underlying chunk contents parser to select which variant should be used for parsing

In either case you should have a fallthrough/default "unknown chunk type" variant that either reads in the raw bytes or skips them

Example 1

enum Chunk {

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@philpax
Comment options

@philpax
Comment options

Answer selected by philpax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants