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 #[derive(Debug, Clone, PartialEq, Eq)]
27 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28 pub struct FooData {
29     a: u8,
30     b: u8,
31     c: u8,
32     d: u32,
33     e: u16,
34     f: u8,
35 }
36 #[derive(Debug, Clone, PartialEq, Eq)]
37 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
38 pub struct Foo {
39     #[cfg_attr(feature = "serde", serde(flatten))]
40     foo: FooData,
41 }
42 #[derive(Debug)]
43 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
44 pub struct FooBuilder {
45     pub a: u8,
46     pub b: u8,
47     pub c: u8,
48     pub d: u32,
49     pub e: u16,
50     pub f: u8,
51 }
52 impl FooData {
conforms(bytes: &[u8]) -> bool53     fn conforms(bytes: &[u8]) -> bool {
54         bytes.len() >= 7
55     }
parse(bytes: &[u8]) -> Result<Self, DecodeError>56     fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
57         let mut cell = Cell::new(bytes);
58         let packet = Self::parse_inner(&mut cell)?;
59         Ok(packet)
60     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>61     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
62         if bytes.get().remaining() < 2 {
63             return Err(DecodeError::InvalidLengthError {
64                 obj: "Foo",
65                 wanted: 2,
66                 got: bytes.get().remaining(),
67             });
68         }
69         let chunk = bytes.get_mut().get_u16();
70         let a = (chunk & 0x7) as u8;
71         let b = (chunk >> 3) as u8;
72         let c = ((chunk >> 11) & 0x1f) as u8;
73         if bytes.get().remaining() < 3 {
74             return Err(DecodeError::InvalidLengthError {
75                 obj: "Foo",
76                 wanted: 3,
77                 got: bytes.get().remaining(),
78             });
79         }
80         let d = bytes.get_mut().get_uint(3) as u32;
81         if bytes.get().remaining() < 2 {
82             return Err(DecodeError::InvalidLengthError {
83                 obj: "Foo",
84                 wanted: 2,
85                 got: bytes.get().remaining(),
86             });
87         }
88         let chunk = bytes.get_mut().get_u16();
89         let e = (chunk & 0xfff);
90         let f = ((chunk >> 12) & 0xf) as u8;
91         Ok(Self { a, b, c, d, e, f })
92     }
write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>93     fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> {
94         if self.a > 0x7 {
95             return Err(EncodeError::InvalidScalarValue {
96                 packet: "Foo",
97                 field: "a",
98                 value: self.a as u64,
99                 maximum_value: 0x7,
100             });
101         }
102         if self.c > 0x1f {
103             return Err(EncodeError::InvalidScalarValue {
104                 packet: "Foo",
105                 field: "c",
106                 value: self.c as u64,
107                 maximum_value: 0x1f,
108             });
109         }
110         let value = (self.a as u16) | ((self.b as u16) << 3) | ((self.c as u16) << 11);
111         buffer.put_u16(value);
112         if self.d > 0xff_ffff {
113             return Err(EncodeError::InvalidScalarValue {
114                 packet: "Foo",
115                 field: "d",
116                 value: self.d as u64,
117                 maximum_value: 0xff_ffff,
118             });
119         }
120         buffer.put_uint(self.d as u64, 3);
121         if self.e > 0xfff {
122             return Err(EncodeError::InvalidScalarValue {
123                 packet: "Foo",
124                 field: "e",
125                 value: self.e as u64,
126                 maximum_value: 0xfff,
127             });
128         }
129         if self.f > 0xf {
130             return Err(EncodeError::InvalidScalarValue {
131                 packet: "Foo",
132                 field: "f",
133                 value: self.f as u64,
134                 maximum_value: 0xf,
135             });
136         }
137         let value = self.e | ((self.f as u16) << 12);
138         buffer.put_u16(value);
139         Ok(())
140     }
get_total_size(&self) -> usize141     fn get_total_size(&self) -> usize {
142         self.get_size()
143     }
get_size(&self) -> usize144     fn get_size(&self) -> usize {
145         7
146     }
147 }
148 impl Packet for Foo {
encoded_len(&self) -> usize149     fn encoded_len(&self) -> usize {
150         self.get_size()
151     }
encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>152     fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> {
153         self.foo.write_to(buf)
154     }
decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError>155     fn decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError> {
156         unimplemented!("Rust legacy does not implement full packet trait")
157     }
158 }
159 impl TryFrom<Foo> for Bytes {
160     type Error = EncodeError;
try_from(packet: Foo) -> Result<Self, Self::Error>161     fn try_from(packet: Foo) -> Result<Self, Self::Error> {
162         packet.encode_to_bytes()
163     }
164 }
165 impl TryFrom<Foo> for Vec<u8> {
166     type Error = EncodeError;
try_from(packet: Foo) -> Result<Self, Self::Error>167     fn try_from(packet: Foo) -> Result<Self, Self::Error> {
168         packet.encode_to_vec()
169     }
170 }
171 impl Foo {
parse(bytes: &[u8]) -> Result<Self, DecodeError>172     pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
173         let mut cell = Cell::new(bytes);
174         let packet = Self::parse_inner(&mut cell)?;
175         Ok(packet)
176     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>177     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
178         let data = FooData::parse_inner(&mut bytes)?;
179         Self::new(data)
180     }
new(foo: FooData) -> Result<Self, DecodeError>181     fn new(foo: FooData) -> Result<Self, DecodeError> {
182         Ok(Self { foo })
183     }
get_a(&self) -> u8184     pub fn get_a(&self) -> u8 {
185         self.foo.a
186     }
get_b(&self) -> u8187     pub fn get_b(&self) -> u8 {
188         self.foo.b
189     }
get_c(&self) -> u8190     pub fn get_c(&self) -> u8 {
191         self.foo.c
192     }
get_d(&self) -> u32193     pub fn get_d(&self) -> u32 {
194         self.foo.d
195     }
get_e(&self) -> u16196     pub fn get_e(&self) -> u16 {
197         self.foo.e
198     }
get_f(&self) -> u8199     pub fn get_f(&self) -> u8 {
200         self.foo.f
201     }
write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError>202     fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> {
203         self.foo.write_to(buffer)
204     }
get_size(&self) -> usize205     pub fn get_size(&self) -> usize {
206         self.foo.get_size()
207     }
208 }
209 impl FooBuilder {
build(self) -> Foo210     pub fn build(self) -> Foo {
211         let foo = FooData {
212             a: self.a,
213             b: self.b,
214             c: self.c,
215             d: self.d,
216             e: self.e,
217             f: self.f,
218         };
219         Foo::new(foo).unwrap()
220     }
221 }
222 impl From<FooBuilder> for Foo {
from(builder: FooBuilder) -> Foo223     fn from(builder: FooBuilder) -> Foo {
224         builder.build().into()
225     }
226 }
227