• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![rustfmt::skip]
2 /// @generated rust packets from test.
3 use bytes::{Buf, BufMut, Bytes, BytesMut};
4 use std::convert::{TryFrom, TryInto};
5 use std::cell::Cell;
6 use std::fmt;
7 use std::result::Result;
8 use pdl_runtime::{DecodeError, EncodeError, Packet};
9 /// Private prevents users from creating arbitrary scalar values
10 /// in situations where the value needs to be validated.
11 /// Users can freely deref the value, but only the backend
12 /// may create it.
13 #[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
14 pub struct Private<T>(T);
15 impl<T> std::ops::Deref for Private<T> {
16     type Target = T;
deref(&self) -> &Self::Target17     fn deref(&self) -> &Self::Target {
18         &self.0
19     }
20 }
21 impl<T: std::fmt::Debug> std::fmt::Debug for Private<T> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result22     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23         T::fmt(&self.0, f)
24     }
25 }
26 #[repr(u64)]
27 #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
28 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
29 #[cfg_attr(feature = "serde", serde(try_from = "u8", into = "u8"))]
30 pub enum Enum7 {
31     A = 0x1,
32     B = 0x2,
33 }
34 impl TryFrom<u8> for Enum7 {
35     type Error = u8;
try_from(value: u8) -> Result<Self, Self::Error>36     fn try_from(value: u8) -> Result<Self, Self::Error> {
37         match value {
38             0x1 => Ok(Enum7::A),
39             0x2 => Ok(Enum7::B),
40             _ => Err(value),
41         }
42     }
43 }
44 impl From<&Enum7> for u8 {
from(value: &Enum7) -> Self45     fn from(value: &Enum7) -> Self {
46         match value {
47             Enum7::A => 0x1,
48             Enum7::B => 0x2,
49         }
50     }
51 }
52 impl From<Enum7> for u8 {
from(value: Enum7) -> Self53     fn from(value: Enum7) -> Self {
54         (&value).into()
55     }
56 }
57 impl From<Enum7> for i8 {
from(value: Enum7) -> Self58     fn from(value: Enum7) -> Self {
59         u8::from(value) as Self
60     }
61 }
62 impl From<Enum7> for i16 {
from(value: Enum7) -> Self63     fn from(value: Enum7) -> Self {
64         u8::from(value) as Self
65     }
66 }
67 impl From<Enum7> for i32 {
from(value: Enum7) -> Self68     fn from(value: Enum7) -> Self {
69         u8::from(value) as Self
70     }
71 }
72 impl From<Enum7> for i64 {
from(value: Enum7) -> Self73     fn from(value: Enum7) -> Self {
74         u8::from(value) as Self
75     }
76 }
77 impl From<Enum7> for u16 {
from(value: Enum7) -> Self78     fn from(value: Enum7) -> Self {
79         u8::from(value) as Self
80     }
81 }
82 impl From<Enum7> for u32 {
from(value: Enum7) -> Self83     fn from(value: Enum7) -> Self {
84         u8::from(value) as Self
85     }
86 }
87 impl From<Enum7> for u64 {
from(value: Enum7) -> Self88     fn from(value: Enum7) -> Self {
89         u8::from(value) as Self
90     }
91 }
92 #[repr(u64)]
93 #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
94 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
95 #[cfg_attr(feature = "serde", serde(try_from = "u16", into = "u16"))]
96 pub enum Enum9 {
97     A = 0x1,
98     B = 0x2,
99 }
100 impl TryFrom<u16> for Enum9 {
101     type Error = u16;
try_from(value: u16) -> Result<Self, Self::Error>102     fn try_from(value: u16) -> Result<Self, Self::Error> {
103         match value {
104             0x1 => Ok(Enum9::A),
105             0x2 => Ok(Enum9::B),
106             _ => Err(value),
107         }
108     }
109 }
110 impl From<&Enum9> for u16 {
from(value: &Enum9) -> Self111     fn from(value: &Enum9) -> Self {
112         match value {
113             Enum9::A => 0x1,
114             Enum9::B => 0x2,
115         }
116     }
117 }
118 impl From<Enum9> for u16 {
from(value: Enum9) -> Self119     fn from(value: Enum9) -> Self {
120         (&value).into()
121     }
122 }
123 impl From<Enum9> for i16 {
from(value: Enum9) -> Self124     fn from(value: Enum9) -> Self {
125         u16::from(value) as Self
126     }
127 }
128 impl From<Enum9> for i32 {
from(value: Enum9) -> Self129     fn from(value: Enum9) -> Self {
130         u16::from(value) as Self
131     }
132 }
133 impl From<Enum9> for i64 {
from(value: Enum9) -> Self134     fn from(value: Enum9) -> Self {
135         u16::from(value) as Self
136     }
137 }
138 impl From<Enum9> for u32 {
from(value: Enum9) -> Self139     fn from(value: Enum9) -> Self {
140         u16::from(value) as Self
141     }
142 }
143 impl From<Enum9> for u64 {
from(value: Enum9) -> Self144     fn from(value: Enum9) -> Self {
145         u16::from(value) as Self
146     }
147 }
148 #[derive(Debug, Clone, PartialEq, Eq)]
149 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
150 pub struct FooData {
151     x: Enum7,
152     y: u8,
153     z: Enum9,
154     w: u8,
155 }
156 #[derive(Debug, Clone, PartialEq, Eq)]
157 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
158 pub struct Foo {
159     #[cfg_attr(feature = "serde", serde(flatten))]
160     foo: FooData,
161 }
162 #[derive(Debug)]
163 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
164 pub struct FooBuilder {
165     pub w: u8,
166     pub x: Enum7,
167     pub y: u8,
168     pub z: Enum9,
169 }
170 impl FooData {
conforms(bytes: &[u8]) -> bool171     fn conforms(bytes: &[u8]) -> bool {
172         bytes.len() >= 3
173     }
parse(bytes: &[u8]) -> Result<Self, DecodeError>174     fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
175         let mut cell = Cell::new(bytes);
176         let packet = Self::parse_inner(&mut cell)?;
177         Ok(packet)
178     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>179     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
180         if bytes.get().remaining() < 3 {
181             return Err(DecodeError::InvalidLengthError {
182                 obj: "Foo",
183                 wanted: 3,
184                 got: bytes.get().remaining(),
185             });
186         }
187         let chunk = bytes.get_mut().get_uint_le(3) as u32;
188         let x = Enum7::try_from((chunk & 0x7f) as u8)
189             .map_err(|unknown_val| DecodeError::InvalidEnumValueError {
190                 obj: "Foo",
191                 field: "x",
192                 value: unknown_val as u64,
193                 type_: "Enum7",
194             })?;
195         let y = ((chunk >> 7) & 0x1f) as u8;
196         let z = Enum9::try_from(((chunk >> 12) & 0x1ff) as u16)
197             .map_err(|unknown_val| DecodeError::InvalidEnumValueError {
198                 obj: "Foo",
199                 field: "z",
200                 value: unknown_val as u64,
201                 type_: "Enum9",
202             })?;
203         let w = ((chunk >> 21) & 0x7) as u8;
204         Ok(Self { x, y, z, w })
205     }
write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>206     fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> {
207         if self.y > 0x1f {
208             return Err(EncodeError::InvalidScalarValue {
209                 packet: "Foo",
210                 field: "y",
211                 value: self.y as u64,
212                 maximum_value: 0x1f,
213             });
214         }
215         if self.w > 0x7 {
216             return Err(EncodeError::InvalidScalarValue {
217                 packet: "Foo",
218                 field: "w",
219                 value: self.w as u64,
220                 maximum_value: 0x7,
221             });
222         }
223         let value = (u8::from(self.x) as u32) | ((self.y as u32) << 7)
224             | ((u16::from(self.z) as u32) << 12) | ((self.w as u32) << 21);
225         buffer.put_uint_le(value as u64, 3);
226         Ok(())
227     }
get_total_size(&self) -> usize228     fn get_total_size(&self) -> usize {
229         self.get_size()
230     }
get_size(&self) -> usize231     fn get_size(&self) -> usize {
232         3
233     }
234 }
235 impl Packet for Foo {
encoded_len(&self) -> usize236     fn encoded_len(&self) -> usize {
237         self.get_size()
238     }
encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>239     fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> {
240         self.foo.write_to(buf)
241     }
decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError>242     fn decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError> {
243         unimplemented!("Rust legacy does not implement full packet trait")
244     }
245 }
246 impl TryFrom<Foo> for Bytes {
247     type Error = EncodeError;
try_from(packet: Foo) -> Result<Self, Self::Error>248     fn try_from(packet: Foo) -> Result<Self, Self::Error> {
249         packet.encode_to_bytes()
250     }
251 }
252 impl TryFrom<Foo> for Vec<u8> {
253     type Error = EncodeError;
try_from(packet: Foo) -> Result<Self, Self::Error>254     fn try_from(packet: Foo) -> Result<Self, Self::Error> {
255         packet.encode_to_vec()
256     }
257 }
258 impl Foo {
parse(bytes: &[u8]) -> Result<Self, DecodeError>259     pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
260         let mut cell = Cell::new(bytes);
261         let packet = Self::parse_inner(&mut cell)?;
262         Ok(packet)
263     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>264     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
265         let data = FooData::parse_inner(&mut bytes)?;
266         Self::new(data)
267     }
new(foo: FooData) -> Result<Self, DecodeError>268     fn new(foo: FooData) -> Result<Self, DecodeError> {
269         Ok(Self { foo })
270     }
get_w(&self) -> u8271     pub fn get_w(&self) -> u8 {
272         self.foo.w
273     }
get_x(&self) -> Enum7274     pub fn get_x(&self) -> Enum7 {
275         self.foo.x
276     }
get_y(&self) -> u8277     pub fn get_y(&self) -> u8 {
278         self.foo.y
279     }
get_z(&self) -> Enum9280     pub fn get_z(&self) -> Enum9 {
281         self.foo.z
282     }
write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError>283     fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> {
284         self.foo.write_to(buffer)
285     }
get_size(&self) -> usize286     pub fn get_size(&self) -> usize {
287         self.foo.get_size()
288     }
289 }
290 impl FooBuilder {
build(self) -> Foo291     pub fn build(self) -> Foo {
292         let foo = FooData {
293             w: self.w,
294             x: self.x,
295             y: self.y,
296             z: self.z,
297         };
298         Foo::new(foo).unwrap()
299     }
300 }
301 impl From<FooBuilder> for Foo {
from(builder: FooBuilder) -> Foo302     fn from(builder: FooBuilder) -> Foo {
303         builder.build().into()
304     }
305 }
306