-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathethernet_example.rs
34 lines (26 loc) · 931 Bytes
/
ethernet_example.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
use std::str::FromStr;
use jppe::{ByteDecode, ByteEncode};
use jppe_derive::{ByteEncode, ByteDecode};
use jppe::prelude::MacAddress;
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
pub struct Ethernet {
pub smac: MacAddress,
pub dmac: MacAddress,
pub r#type: u16,
}
fn main() {
let input = b"\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x08\x00\x45\x00";
// decode
let (input_remain, value) = Ethernet::decode(input, None, None).unwrap();
println!("{value:?} {input_remain:?}");
assert_eq!(value, Ethernet {
smac: MacAddress::from_str("ff:ff:ff:ff:ff:ff").unwrap(),
dmac: MacAddress::from_str("00:00:00:00:00:00").unwrap(),
r#type: 0x0800,
});
// encode
let mut buf = vec![];
value.encode(&mut buf, None, None);
assert_eq!(buf, b"\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x08\x00");
assert_eq!(input_remain, b"\x45\x00");
}