• 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 Foo {
29     pub a: Vec<u16>,
30 }
31 impl Foo {
conforms(bytes: &[u8]) -> bool32     fn conforms(bytes: &[u8]) -> bool {
33         bytes.len() >= 5
34     }
parse(bytes: &[u8]) -> Result<Self, DecodeError>35     pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
36         let mut cell = Cell::new(bytes);
37         let packet = Self::parse_inner(&mut cell)?;
38         Ok(packet)
39     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>40     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
41         if bytes.get().remaining() < 5 {
42             return Err(DecodeError::InvalidLengthError {
43                 obj: "Foo",
44                 wanted: 5,
45                 got: bytes.get().remaining(),
46             });
47         }
48         let a_count = bytes.get_mut().get_uint(5) as usize;
49         if bytes.get().remaining() < a_count * 2usize {
50             return Err(DecodeError::InvalidLengthError {
51                 obj: "Foo",
52                 wanted: a_count * 2usize,
53                 got: bytes.get().remaining(),
54             });
55         }
56         let a = (0..a_count)
57             .map(|_| Ok::<_, DecodeError>(bytes.get_mut().get_u16()))
58             .collect::<Result<Vec<_>, DecodeError>>()?;
59         Ok(Self { a })
60     }
write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>61     fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> {
62         if self.a.len() > 0xff_ffff_ffff_usize {
63             return Err(EncodeError::CountOverflow {
64                 packet: "Foo",
65                 field: "a",
66                 count: self.a.len(),
67                 maximum_count: 0xff_ffff_ffff_usize,
68             });
69         }
70         buffer.put_uint(self.a.len() as u64, 5);
71         for elem in &self.a {
72             buffer.put_u16(*elem);
73         }
74         Ok(())
75     }
get_total_size(&self) -> usize76     fn get_total_size(&self) -> usize {
77         self.get_size()
78     }
get_size(&self) -> usize79     fn get_size(&self) -> usize {
80         5 + self.a.len() * 2
81     }
82 }
83 #[derive(Debug, Clone, PartialEq, Eq)]
84 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
85 pub struct BarData {
86     a: Vec<Foo>,
87 }
88 #[derive(Debug, Clone, PartialEq, Eq)]
89 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
90 pub struct Bar {
91     #[cfg_attr(feature = "serde", serde(flatten))]
92     bar: BarData,
93 }
94 #[derive(Debug)]
95 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
96 pub struct BarBuilder {
97     pub a: Vec<Foo>,
98 }
99 impl BarData {
conforms(bytes: &[u8]) -> bool100     fn conforms(bytes: &[u8]) -> bool {
101         bytes.len() >= 128
102     }
parse(bytes: &[u8]) -> Result<Self, DecodeError>103     fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
104         let mut cell = Cell::new(bytes);
105         let packet = Self::parse_inner(&mut cell)?;
106         Ok(packet)
107     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>108     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
109         if bytes.get().remaining() < 128usize {
110             return Err(DecodeError::InvalidLengthError {
111                 obj: "Bar",
112                 wanted: 128usize,
113                 got: bytes.get().remaining(),
114             });
115         }
116         let (head, tail) = bytes.get().split_at(128usize);
117         let mut head = &mut Cell::new(head);
118         bytes.replace(tail);
119         let mut a = Vec::new();
120         while !head.get().is_empty() {
121             a.push(Foo::parse_inner(head)?);
122         }
123         Ok(Self { a })
124     }
write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>125     fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> {
126         let array_size = self.a.iter().fold(0, |size, elem| size + elem.get_size());
127         if array_size > 128usize {
128             return Err(EncodeError::SizeOverflow {
129                 packet: "Bar",
130                 field: "a",
131                 size: array_size,
132                 maximum_size: 128usize,
133             });
134         }
135         for elem in &self.a {
136             elem.write_to(buffer)?;
137         }
138         buffer.put_bytes(0, 128usize - array_size);
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         128
146     }
147 }
148 impl Packet for Bar {
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.bar.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<Bar> for Bytes {
160     type Error = EncodeError;
try_from(packet: Bar) -> Result<Self, Self::Error>161     fn try_from(packet: Bar) -> Result<Self, Self::Error> {
162         packet.encode_to_bytes()
163     }
164 }
165 impl TryFrom<Bar> for Vec<u8> {
166     type Error = EncodeError;
try_from(packet: Bar) -> Result<Self, Self::Error>167     fn try_from(packet: Bar) -> Result<Self, Self::Error> {
168         packet.encode_to_vec()
169     }
170 }
171 impl Bar {
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 = BarData::parse_inner(&mut bytes)?;
179         Self::new(data)
180     }
new(bar: BarData) -> Result<Self, DecodeError>181     fn new(bar: BarData) -> Result<Self, DecodeError> {
182         Ok(Self { bar })
183     }
get_a(&self) -> &Vec<Foo>184     pub fn get_a(&self) -> &Vec<Foo> {
185         &self.bar.a
186     }
write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError>187     fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> {
188         self.bar.write_to(buffer)
189     }
get_size(&self) -> usize190     pub fn get_size(&self) -> usize {
191         self.bar.get_size()
192     }
193 }
194 impl BarBuilder {
build(self) -> Bar195     pub fn build(self) -> Bar {
196         let bar = BarData { a: self.a };
197         Bar::new(bar).unwrap()
198     }
199 }
200 impl From<BarBuilder> for Bar {
from(builder: BarBuilder) -> Bar201     fn from(builder: BarBuilder) -> Bar {
202         builder.build().into()
203     }
204 }
205