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

Skip to content

Commit eb5b668

Browse files
committed
Support bool literal as enum id
1 parent 563f5e5 commit eb5b668

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
@@ -232,6 +232,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
232232
Id::TokenStream(v) => (false, quote! {&#v}.into_token_stream()),
233233
Id::LitByteStr(v) => (false, v.into_token_stream()),
234234
Id::Int(v) => (false, v.into_token_stream()),
235+
Id::Bool(v) => (false, v.into_token_stream()),
235236
}
236237
} else if let Some(variant_id_pat) = &variant.id_pat {
237238
// If user has supplied an id, then we have an id_pat that and the id variant doesn't
@@ -284,6 +285,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
284285
let deref = match variant_id {
285286
Id::TokenStream(_) => quote! {},
286287
Id::Int(_) => quote! {},
288+
Id::Bool(_) => quote! {},
287289
Id::LitByteStr(_) => quote! {*},
288290
};
289291

deku-derive/src/macros/deku_write.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,12 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
231231
__deku_variant_id.to_writer(__deku_writer, (#id_args))?;
232232
}
233233
}
234+
Id::Bool(v) => {
235+
quote! {
236+
let mut __deku_variant_id: #id_type = #v;
237+
__deku_variant_id.to_writer(__deku_writer, (#id_args))?;
238+
}
239+
}
234240
Id::LitByteStr(v) => {
235241
quote! {
236242
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
@@ -155,3 +155,47 @@ fn test_id_pat_with_id() {
155155
);
156156
assert_eq!(input, &*v.to_bytes().unwrap());
157157
}
158+
159+
#[test]
160+
fn test_litbool_as_id() {
161+
use deku::prelude::*;
162+
163+
#[derive(DekuRead, DekuWrite, Debug, PartialEq, Eq)]
164+
pub struct A {
165+
#[deku(bits = 1)]
166+
bit: bool,
167+
#[deku(ctx = "*bit")]
168+
var: Var,
169+
}
170+
171+
#[derive(DekuRead, DekuWrite, Debug, PartialEq, Eq)]
172+
#[deku(id = "bit", ctx = "bit: bool")]
173+
pub enum Var {
174+
#[deku(id = false)]
175+
False(#[deku(bits = 15)] u16),
176+
#[deku(id = true)]
177+
True(#[deku(bits = 15)] u16),
178+
}
179+
let input = [0b1000_0000, 0xff];
180+
let mut cursor = Cursor::new(input);
181+
let (_, v) = A::from_reader((&mut cursor, 0)).unwrap();
182+
assert_eq!(
183+
v,
184+
A {
185+
bit: true,
186+
var: Var::True(0x7f01),
187+
}
188+
);
189+
assert_eq!(input, &*v.to_bytes().unwrap());
190+
let input = [0b0000_0000, 0xff];
191+
let mut cursor = Cursor::new(input);
192+
let (_, v) = A::from_reader((&mut cursor, 0)).unwrap();
193+
assert_eq!(
194+
v,
195+
A {
196+
bit: false,
197+
var: Var::False(0x7f01),
198+
}
199+
);
200+
assert_eq!(input, &*v.to_bytes().unwrap());
201+
}

0 commit comments

Comments
 (0)