-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_modifier_full.rs
110 lines (85 loc) · 2.68 KB
/
test_modifier_full.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use jbytes::{
Bytes, BufRead,
ByteDecode
};
use jbytes_derive::{ByteDecode, ByteEncode};
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
pub struct FullExample1 {
#[jbytes(offset=2, full=0xff)]
pub a: u8,
pub b: u16,
}
#[test]
fn test_full_example1() {
let bytes = Bytes::new([0x00, 0x00, 0x01, 0x00, 0x02]);
let value = FullExample1 { a: 1, b: 2};
assert_eq!(FullExample1::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), [0xff, 0xff, 0x01, 0x00, 0x02]);
}
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
pub struct OffsetExample2 {
pub a: u8,
#[jbytes(offset="a", full=0xff)]
pub b: u16,
}
#[test]
fn test_full_example2() {
let bytes = Bytes::new([0x02, 0x00, 0x00, 0x00, 0x01]);
let value = OffsetExample2 { a: 2, b: 1};
assert_eq!(OffsetExample2::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), [0x02, 0xff, 0xff, 0x00, 0x01]);
}
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
pub struct OffsetExample3 {
pub a: u8,
#[jbytes(offset="a - 1", full=0xff)]
pub b: u16,
}
#[test]
fn test_full_example3() {
let bytes = Bytes::new([0x02, 0x00, 0x00, 0x01]);
let value = OffsetExample3 { a: 2, b: 1};
assert_eq!(OffsetExample3::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), [0x02, 0xff, 0x00, 0x01]);
}
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
pub enum OffsetExample4 {
#[jbytes(branch_value = 1)]
Jkc {
#[jbytes(offset=2, full=0xff)]
a: u8,
b: u16,
},
#[jbytes(branch_default)]
Unknown
}
#[test]
fn test_full_example4() {
let bytes = Bytes::new([0x01, 0x00, 0x00, 0x02, 0x00, 0x01]);
let value = OffsetExample4::Jkc { a: 2, b: 1};
assert_eq!(OffsetExample4::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), [0x01, 0xff, 0xff, 0x02, 0x00, 0x01]);
}
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
pub enum OffsetExample5 {
#[jbytes(branch_value = 1)]
Jkc {
a: u8,
#[jbytes(offset = "a", full=0xff)]
b: u16,
},
#[jbytes(branch_default)]
Unknown
}
#[test]
fn test_full_example5() {
let bytes = Bytes::new([0x01, 0x02, 0x00, 0x00, 0x00, 0x01]);
let value = OffsetExample5::Jkc { a: 2, b: 1};
assert_eq!(OffsetExample5::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), [0x01, 0x02, 0xff, 0xff, 0x00, 0x01]);
}