Help passing context around #396
-
I'm brand new to the Deku crate. Its really cool! I'm looking to parse some binary packet data with this crate. The general structure of packets is a Header/Payload structure. The header is an Identifier and a total packet length (as u32). My initial thought was to treat the payload section as an enum where the id is an external id passed from the header type through the context variable (referencing the docs) Below is a minimal implementation for one variant but I'm getting a proc-macro error that I don't understand. use deku::prelude::*;
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct Packet {
header: Header,
#[deku(ctx = "*header")]
payload: Payload,
}
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct Header {
kind: PacketType,
length: u32,
}
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(type = "[u8;4]", endian="big")]
enum PacketType {
#[deku(id = b"INFO")]
InfoType,
}
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(ctx = "header: Header", id = "header.kind")]
enum Payload {
#[deku(id = "PacketType::InfoType")]
Info {
#[deku(count = "header.length-8")]
data: Vec<u8>
},
}
fn main() {
// let data: Vec<u8> = vec![0x54, 0x49, 0x4D, 0x45,0x00,0x00,0x00,0x0C, 0x00,0x00,0x00,0x10, 0x53, 0x46, 0x4F, 0x52, 0x00, 0x00, 0x00, 0xA0];
let data : Vec<u8> = vec![0x49, 0x4E, 0x46, 0x4F, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x10];
let mut rest = (data.as_ref(),0 as usize);
let (_rest,val) = Packet::from_bytes(rest).unwrap();
dbg!(val);
dbg!(rest);
} These are the errors: error: proc-macro derive panicked
--> src\main.rs:26:28
|
26 | #[derive(Debug, PartialEq, DekuRead, DekuWrite)]
| ^^^^^^^^
|
= help: message: called `Result::unwrap()` on an `Err` value: Error("expected `,`")
error[E0277]: the trait bound `Payload: deku::DekuRead<'_, _>` is not satisfied
--> src\main.rs:7:14
|
7 | payload: Payload,
| ^^^^^^^ the trait `deku::DekuRead<'_, _>` is not implemented for `Payload`
|
= help: the following other types implement trait `deku::DekuRead<'a, Ctx>`:
<bool as deku::DekuRead<'a, Ctx>>
<isize as deku::DekuRead<'_, (Endian, deku::ctx::ByteSize)>>
<isize as deku::DekuRead<'_, (Endian, deku::ctx::BitSize)>>
<isize as deku::DekuRead<'_, Endian>>
<isize as deku::DekuRead<'_, deku::ctx::ByteSize>>
<isize as deku::DekuRead<'_, deku::ctx::BitSize>>
<isize as deku::DekuRead<'_>>
<i8 as deku::DekuRead<'_, (Endian, deku::ctx::ByteSize)>>
and 155 others
For more information about this error, try `rustc --explain E0277`.
error: could not compile `test-rs` (bin "test-rs") due to 3 previous errors I'd appreciate any pointers to what I may be doing wrong here. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey! This is actually a bug with deku, see #397 and #398 Lmk if that MR helps you. |
Beta Was this translation helpful? Give feedback.
This should have been fixed with #398.