-
I'm trying to implement serialisation and deserialisation for a binary protocol using struct Message {
len: u16, // of Message
commands: Vec<Command>, // Message contains 1..n Command(s)
}
struct Command {
len: u16, // of the entire Command
_padding: u16, // because C++
id: [u8; 4], // or magic
payload: Vec<u8>, // (len - 8) bytes, command-specific options
} My current (non- struct MessageAsPayloads {
payloads: Vec<Payload>,
}
enum Payload {
// Command::id = b"Alph"
Alpha(AlphaOpts),
// Command::id = b"Brav"
Bravo(BravoOpts),
// Command types we don't know become an Unknown variant
Unknown(id: [u8; 4], payload: Vec<u8>),
} I haven't implemented all of the protocol (it's a slow process of reverse engineering, and there are a lot of command types), so I'd like the parser to just ignore things that aren't implemented yet by pushing them to an "Unknown" variant. It looks like Is there a nice way handle unknown variants with |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Enum parsing is done by attempting to parse each variant in order until one of them succeeds. Only if all variants fail is it considered a failure. So just don’t put any magic on the fallback |
Beta Was this translation helpful? Give feedback.
Enum parsing is done by attempting to parse each variant in order until one of them succeeds. Only if all variants fail is it considered a failure. So just don’t put any magic on the fallback
Unknown
variant and so long as there is no error parsing the variant’s members the parent enum will be considered parsed successfully. (The error handling directives are to avoid wasting time/memory storing errors that you don’t care about.)