• 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 Foo {
31     FooBar = 0x1,
32     Baz = 0x2,
33 }
34 impl TryFrom<u8> for Foo {
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(Foo::FooBar),
39             0x2 => Ok(Foo::Baz),
40             _ => Err(value),
41         }
42     }
43 }
44 impl From<&Foo> for u8 {
from(value: &Foo) -> Self45     fn from(value: &Foo) -> Self {
46         match value {
47             Foo::FooBar => 0x1,
48             Foo::Baz => 0x2,
49         }
50     }
51 }
52 impl From<Foo> for u8 {
from(value: Foo) -> Self53     fn from(value: Foo) -> Self {
54         (&value).into()
55     }
56 }
57 impl From<Foo> for i16 {
from(value: Foo) -> Self58     fn from(value: Foo) -> Self {
59         u8::from(value) as Self
60     }
61 }
62 impl From<Foo> for i32 {
from(value: Foo) -> Self63     fn from(value: Foo) -> Self {
64         u8::from(value) as Self
65     }
66 }
67 impl From<Foo> for i64 {
from(value: Foo) -> Self68     fn from(value: Foo) -> Self {
69         u8::from(value) as Self
70     }
71 }
72 impl From<Foo> for u16 {
from(value: Foo) -> Self73     fn from(value: Foo) -> Self {
74         u8::from(value) as Self
75     }
76 }
77 impl From<Foo> for u32 {
from(value: Foo) -> Self78     fn from(value: Foo) -> Self {
79         u8::from(value) as Self
80     }
81 }
82 impl From<Foo> for u64 {
from(value: Foo) -> Self83     fn from(value: Foo) -> Self {
84         u8::from(value) as Self
85     }
86 }
87 #[derive(Debug, Clone, PartialEq, Eq)]
88 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
89 pub struct BarData {
90     x: [Foo; 3],
91 }
92 #[derive(Debug, Clone, PartialEq, Eq)]
93 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
94 pub struct Bar {
95     #[cfg_attr(feature = "serde", serde(flatten))]
96     bar: BarData,
97 }
98 #[derive(Debug)]
99 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
100 pub struct BarBuilder {
101     pub x: [Foo; 3],
102 }
103 impl BarData {
conforms(bytes: &[u8]) -> bool104     fn conforms(bytes: &[u8]) -> bool {
105         bytes.len() >= 3
106     }
parse(bytes: &[u8]) -> Result<Self, DecodeError>107     fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
108         let mut cell = Cell::new(bytes);
109         let packet = Self::parse_inner(&mut cell)?;
110         Ok(packet)
111     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>112     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
113         if bytes.get().remaining() < 3 {
114             return Err(DecodeError::InvalidLengthError {
115                 obj: "Bar",
116                 wanted: 3,
117                 got: bytes.get().remaining(),
118             });
119         }
120         let x = (0..3)
121             .map(|_| {
122                 Foo::try_from(bytes.get_mut().get_u8())
123                     .map_err(|unknown_val| DecodeError::InvalidEnumValueError {
124                         obj: "Bar",
125                         field: "",
126                         value: unknown_val as u64,
127                         type_: "Foo",
128                     })
129             })
130             .collect::<Result<Vec<_>, DecodeError>>()?
131             .try_into()
132             .map_err(|_| DecodeError::InvalidPacketError)?;
133         Ok(Self { x })
134     }
write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>135     fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> {
136         for elem in &self.x {
137             buffer.put_u8(u8::from(elem));
138         }
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         3
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_x(&self) -> &[Foo; 3]184     pub fn get_x(&self) -> &[Foo; 3] {
185         &self.bar.x
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 { x: self.x };
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