Skip to content

Commit

Permalink
Remove quotation marks from attribute bytes and bits
Browse files Browse the repository at this point in the history
  • Loading branch information
wcampbell0x2a committed Dec 28, 2023
1 parent b223767 commit fb097d9
Show file tree
Hide file tree
Showing 20 changed files with 103 additions and 102 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ use deku::prelude::*;
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(endian = "big")]
struct DekuTest {
#[deku(bits = "4")]
#[deku(bits = 4)]
field_a: u8,
#[deku(bits = "4")]
#[deku(bits = 4)]
field_b: u8,
field_c: u16,
}
Expand Down
6 changes: 3 additions & 3 deletions benches/deku.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use deku::prelude::*;

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct DekuBits {
#[deku(bits = "1")]
#[deku(bits = 1)]
data_01: u8,
#[deku(bits = "2")]
#[deku(bits = 2)]
data_02: u8,
#[deku(bits = "5")]
#[deku(bits = 5)]
data_03: u8,
}

Expand Down
8 changes: 4 additions & 4 deletions deku-derive/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ fn gen_type_from_ctx_id(
}

/// Generate argument for `id`:
/// `#deku(endian = "big", bits = "1")` -> `Endian::Big, BitSize(1)`
/// `#deku(endian = "big", bytes = "1")` -> `Endian::Big, ByteSize(1)`
/// `#deku(endian = "big", bits = 1)` -> `Endian::Big, BitSize(1)`
/// `#deku(endian = "big", bytes = 1)` -> `Endian::Big, ByteSize(1)`
pub(crate) fn gen_id_args(
endian: Option<&syn::LitStr>,
bits: Option<&Num>,
Expand All @@ -258,8 +258,8 @@ pub(crate) fn gen_id_args(

/// Generate argument for fields:
///
/// `#deku(endian = "big", bits = "1", ctx = "a")` -> `Endian::Big, BitSize(1), a`
/// `#deku(endian = "big", bytes = "1", ctx = "a")` -> `Endian::Big, ByteSize(1), a`
/// `#deku(endian = "big", bits = 1, ctx = "a")` -> `Endian::Big, BitSize(1), a`
/// `#deku(endian = "big", bytes = 1, ctx = "a")` -> `Endian::Big, ByteSize(1), a`
fn gen_field_args(
endian: Option<&syn::LitStr>,
bits: Option<&Num>,
Expand Down
4 changes: 2 additions & 2 deletions ensure_no_std/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use deku::prelude::*;

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct DekuTest {
#[deku(bits = "5")]
#[deku(bits = 5)]
field_a: u8,
#[deku(bits = "3")]
#[deku(bits = 3)]
field_b: u8,
count: u8,
#[deku(count = "count")]
Expand Down
4 changes: 2 additions & 2 deletions ensure_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ use deku::prelude::*;
#[wasm_bindgen]
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
pub struct DekuTest {
#[deku(bits = "5")]
#[deku(bits = 5)]
pub field_a: u8,
#[deku(bits = "3")]
#[deku(bits = 3)]
pub field_b: u8,
pub field_c: u8,
}
Expand Down
8 changes: 4 additions & 4 deletions examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use deku::prelude::*;

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct FieldF {
#[deku(bits = "6")]
#[deku(bits = 6)]
#[deku(assert_eq = "6")]
data: u8,
}
Expand All @@ -26,13 +26,13 @@ struct FieldF {
// #[deku(endian = "little")] // By default it uses the system endianness, but can be overwritten
struct DekuTest {
field_a: u8,
#[deku(bits = "7")]
#[deku(bits = 7)]
field_b: u8,
#[deku(bits = "1")]
#[deku(bits = 1)]
field_c: u8,
#[deku(endian = "big")]
field_d: u16,
#[deku(bits = "2")]
#[deku(bits = 2)]
field_e: u8,
field_f: FieldF,
num_items: u8,
Expand Down
12 changes: 6 additions & 6 deletions examples/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ use hexlit::hex;
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(endian = "big")]
pub struct Ipv4Header {
#[deku(bits = "4")]
#[deku(bits = 4)]
pub version: u8, // Version
#[deku(bits = "4")]
#[deku(bits = 4)]
pub ihl: u8, // Internet Header Length
#[deku(bits = "6")]
#[deku(bits = 6)]
pub dscp: u8, // Differentiated Services Code Point
#[deku(bits = "2")]
#[deku(bits = 2)]
pub ecn: u8, // Explicit Congestion Notification
pub length: u16, // Total Length
pub identification: u16, // Identification
#[deku(bits = "3")]
#[deku(bits = 3)]
pub flags: u8, // Flags
#[deku(bits = "13")]
#[deku(bits = 13)]
pub offset: u16, // Fragment Offset
pub ttl: u8, // Time To Live
pub protocol: u8, // Protocol
Expand Down
10 changes: 5 additions & 5 deletions src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ assert_eq!(value.sub.b, 0x01 + 0x02)
Example:
```ignore
struct Type1 {
#[deku(endian = "big", bits = "1")]
#[deku(endian = "big", bits = 1)]
field: u8,
}
Expand All @@ -904,7 +904,7 @@ Example: Adding context
#[deku(endian = "big")]
struct Type1 {
field_a: u16,
#[deku(bits = "5", ctx = "*field_a")]
#[deku(bits = 5, ctx = "*field_a")]
field_b: SubType,
}
Expand Down Expand Up @@ -1151,10 +1151,10 @@ Example:
# use std::io::Cursor;
# use std::convert::{TryInto, TryFrom};
# #[derive(PartialEq, Debug, DekuRead, DekuWrite)]
#[deku(type = "u8", bits = "4")]
#[deku(type = "u8", bits = 4)]
enum DekuTest {
#[deku(id = 0b1001)]
VariantA( #[deku(bits = "4")] u8, u8),
VariantA( #[deku(bits = 4)] u8, u8),
}
let data: &[u8] = &[0b1001_0110, 0xFF];
Expand Down Expand Up @@ -1182,7 +1182,7 @@ Example:
# use deku::prelude::*;
# use std::convert::{TryInto, TryFrom};
# #[derive(PartialEq, Debug, DekuRead, DekuWrite)]
#[deku(type = "u32", bytes = "2")]
#[deku(type = "u32", bytes = 2)]
enum DekuTest {
#[deku(id = 0xBEEF)]
VariantA(u8),
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ use deku::prelude::*;
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(endian = "big")]
struct DekuTest {
#[deku(bits = "4")]
#[deku(bits = 4)]
field_a: u8,
#[deku(bits = "4")]
#[deku(bits = 4)]
field_b: u8,
field_c: u16,
}
Expand Down
4 changes: 2 additions & 2 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ impl<'a, R: Read> Reader<'a, R> {
/// #[derive(Debug, PartialEq, DekuRead, DekuWrite)]
/// #[deku(endian = "big")]
/// struct DekuTest {
/// #[deku(bits = "4")]
/// #[deku(bits = 4)]
/// field_a: u8,
/// #[deku(bits = "2")]
/// #[deku(bits = 2)]
/// field_b: u8,
/// }
/// // | | <= this entire byte is Read
Expand Down
6 changes: 3 additions & 3 deletions tests/test_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ enum NestedEnum {
}

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(type = "u32", bytes = "2", ctx = "_endian: Endian")]
#[deku(type = "u32", bytes = 2, ctx = "_endian: Endian")]
enum NestedEnum2 {
#[deku(id = "0x01")]
VarA(u8),
Expand All @@ -37,9 +37,9 @@ struct TestDeku {
#[deku(count = "1")]
field_e: Vec<u8>, // 1 alloc
field_f: [u8; 3],
#[deku(bits = "3")]
#[deku(bits = 3)]
field_g: u8, // 3 allocs (read_bits(Ordering::Greater))
#[deku(bits = "5")]
#[deku(bits = 5)]
field_h: u8, // 1 alloc (read_bits(Ordering::Equal))
//field_i: NestedEnum2,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_attributes/test_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn test_struct_enum_ctx_id() {
#[derive(PartialEq, Debug, DekuRead, DekuWrite)]
struct StructEnumId {
my_id: u8,
#[deku(bytes = "1")]
#[deku(bytes = 1)]
data: usize,
#[deku(ctx = "*my_id, *data")]
enum_from_id: EnumId,
Expand Down
10 changes: 5 additions & 5 deletions tests/test_compile/cases/bits_bytes_conflict.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
use deku::prelude::*;

#[derive(DekuRead)]
#[deku(type = "u8", bits = "1", bytes = "2")]
#[deku(type = "u8", bits = 1, bytes = 2)]
enum Test1 {}

#[derive(DekuRead)]
#[deku(type = "u8")]
enum Test2 {
A(#[deku(bits = "1", bytes = "2")] u8),
A(#[deku(bits = 1, bytes = 2)] u8),
B {
#[deku(bits = "3", bytes = "4")]
#[deku(bits = 3, bytes = 4)]
a: u8,
},
}

#[derive(DekuRead)]
struct Test3 {
#[deku(bits = "5", bytes = "6")]
#[deku(bits = 5, bytes = 6)]
a: u8,
}

#[derive(DekuRead)]
struct Test4(#[deku(bits = "7", bytes = "8")] u8);
struct Test4(#[deku(bits = 7, bytes = 8)] u8);

fn main() {}
16 changes: 8 additions & 8 deletions tests/test_compile/cases/bits_bytes_conflict.stderr
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
error: conflicting: both `bits` and `bytes` specified on enum
--> $DIR/bits_bytes_conflict.rs:4:28
|
4 | #[deku(type = "u8", bits = "1", bytes = "2")]
| ^^^
4 | #[deku(type = "u8", bits = 1, bytes = 2)]
| ^

error: conflicting: both `bits` and `bytes` specified on field
--> $DIR/bits_bytes_conflict.rs:10:21
|
10 | A(#[deku(bits = "1", bytes = "2")] u8),
| ^^^
10 | A(#[deku(bits = 1, bytes = 2)] u8),
| ^

error: conflicting: both `bits` and `bytes` specified on field
--> $DIR/bits_bytes_conflict.rs:19:19
|
19 | #[deku(bits = "5", bytes = "6")]
| ^^^
19 | #[deku(bits = 5, bytes = 6)]
| ^

error: conflicting: both `bits` and `bytes` specified on field
--> $DIR/bits_bytes_conflict.rs:24:28
|
24 | struct Test4(#[deku(bits = "7", bytes = "8")] u8);
| ^^^
24 | struct Test4(#[deku(bits = 7, bytes = 8)] u8);
| ^
27 changes: 14 additions & 13 deletions tests/test_compile/cases/enum_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,58 @@ enum Test2 {}
#[derive(DekuRead)]
#[deku(type = "u8")]
enum Test3 {
#[deku(id = "1", id_pat = "2..=3")] A(u8),
#[deku(id = "1", id_pat = "2..=3")]
A(u8),
}

// test `type` only allowed on enum
#[derive(DekuRead)]
#[deku(type = "u8")]
struct Test4 {
a: u8
a: u8,
}

// test `bits` only allowed on enum
#[derive(DekuRead)]
#[deku(bits = "1")]
#[deku(bits = 1)]
struct Test5 {
a: u8
a: u8,
}

// test `bytes` only allowed on enum
#[derive(DekuRead)]
#[deku(bits = "1")]
#[deku(bits = 1)]
struct Test6 {
a: u8
a: u8,
}

// test `id` only allowed on enum
#[derive(DekuRead)]
#[deku(id = "test")]
struct Test7 {
a: u8
a: u8,
}

// test `bits` cannot be used with `id`
#[derive(DekuRead)]
#[deku(id = "test", bits = "4")]
#[deku(id = "test", bits = 4)]
enum Test8 {
A
A,
}

// test `bytes` cannot be used with `id`
#[derive(DekuRead)]
#[deku(id = "test", bytes = "4")]
#[deku(id = "test", bytes = 4)]
enum Test9 {
A
A,
}

// test `id` cannot be `_`
#[derive(DekuRead)]
#[deku(type = "u8")]
enum Test10 {
#[deku(id = "_")]
A
A,
}

// test missing `id`
Expand All @@ -72,7 +73,7 @@ enum Test10 {
enum Test11 {
#[deku(id = "1")]
A,
B(u8)
B(u8),
}

fn main() {}
Loading

0 comments on commit fb097d9

Please sign in to comment.