• 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 #[derive(Debug, Clone, PartialEq, Eq)]
27 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28 pub struct FooData {
29     padding: u8,
30     x: Vec<u32>,
31 }
32 #[derive(Debug, Clone, PartialEq, Eq)]
33 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
34 pub struct Foo {
35     #[cfg_attr(feature = "serde", serde(flatten))]
36     foo: FooData,
37 }
38 #[derive(Debug)]
39 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
40 pub struct FooBuilder {
41     pub padding: u8,
42     pub x: Vec<u32>,
43 }
44 impl FooData {
conforms(bytes: &[u8]) -> bool45     fn conforms(bytes: &[u8]) -> bool {
46         bytes.len() >= 1
47     }
parse(bytes: &[u8]) -> Result<Self, DecodeError>48     fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
49         let mut cell = Cell::new(bytes);
50         let packet = Self::parse_inner(&mut cell)?;
51         Ok(packet)
52     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>53     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
54         if bytes.get().remaining() < 1 {
55             return Err(DecodeError::InvalidLengthError {
56                 obj: "Foo",
57                 wanted: 1,
58                 got: bytes.get().remaining(),
59             });
60         }
61         let chunk = bytes.get_mut().get_u8();
62         let x_count = (chunk & 0x1f) as usize;
63         let padding = ((chunk >> 5) & 0x7);
64         if bytes.get().remaining() < x_count * 3usize {
65             return Err(DecodeError::InvalidLengthError {
66                 obj: "Foo",
67                 wanted: x_count * 3usize,
68                 got: bytes.get().remaining(),
69             });
70         }
71         let x = (0..x_count)
72             .map(|_| Ok::<_, DecodeError>(bytes.get_mut().get_uint(3) as u32))
73             .collect::<Result<Vec<_>, DecodeError>>()?;
74         Ok(Self { padding, x })
75     }
write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>76     fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> {
77         if self.x.len() > 0x1f {
78             return Err(EncodeError::CountOverflow {
79                 packet: "Foo",
80                 field: "x",
81                 count: self.x.len(),
82                 maximum_count: 0x1f,
83             });
84         }
85         if self.padding > 0x7 {
86             return Err(EncodeError::InvalidScalarValue {
87                 packet: "Foo",
88                 field: "padding",
89                 value: self.padding as u64,
90                 maximum_value: 0x7,
91             });
92         }
93         let value = self.x.len() as u8 | (self.padding << 5);
94         buffer.put_u8(value);
95         for elem in &self.x {
96             buffer.put_uint(*elem as u64, 3);
97         }
98         Ok(())
99     }
get_total_size(&self) -> usize100     fn get_total_size(&self) -> usize {
101         self.get_size()
102     }
get_size(&self) -> usize103     fn get_size(&self) -> usize {
104         1 + self.x.len() * 3
105     }
106 }
107 impl Packet for Foo {
encoded_len(&self) -> usize108     fn encoded_len(&self) -> usize {
109         self.get_size()
110     }
encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>111     fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> {
112         self.foo.write_to(buf)
113     }
decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError>114     fn decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError> {
115         unimplemented!("Rust legacy does not implement full packet trait")
116     }
117 }
118 impl TryFrom<Foo> for Bytes {
119     type Error = EncodeError;
try_from(packet: Foo) -> Result<Self, Self::Error>120     fn try_from(packet: Foo) -> Result<Self, Self::Error> {
121         packet.encode_to_bytes()
122     }
123 }
124 impl TryFrom<Foo> for Vec<u8> {
125     type Error = EncodeError;
try_from(packet: Foo) -> Result<Self, Self::Error>126     fn try_from(packet: Foo) -> Result<Self, Self::Error> {
127         packet.encode_to_vec()
128     }
129 }
130 impl Foo {
parse(bytes: &[u8]) -> Result<Self, DecodeError>131     pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
132         let mut cell = Cell::new(bytes);
133         let packet = Self::parse_inner(&mut cell)?;
134         Ok(packet)
135     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>136     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
137         let data = FooData::parse_inner(&mut bytes)?;
138         Self::new(data)
139     }
new(foo: FooData) -> Result<Self, DecodeError>140     fn new(foo: FooData) -> Result<Self, DecodeError> {
141         Ok(Self { foo })
142     }
get_padding(&self) -> u8143     pub fn get_padding(&self) -> u8 {
144         self.foo.padding
145     }
get_x(&self) -> &Vec<u32>146     pub fn get_x(&self) -> &Vec<u32> {
147         &self.foo.x
148     }
write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError>149     fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> {
150         self.foo.write_to(buffer)
151     }
get_size(&self) -> usize152     pub fn get_size(&self) -> usize {
153         self.foo.get_size()
154     }
155 }
156 impl FooBuilder {
build(self) -> Foo157     pub fn build(self) -> Foo {
158         let foo = FooData {
159             padding: self.padding,
160             x: self.x,
161         };
162         Foo::new(foo).unwrap()
163     }
164 }
165 impl From<FooBuilder> for Foo {
from(builder: FooBuilder) -> Foo166     fn from(builder: FooBuilder) -> Foo {
167         builder.build().into()
168     }
169 }
170