Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c22ec31

Browse files
committed
Support bool literal as enum id
1 parent ccc76da commit c22ec31

4 files changed

Lines changed: 55 additions & 0 deletions

File tree

deku-derive/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ enum Id {
2525
TokenStream(TokenStream),
2626
LitByteStr(syn::LitByteStr),
2727
Int(syn::LitInt),
28+
Bool(syn::LitBool),
2829
}
2930

3031
impl Display for Id {
@@ -39,6 +40,7 @@ impl ToTokens for Id {
3940
Id::TokenStream(v) => v.to_tokens(tokens),
4041
Id::LitByteStr(v) => v.to_tokens(tokens),
4142
Id::Int(v) => v.to_tokens(tokens),
43+
Id::Bool(v) => v.to_tokens(tokens),
4244
}
4345
}
4446
}
@@ -53,6 +55,7 @@ impl FromMeta for Id {
5355
.expect("could not parse token stream"),
5456
)),
5557
syn::Lit::Int(ref s) => Ok(Id::Int(s.clone())),
58+
syn::Lit::Bool(ref s) => Ok(Id::Bool(s.clone())),
5659
syn::Lit::ByteStr(ref s) => Ok(Id::LitByteStr(s.clone())),
5760
_ => Err(darling::Error::unexpected_lit_type(value)),
5861
})

deku-derive/src/macros/deku_read.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
187187
Id::TokenStream(v) => (false, quote! {&#v}.into_token_stream()),
188188
Id::LitByteStr(v) => (false, v.into_token_stream()),
189189
Id::Int(v) => (false, v.into_token_stream()),
190+
Id::Bool(v) => (false, v.into_token_stream()),
190191
}
191192
} else if let Some(variant_id_pat) = &variant.id_pat {
192193
// If user has supplied an id, then we have an id_pat that and the id variant doesn't
@@ -239,6 +240,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
239240
let deref = match variant_id {
240241
Id::TokenStream(_) => quote! {},
241242
Id::Int(_) => quote! {},
243+
Id::Bool(_) => quote! {},
242244
Id::LitByteStr(_) => quote! {*},
243245
};
244246

deku-derive/src/macros/deku_write.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
187187
__deku_variant_id.to_writer(__deku_writer, (#id_args))?;
188188
}
189189
}
190+
Id::Bool(v) => {
191+
quote! {
192+
let mut __deku_variant_id: #id_type = #v;
193+
__deku_variant_id.to_writer(__deku_writer, (#id_args))?;
194+
}
195+
}
190196
Id::LitByteStr(v) => {
191197
quote! {
192198
let mut __deku_variant_id: #id_type = *#v;

tests/test_enum.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,47 @@ fn test_id_pat_with_id() {
152152
);
153153
assert_eq!(input, &*v.to_bytes().unwrap());
154154
}
155+
156+
#[test]
157+
fn test_litbool_as_id() {
158+
use deku::prelude::*;
159+
160+
#[derive(DekuRead, DekuWrite, Debug, PartialEq, Eq)]
161+
pub struct A {
162+
#[deku(bits = 1)]
163+
bit: bool,
164+
#[deku(ctx = "*bit")]
165+
var: Var,
166+
}
167+
168+
#[derive(DekuRead, DekuWrite, Debug, PartialEq, Eq)]
169+
#[deku(id = "bit", ctx = "bit: bool")]
170+
pub enum Var {
171+
#[deku(id = false)]
172+
False(#[deku(bits = 15)] u16),
173+
#[deku(id = true)]
174+
True(#[deku(bits = 15)] u16),
175+
}
176+
let input = [0b1000_0000, 0xff];
177+
let mut cursor = Cursor::new(input);
178+
let (_, v) = A::from_reader((&mut cursor, 0)).unwrap();
179+
assert_eq!(
180+
v,
181+
A {
182+
bit: true,
183+
var: Var::True(0x7f01),
184+
}
185+
);
186+
assert_eq!(input, &*v.to_bytes().unwrap());
187+
let input = [0b0000_0000, 0xff];
188+
let mut cursor = Cursor::new(input);
189+
let (_, v) = A::from_reader((&mut cursor, 0)).unwrap();
190+
assert_eq!(
191+
v,
192+
A {
193+
bit: false,
194+
var: Var::False(0x7f01),
195+
}
196+
);
197+
assert_eq!(input, &*v.to_bytes().unwrap());
198+
}

0 commit comments

Comments
 (0)