1 Star 13 Fork 4

JanKinCai / jppe-rs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
test_type_enum_byte_count.rs 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
use jppe_derive::{ByteEncode, ByteDecode};
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
struct EnumByteCountExample {
pub version: u8,
#[jppe(byte_count=1)] // Fetches 1 byte mapping enum in advance.
pub body: EnumByteCountBody,
}
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
#[repr(u8)]
enum EnumByteCountBody {
Read {
value: u8,
} = 1,
Write {
address: u8,
}
}
#[test]
fn test_type_enum_byte_count() {
let (input, value) = jppe::decode::<EnumByteCountExample>(b"\x01\x01\x02").unwrap();
assert_eq!(value, EnumByteCountExample { version: 1, body: EnumByteCountBody::Read { value: 2 } });
assert_eq!(input.is_empty(), true);
assert_eq!(jppe::encode(value), b"\x01\x01\x02");
}
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
struct EnumByteCountExample2 {
pub version: u8,
#[jppe(byte_count=2)] // Fetches 2 byte mapping enum in advance.
pub body: EnumByteCountBody2,
}
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
#[repr(u8)]
enum EnumByteCountBody2 {
Read {
value: u8,
} = 1,
Write {
address: u8,
}
}
#[test]
fn test_type_enum_byte_count2() {
let (input, value) = jppe::decode::<EnumByteCountExample2>(b"\x01\x00\x01\x02").unwrap();
assert_eq!(value, EnumByteCountExample2 { version: 1, body: EnumByteCountBody2::Read { value: 2 } });
assert_eq!(input.is_empty(), true);
assert_eq!(jppe::encode(value), b"\x01\x00\x01\x02");
}
Rust
1
https://gitee.com/JanKinCai/jppe-rs.git
git@gitee.com:JanKinCai/jppe-rs.git
JanKinCai
jppe-rs
jppe-rs
master

搜索帮助