I didn’t do much this week but progess is progress :)
I added the support for long frames. According documentation, long frame is defined by flag (Bit 1, “second one from right” must be 1). If the flag is set, size will be 8 octets, and body will be size octets. (So if size is 256, body will be 256 octets).
First lets look at tests. First change you can see is that I removed “reserved” from flags as it is not needed at all and obscured the data. Next I renamed “command” key to “type” - as it reflects better what it is - it indicated the type of frame - if it is a command or message. Next change (in decode) is that body is still in binary (inside << >>). I guess I will work on that later, but was needed in some changes. I added 2 tests to correctly decode long frames, for both command and message. What’s important is :: size(64). It indicates the size of binary element. If not specified it will be default (8). What’s also important (and quite logical) size have to be divisible by 8.
Now onward to code itself. There’s only one public decode function, but it calls one of the private ones depending of the flag. There’s a pattern matching on long key, and binary data. As you can see here I used ::size accordingly. What’s worth noting is if you want to match against unknown sized binary data you can use ::binary. Here I used it for body and for “rest” in main function - as before getting flags I don’t care about size and body.
And that’s it! Not a lot, but I learnt a bit when it comes to working with binary data in Elixir and made code a bit “better”.