Skip to content

Commit

Permalink
Support bool literal as enum id (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcampbell0x2a authored Sep 5, 2024
1 parent efbe7b4 commit d0ca6a3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions deku-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum Id {
TokenStream(TokenStream),
LitByteStr(syn::LitByteStr),
Int(syn::LitInt),
Bool(syn::LitBool),
}

impl Display for Id {
Expand All @@ -39,6 +40,7 @@ impl ToTokens for Id {
Id::TokenStream(v) => v.to_tokens(tokens),
Id::LitByteStr(v) => v.to_tokens(tokens),
Id::Int(v) => v.to_tokens(tokens),
Id::Bool(v) => v.to_tokens(tokens),
}
}
}
Expand All @@ -53,6 +55,7 @@ impl FromMeta for Id {
.expect("could not parse token stream"),
)),
syn::Lit::Int(ref s) => Ok(Id::Int(s.clone())),
syn::Lit::Bool(ref s) => Ok(Id::Bool(s.clone())),
syn::Lit::ByteStr(ref s) => Ok(Id::LitByteStr(s.clone())),
_ => Err(darling::Error::unexpected_lit_type(value)),
})
Expand Down
2 changes: 2 additions & 0 deletions deku-derive/src/macros/deku_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
Id::TokenStream(v) => quote! {&#v}.into_token_stream(),
Id::LitByteStr(v) => v.into_token_stream(),
Id::Int(v) => v.into_token_stream(),
Id::Bool(v) => v.into_token_stream(),
}
} else if let Some(variant_id_pat) = &variant.id_pat {
// If user has supplied an id, then we have an id_pat that and the id variant doesn't
Expand Down Expand Up @@ -285,6 +286,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
let deref = match variant_id {
Id::TokenStream(_) => quote! {},
Id::Int(_) => quote! {},
Id::Bool(_) => quote! {},
Id::LitByteStr(_) => quote! {*},
};

Expand Down
6 changes: 6 additions & 0 deletions deku-derive/src/macros/deku_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
__deku_variant_id.to_writer(__deku_writer, (#id_args))?;
}
}
Id::Bool(v) => {
quote! {
let mut __deku_variant_id: #id_type = #v;
__deku_variant_id.to_writer(__deku_writer, (#id_args))?;
}
}
Id::LitByteStr(v) => {
quote! {
let mut __deku_variant_id: #id_type = *#v;
Expand Down
44 changes: 44 additions & 0 deletions tests/test_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,47 @@ fn id_pat_with_id_bits() {
assert_eq!(v, IdPatBitsTuple::B((0, 1)));
assert_eq!(input, &*v.to_bytes().unwrap());
}

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

#[derive(DekuRead, DekuWrite, Debug, PartialEq, Eq)]
pub struct A {
#[deku(bits = 1)]
bit: bool,
#[deku(ctx = "*bit")]
var: Var,
}

#[derive(DekuRead, DekuWrite, Debug, PartialEq, Eq)]
#[deku(id = "bit", ctx = "bit: bool")]
pub enum Var {
#[deku(id = false)]
False(#[deku(bits = 15)] u16),
#[deku(id = true)]
True(#[deku(bits = 15)] u16),
}
let input = [0b1000_0000, 0xff];
let mut cursor = Cursor::new(input);
let (_, v) = A::from_reader((&mut cursor, 0)).unwrap();
assert_eq!(
v,
A {
bit: true,
var: Var::True(0x7f01),
}
);
assert_eq!(input, &*v.to_bytes().unwrap());
let input = [0b0000_0000, 0xff];
let mut cursor = Cursor::new(input);
let (_, v) = A::from_reader((&mut cursor, 0)).unwrap();
assert_eq!(
v,
A {
bit: false,
var: Var::False(0x7f01),
}
);
assert_eq!(input, &*v.to_bytes().unwrap());
}

0 comments on commit d0ca6a3

Please sign in to comment.