use bitcraft::{bitenum, bytestruct};
bitenum! {
pub enum Priority(2) {
LOW = 0,
MEDIUM = 1,
HIGH = 2,
URGENT = 3,
}
}
bytestruct! {
pub struct CustomHeader(16) {
pub version: u8 = 4, pub priority: Priority = 2, pub is_encrypted: bool = 1, pub has_checksum: bool = 1, pub sequence_id: u32 = 24, pub payload_len: u32 = 32, pub sender_id: u64 = 64, }
}
#[test]
fn test_protocol_header_example() {
let header = CustomHeader::default()
.with_version(1)
.with_priority(Priority::HIGH)
.with_is_encrypted(true)
.with_sequence_id(12345)
.with_payload_len(1024)
.with_sender_id(0xDEADBEEF_CAFEBABE);
assert_eq!(header.version(), 1);
assert_eq!(header.priority(), Priority::HIGH);
assert!(header.is_encrypted());
assert_eq!(header.sequence_id(), 12345);
assert_eq!(header.payload_len(), 1024);
assert_eq!(header.sender_id(), 0xDEADBEEF_CAFEBABE);
let raw_bytes: [u8; 16] = header.to_array();
let parsed = CustomHeader::from_array(raw_bytes);
assert_eq!(parsed, header);
}