-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_modifier_branch.rs
113 lines (94 loc) · 3.52 KB
/
test_modifier_branch.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
111
112
113
use jbytes_derive::{ByteDecode, ByteEncode};
use jbytes::prelude::*;
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
struct BranchExample {
pub cmd: u16,
#[jbytes(branch="cmd")]
pub body: BranchValueExampleBody,
}
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
enum BranchValueExampleBody {
#[jbytes(branch_value=1)]
V0, // Set 1
V1(u8), // Increment to 2
V2(u8, u16), // Increment to 3
V3((u8, u16)), // Increment to 4
V4 { // Increment to 5
a: u8,
b: u16,
},
#[jbytes(branch_default)] // match xxx { _ => xxxx }
Unknown,
}
#[test]
fn test_modifier_branch() {
// V0
let data = [
0x00, 0x01, // cmd
];
let bytes = Bytes::new(data);
let value = BranchExample { cmd: 1, body: BranchValueExampleBody::V0 };
assert_eq!(BranchExample::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), data);
// V1
let data = [
0x00, 0x02, // cmd
0x01, // V1(u8)
];
let bytes = Bytes::new(data);
let value = BranchExample { cmd: 2, body: BranchValueExampleBody::V1(1) };
assert_eq!(BranchExample::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), data);
// V2
let data = [
0x00, 0x03, // cmd
0x01, // V2(u8)
0x00, 0x02, // v2(u16)
];
let bytes = Bytes::new(data);
let value = BranchExample { cmd: 3, body: BranchValueExampleBody::V2(1, 2) };
assert_eq!(BranchExample::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), data);
// V3
let data = [
0x00, 0x04, // cmd
0x01, // V3(u8)
0x00, 0x02, // v3(u16)
];
let bytes = Bytes::new(data);
let value = BranchExample { cmd: 4, body: BranchValueExampleBody::V3((1, 2)) };
assert_eq!(BranchExample::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), data);
// V4
let data = [
0x00, 0x05, // cmd
0x01, // a
0x00, 0x02, // b
];
let bytes = Bytes::new(data);
let value = BranchExample { cmd: 5, body: BranchValueExampleBody::V4 { a: 1, b: 2 } };
assert_eq!(BranchExample::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), data);
// Unknown
let data = [
0x00, 0x06, // cmd
];
let bytes = Bytes::new(data);
let value = BranchExample { cmd: 6, body: BranchValueExampleBody::Unknown};
assert_eq!(BranchExample::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), data);
let data = [
0x00, 0x1f, // cmd
];
let bytes = Bytes::new(data);
let value = BranchExample { cmd: 0x1f, body: BranchValueExampleBody::Unknown};
assert_eq!(BranchExample::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), data);
}