-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_modifier_length.rs
97 lines (82 loc) · 2.33 KB
/
test_modifier_length.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
use jbytes::{
Bytes, BufRead,
BorrowByteDecode
};
use jbytes_derive::{BorrowByteDecode, BorrowByteEncode};
#[derive(Debug, PartialEq, Eq, BorrowByteDecode, BorrowByteEncode)]
pub struct LengthExample1<'a> {
#[jbytes(length=3)]
pub a: u32,
#[jbytes(length=1)]
pub b: &'a [u8],
#[jbytes(count=1, length=3)] // count indicates the Vec size and length indicates bytes of the int type.
pub c: Vec<u32>,
#[jbytes(length=3)]
pub d: &'a str,
#[jbytes(length=4)]
pub e: String,
}
#[test]
fn test_length_example1() {
let data = [
0x00, 0x00, 0x01,
0xff,
0x00, 0x00, 0x02,
0x61, 0x62, 0x63,
0x61, 0x62, 0x63, 0x64
];
let bytes = Bytes::new(data);
let value = LengthExample1 { a: 0x000001, b: &[0xff], c: vec![0x000002], d: "abc", e: "abcd".to_string()};
assert_eq!(LengthExample1::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode_borrow(value).unwrap(), data);
}
#[derive(Debug, PartialEq, Eq, BorrowByteDecode, BorrowByteEncode)]
pub struct LengthExample2<'a> {
pub a: u8,
#[jbytes(length="a")]
pub b: &'a str,
#[jbytes(length="a - 1")]
pub c: &'a str,
}
#[test]
fn test_length_example2() {
let data = [
0x03,
0x61, 0x62, 0x63,
0x61, 0x62,
];
let bytes = Bytes::new(data);
let value = LengthExample2 { a: 0x03, b: "abc", c: "ab"};
assert_eq!(LengthExample2::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode_borrow(value).unwrap(), data);
}
#[derive(Debug, Default, PartialEq, Eq, BorrowByteDecode, BorrowByteEncode)]
pub enum LengthExample3<'a> {
#[jbytes(branch_value = 1)]
Jkc {
a: u8,
#[jbytes(length="a")]
b: &'a str,
#[jbytes(length="a - 1")]
c: &'a str,
},
#[jbytes(branch_default)]
#[default]
Unknown
}
#[test]
fn test_length_example3() {
let data = [
0x01, // enum branch
0x03,
0x61, 0x62, 0x63,
0x61, 0x62,
];
let bytes = Bytes::new(data);
let value = LengthExample3::Jkc { a: 0x03, b: "abc", c: "ab"};
assert_eq!(LengthExample3::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode_borrow(value).unwrap(), data);
}