Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional Skip #63

Closed
wcampbell0x2a opened this issue Jul 25, 2020 · 4 comments · Fixed by #65
Closed

Conditional Skip #63

wcampbell0x2a opened this issue Jul 25, 2020 · 4 comments · Fixed by #65
Labels
enhancement New feature or request

Comments

@wcampbell0x2a
Copy link
Collaborator

Great idea of a library.

I have a protocol that needs conditional parsing of fields in a struct. I see the skip attribute, but would something like a conditional skip be possible?

use deku::prelude::*;
use std::convert::TryFrom;

#[derive(PartialEq, Debug, DekuRead, DekuWrite)]
pub struct DekuTest {
    pub field_a: u8,
    #[deku(skip, if=(field_a, 1))]
    pub field_b: Option<u8>,
    #[deku(skip, if=(field_b, Some(1)))]
    pub field_c: Option<u8>,
}

fn main() {
    let data: Vec<u8> = vec![0x01, 0x02];

    let value = DekuTest::from_bytes((data.as_ref(), 0)).unwrap();
    println!("{:#?}", value)
}
@sharksforarms
Copy link
Owner

sharksforarms commented Jul 25, 2020

I like the idea! Shouldn't be too hard to implement. I can get to it next week most likely, also reminds me of a cond attribute for a conditional parsing, which may cover your use case

@sharksforarms sharksforarms added the enhancement New feature or request label Jul 25, 2020
sharksforarms added a commit that referenced this issue Jul 27, 2020
- Make it work with `skip` for a "skip if" construct
- Make it work with default
sharksforarms added a commit that referenced this issue Jul 28, 2020
- Make it work with `skip` for a "skip if" construct
- Make it work with default
sharksforarms added a commit that referenced this issue Jul 28, 2020
- Make it work with `skip` for a "skip if" construct
- Make it work with default
@sharksforarms
Copy link
Owner

sharksforarms commented Jul 28, 2020

If you don't mind taking a look to see if it meets your use-case, I plan on making a release tomorrow morning.

In the case of your original ticket:

use deku::prelude::*;
use std::convert::TryFrom;

#[derive(PartialEq, Debug, DekuRead, DekuWrite)]
pub struct DekuTest {
    pub field_a: u8,
    #[deku(skip, cond="*field_a == 1")] // skip if field_a == 1
    pub field_b: Option<u8>,
    #[deku(skip, cond="*field_b == Some(1)")] // skip if field_b == Some(1)
    pub field_c: Option<u8>,
}

fn main() {
    let data: Vec<u8> = vec![0x01, 0x02];

    let value = DekuTest::from_bytes((data.as_ref(), 0)).unwrap();
    println!("{:#?}", value)
}
DekuTest {
        field_a: 1,
        field_b: None,
        field_c: Some(
            2,
        ),
    },

@wcampbell0x2a
Copy link
Collaborator Author

I shall

sharksforarms added a commit that referenced this issue Jul 28, 2020
- Make it work with `skip` for a "skip if" construct
- Make it work with default
@wcampbell0x2a
Copy link
Collaborator Author

Perfect, works as expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants