-
Notifications
You must be signed in to change notification settings - Fork 8
/
packet_decl_64bit_enum_little_endian.rs
163 lines (163 loc) · 4.54 KB
/
packet_decl_64bit_enum_little_endian.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#![rustfmt::skip]
/// @generated rust packets from test.
use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::convert::{TryFrom, TryInto};
use std::cell::Cell;
use std::fmt;
use std::result::Result;
use pdl_runtime::{DecodeError, EncodeError, Packet};
/// Private prevents users from creating arbitrary scalar values
/// in situations where the value needs to be validated.
/// Users can freely deref the value, but only the backend
/// may create it.
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Private<T>(T);
impl<T> std::ops::Deref for Private<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: std::fmt::Debug> std::fmt::Debug for Private<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
T::fmt(&self.0, f)
}
}
#[repr(u64)]
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(try_from = "u64", into = "u64"))]
pub enum Foo {
A = 0x1,
B = 0x2,
}
impl TryFrom<u64> for Foo {
type Error = u64;
fn try_from(value: u64) -> Result<Self, Self::Error> {
match value {
0x1 => Ok(Foo::A),
0x2 => Ok(Foo::B),
_ => Err(value),
}
}
}
impl From<&Foo> for u64 {
fn from(value: &Foo) -> Self {
match value {
Foo::A => 0x1,
Foo::B => 0x2,
}
}
}
impl From<Foo> for u64 {
fn from(value: Foo) -> Self {
(&value).into()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BarData {
x: Foo,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Bar {
#[cfg_attr(feature = "serde", serde(flatten))]
bar: BarData,
}
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BarBuilder {
pub x: Foo,
}
impl BarData {
fn conforms(bytes: &[u8]) -> bool {
bytes.len() >= 8
}
fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
let mut cell = Cell::new(bytes);
let packet = Self::parse_inner(&mut cell)?;
Ok(packet)
}
fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
if bytes.get().remaining() < 8 {
return Err(DecodeError::InvalidLengthError {
obj: "Bar",
wanted: 8,
got: bytes.get().remaining(),
});
}
let x = Foo::try_from(bytes.get_mut().get_u64_le())
.map_err(|unknown_val| DecodeError::InvalidEnumValueError {
obj: "Bar",
field: "x",
value: unknown_val as u64,
type_: "Foo",
})?;
Ok(Self { x })
}
fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> {
buffer.put_u64_le(u64::from(self.x));
Ok(())
}
fn get_total_size(&self) -> usize {
self.get_size()
}
fn get_size(&self) -> usize {
8
}
}
impl Packet for Bar {
fn encoded_len(&self) -> usize {
self.get_size()
}
fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> {
self.bar.write_to(buf)
}
}
impl TryFrom<Bar> for Bytes {
type Error = EncodeError;
fn try_from(packet: Bar) -> Result<Self, Self::Error> {
packet.encode_to_bytes()
}
}
impl TryFrom<Bar> for Vec<u8> {
type Error = EncodeError;
fn try_from(packet: Bar) -> Result<Self, Self::Error> {
packet.encode_to_vec()
}
}
impl Bar {
pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
let mut cell = Cell::new(bytes);
let packet = Self::parse_inner(&mut cell)?;
Ok(packet)
}
fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
let data = BarData::parse_inner(&mut bytes)?;
Self::new(data)
}
fn new(bar: BarData) -> Result<Self, DecodeError> {
Ok(Self { bar })
}
pub fn get_x(&self) -> Foo {
self.bar.x
}
fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> {
self.bar.write_to(buffer)
}
pub fn get_size(&self) -> usize {
self.bar.get_size()
}
}
impl BarBuilder {
pub fn build(self) -> Bar {
let bar = BarData { x: self.x };
Bar::new(bar).unwrap()
}
}
impl From<BarBuilder> for Bar {
fn from(builder: BarBuilder) -> Bar {
builder.build().into()
}
}