Skip to content

Commit

Permalink
Add attribute docs
Browse files Browse the repository at this point in the history
  • Loading branch information
wcampbell0x2a committed Nov 3, 2023
1 parent 75b66ec commit cca7d89
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 5 deletions.
87 changes: 87 additions & 0 deletions src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum DekuEnum {
| Attribute | Scope | Description
|-----------|------------------|------------
| [endian](#endian) | top-level, field | Set the endianness
| [bit_order](#bit_order) | top-level, field | Set the field representing the order in which to read the bits
| [magic](#magic) | top-level | A magic value that must be present at the start of this struct/enum
| [assert](#assert) | field | Assert a condition
| [assert_eq](#assert_eq) | field | Assert equals on the field
Expand Down Expand Up @@ -141,6 +142,92 @@ assert_eq!(
let value: Vec<u8> = value.try_into().unwrap();
assert_eq!(&*data, value);
```
# bit_order
Specify the field or containers bit order. By default all bits are read in `Msb0` (Most significant bit) order.
### Top-Level Example
```rust
# use deku::prelude::*;
# use std::convert::{TryInto, TryFrom};
# #[derive(Debug, DekuRead, DekuWrite, PartialEq)]
#[deku(bit_order = "lsb")]
pub struct SquashfsV3 {
#[deku(bits = "4")]
inode_type: u32,
#[deku(bits = "12")]
mode: u32,
#[deku(bits = "8")]
uid: u32,
#[deku(bits = "8")]
guid: u32,
mtime: u32,
inode_number: u32,
}
let data: &[u8] = &[
0x31, 0x12, 0x04, 0x05, 0x06, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
];
let header = SquashfsV3::try_from(data).unwrap();
assert_eq!(
SquashfsV3 {
inode_type: 0x01,
mode: 0x123,
uid: 0x4,
guid: 0x5,
mtime: 0x6,
inode_number: 0x7
},
header,
);
```
With endian-ness:
```rust
# use deku::prelude::*;
# use std::convert::{TryInto, TryFrom};
# #[derive(Debug, DekuRead, DekuWrite, PartialEq)]
#[deku(endian = "big", bit_order = "lsb")]
pub struct BigEndian {
#[deku(bits = "13")]
offset: u16,
#[deku(bits = "3")]
t: u8,
}
let data = vec![0x40, 0x40];
let big_endian = BigEndian::try_from(data.as_ref()).unwrap();
assert_eq!(
more_first,

Check failure on line 201 in src/attributes.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find value `more_first` in this scope

Check failure on line 201 in src/attributes.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find value `more_first` in this scope
BigEndian {
offset: 0x4000,
t: 2
}
);
let bytes = big_endian.to_bytes().unwrap();
assert_eq!(bytes, data);
````
### Field Example
```rust
# use deku::prelude::*;
# use std::convert::{TryInto, TryFrom};
# #[derive(Debug, DekuRead, DekuWrite, PartialEq)]
pub struct LsbField {
#[deku(bit_order = "lsb", bits = "13")]
offset: u16,
#[deku(bit_order = "lsb", bits = "3")]
t: u8,
}
let data = vec![0x40, 0x40];
let more_first = LsbField::try_from(data.as_ref()).unwrap();
assert_eq!(more_first, LsbField { offset: 0x40, t: 2 });
let bytes = more_first.to_bytes().unwrap();
assert_eq!(bytes, data);
```
# magic
Expand Down
6 changes: 3 additions & 3 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
use core::marker::PhantomData;
use core::str::FromStr;

/// An Bit Order
/// Bit numbering
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Order {
/// Big endian
/// Most significant bit
Msb0,
/// Little endian
/// least significant bit
Lsb0,
}

Expand Down
20 changes: 18 additions & 2 deletions tests/bit_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,30 @@ pub struct MoreFirst {
fn test_bit_order_more_first() {
let data = vec![0x40, 0x40];
let more_first = MoreFirst::try_from(data.as_ref()).unwrap();
// bits: 13 3
assert_eq!(more_first, MoreFirst { offset: 0x40, t: 2 });

let bytes = more_first.to_bytes().unwrap();
assert_eq_hex!(bytes, data);
}

#[derive(Debug, DekuRead, DekuWrite, PartialEq)]
pub struct LsbField {
#[deku(bit_order = "lsb", bits = "13")]
offset: u16,
#[deku(bit_order = "lsb", bits = "3")]
t: u8,
}

#[test]
fn test_bit_order_lsb_field() {
let data = vec![0x40, 0x40];
let more_first = LsbField::try_from(data.as_ref()).unwrap();
assert_eq!(more_first, LsbField { offset: 0x40, t: 2 });

let bytes = more_first.to_bytes().unwrap();
assert_eq_hex!(bytes, data);
}

#[test]
fn test_bit_order_custom_reader_writer() {
fn reader_lsb<R: std::io::Read>(reader: &mut Reader<R>) -> Result<(u16, u8), DekuError> {
Expand Down Expand Up @@ -309,7 +326,6 @@ pub struct MoreFirstBe {

#[test]
fn test_bit_order_more_first_be() {
env_logger::init();
let data = vec![0x40, 0x40];
let more_first = MoreFirstBe::try_from(data.as_ref()).unwrap();
assert_eq!(
Expand Down

0 comments on commit cca7d89

Please sign in to comment.