Skip to content

Protocol with multiple Headers #455

Answered by wcampbell0x2a
knolljo asked this question in Q&A
Discussion options

You must be logged in to vote

Yep! ctx is the way I would do it.

use deku::prelude::*;

#[derive(Debug, DekuRead, DekuWrite)]
#[deku(endian = "big")]
struct Packet {
    id: u16,
    data_len: u16,
    #[deku(ctx = "*id")]
    header: Header,
    #[deku(count = "data_len")]
    data: Vec<u8>,
}

#[derive(DekuRead, DekuWrite, Debug)]
#[deku(
    ctx = "endian: deku::ctx::Endian, data_len: u16",
    id = "data_len",
    endian = "endian"
)]
enum Header {
    #[deku(id = 0x11)]
    Standard { id: u32 },
    #[deku(id = 0x22)]
    StandardIndexed { id: u32, index: u16 },
    #[deku(id = 0x33)]
    Addressed { id: u32, src: u64, dst: u64 },
    #[deku(id = 0x44)]
    AddressedIndexed {
        id: u32,
        src: u64,

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@knolljo
Comment options

Answer selected by knolljo
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