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

Fix unknown id Type for EnumExt::deku_id() #398

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions deku-derive/src/macros/deku_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use darling::ast::{Data, Fields};
use darling::ToTokens;
use proc_macro2::TokenStream;
use quote::quote;
use syn::spanned::Spanned;

use crate::macros::{
gen_ctx_types_and_arg, gen_field_args, gen_internal_field_ident, gen_internal_field_idents,
Expand Down Expand Up @@ -397,12 +396,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
let deku_id_type = if let Some(id_type) = id_type {
Some(quote! {#id_type})
} else if let (Some(ctx), Some(id)) = (input.ctx.as_ref(), input.id.as_ref()) {
Some(gen_type_from_ctx_id(ctx, id).ok_or_else(|| {
syn::Error::new(
id.span(),
"DekuReader: cannot determine `id` type from `ctx`",
)
})?)
gen_type_from_ctx_id(ctx, id)
} else {
None
};
Expand Down
4 changes: 3 additions & 1 deletion deku-derive/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ fn gen_type_from_ctx_id(
id: &crate::Id,
) -> Option<TokenStream> {
let parser = Punctuated::<Ident, Comma>::parse_terminated;
let s = parser.parse(id.to_token_stream().into()).unwrap();
let Ok(s) = parser.parse(id.to_token_stream().into()) else {
return None;
};
let mut matching_types = quote! {};
for s in s {
let id = syn::Ident::new(&s.to_string(), id.span());
Expand Down
32 changes: 32 additions & 0 deletions tests/test_regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,35 @@ fn issue_310() {
#[derive(DekuRead, DekuWrite)]
struct Test {}
}

#[test]
fn issue_397() {
use deku::prelude::*;

#[derive(Debug, Copy, Clone, PartialEq, DekuRead, DekuWrite)]
struct Header {
kind: PacketType,
}

#[derive(Debug, Copy, Clone, PartialEq, DekuRead, DekuWrite)]
#[deku(type = "u8", endian = "big")]
enum PacketType {
#[deku(id = 0)]
Zero,
}

#[derive(Debug, Copy, Clone, PartialEq, DekuRead, DekuWrite)]
struct Packet {
header: Header,
#[deku(ctx = "header")]
payload: Payload,
}

#[derive(Debug, Copy, Clone, PartialEq, DekuRead, DekuWrite)]
#[deku(ctx = "header: &Header", id = "header.kind")]
enum Payload {
#[deku(id = "PacketType::Zero")]
Zero(u8),
}
let _ = Packet::from_bytes((&[0x00, 0x01], 0));
}
Loading