-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_modifier_bytecount_outside.rs
60 lines (50 loc) · 2.08 KB
/
test_modifier_bytecount_outside.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
use jbytes_derive::{ByteDecode, ByteEncode};
use jbytes::prelude::*;
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
pub struct ByteCountExample {
#[jbytes(byte_count_outside=2, byte_count=2)] // Fetches 2 byte mapping length in advance.
pub value: Vec<String>,
}
#[test]
fn test_modifier_byte_count_example() {
let bytes = Bytes::new([
0x00, 0x03, // byte_count_outside, vec_len=3
0x00, 0x02, // byte_count, string_len=2,
0x31, 0x32, // "12"
0x00, 0x03, // byte_count, string_len=3,
0x33, 0x34, 0x35, // "345"
0x00, 0x04, // byte_count, string_len=4,
0x36, 0x37, 0x38, 0x39, // "6789"
]);
let value = ByteCountExample::decode(&bytes).unwrap();
assert_eq!(value, ByteCountExample { value: vec!["12".to_string(), "345".to_string(), "6789".to_string()] });
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), *bytes);
}
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
pub enum ByteCountEnumExample {
#[jbytes(branch_value=1)]
Read {
#[jbytes(byte_count_outside=2, byte_count=2)] // Fetches 2 byte mapping length in advance.
value: Vec<String>,
},
#[jbytes(branch_default)]
Unknown,
}
#[test]
fn test_modifier_byte_count_enum_example() {
let bytes = Bytes::new([
0x01, // branch_value
0x00, 0x03, // byte_count_outside, vec_len=3
0x00, 0x02, // byte_count, string_len=2,
0x31, 0x32, // "12"
0x00, 0x03, // byte_count, string_len=3,
0x33, 0x34, 0x35, // "345"
0x00, 0x04, // byte_count, string_len=4,
0x36, 0x37, 0x38, 0x39, // "6789"
]);
let value = ByteCountEnumExample::decode(&bytes).unwrap();
assert_eq!(value, ByteCountEnumExample::Read { value: vec!["12".to_string(), "345".to_string(), "6789".to_string()] });
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), *bytes);
}