Skip to content

Conditionally parse past the end of the file #313

Answered by sharksforarms
lfrancke asked this question in Q&A
Discussion options

You must be logged in to vote

The cond attribute is what you're looking for. You can check deku::rest which provides the remaining data to be read, here's an example:

use deku::prelude::*;
use std::convert::{TryFrom, TryInto};

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct DekuTest {
    field_a: u8,
    #[deku(cond = "!deku::rest.is_empty()")]
    field_b: Option<u8>,
}

fn main() {
    {
        let test_data: &[u8] = [
            0xAB,
        ]
            .as_ref();

        let test_deku = DekuTest::try_from(test_data).unwrap();

        assert_eq!(
            DekuTest {
                field_a: 0xAB,
                field_b: None,
            },
            test_deku
            );

        let test…

Replies: 1 comment 1 reply

Comment options

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

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