-
Notifications
You must be signed in to change notification settings - Fork 8
/
packet_decl_24bit_enum_array_little_endian.rs
186 lines (186 loc) · 5.18 KB
/
packet_decl_24bit_enum_array_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#![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 = "u32", into = "u32"))]
pub enum Foo {
FooBar = 0x1,
Baz = 0x2,
}
impl TryFrom<u32> for Foo {
type Error = u32;
fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
0x1 => Ok(Foo::FooBar),
0x2 => Ok(Foo::Baz),
_ => Err(value),
}
}
}
impl From<&Foo> for u32 {
fn from(value: &Foo) -> Self {
match value {
Foo::FooBar => 0x1,
Foo::Baz => 0x2,
}
}
}
impl From<Foo> for u32 {
fn from(value: Foo) -> Self {
(&value).into()
}
}
impl From<Foo> for i32 {
fn from(value: Foo) -> Self {
u32::from(value) as Self
}
}
impl From<Foo> for i64 {
fn from(value: Foo) -> Self {
u32::from(value) as Self
}
}
impl From<Foo> for u64 {
fn from(value: Foo) -> Self {
u32::from(value) as Self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BarData {
x: [Foo; 5],
}
#[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; 5],
}
impl BarData {
fn conforms(bytes: &[u8]) -> bool {
bytes.len() >= 15
}
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() < 5 * 3 {
return Err(DecodeError::InvalidLengthError {
obj: "Bar",
wanted: 5 * 3,
got: bytes.get().remaining(),
});
}
let x = (0..5)
.map(|_| {
Foo::try_from(bytes.get_mut().get_uint_le(3) as u32)
.map_err(|unknown_val| DecodeError::InvalidEnumValueError {
obj: "Bar",
field: "",
value: unknown_val as u64,
type_: "Foo",
})
})
.collect::<Result<Vec<_>, DecodeError>>()?
.try_into()
.map_err(|_| DecodeError::InvalidPacketError)?;
Ok(Self { x })
}
fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> {
for elem in &self.x {
buffer.put_uint_le(u32::from(elem) as u64, 3);
}
Ok(())
}
fn get_total_size(&self) -> usize {
self.get_size()
}
fn get_size(&self) -> usize {
15
}
}
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; 5] {
&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()
}
}