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     A = 0x1,
32     B = 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::A),
39             0x2 => Ok(Foo::B),
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::A => 0x1,
48             Foo::B => 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,
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,
102 }
103 impl BarData {
conforms(bytes: &[u8]) -> bool104     fn conforms(bytes: &[u8]) -> bool {
105         bytes.len() >= 1
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() < 1 {
114             return Err(DecodeError::InvalidLengthError {
115                 obj: "Bar",
116                 wanted: 1,
117                 got: bytes.get().remaining(),
118             });
119         }
120         let x = Foo::try_from(bytes.get_mut().get_u8())
121             .map_err(|unknown_val| DecodeError::InvalidEnumValueError {
122                 obj: "Bar",
123                 field: "x",
124                 value: unknown_val as u64,
125                 type_: "Foo",
126             })?;
127         Ok(Self { x })
128     }
write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>129     fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> {
130         buffer.put_u8(u8::from(self.x));
131         Ok(())
132     }
get_total_size(&self) -> usize133     fn get_total_size(&self) -> usize {
134         self.get_size()
135     }
get_size(&self) -> usize136     fn get_size(&self) -> usize {
137         1
138     }
139 }
140 impl Packet for Bar {
encoded_len(&self) -> usize141     fn encoded_len(&self) -> usize {
142         self.get_size()
143     }
encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>144     fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> {
145         self.bar.write_to(buf)
146     }
decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError>147     fn decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError> {
148         unimplemented!("Rust legacy does not implement full packet trait")
149     }
150 }
151 impl TryFrom<Bar> for Bytes {
152     type Error = EncodeError;
try_from(packet: Bar) -> Result<Self, Self::Error>153     fn try_from(packet: Bar) -> Result<Self, Self::Error> {
154         packet.encode_to_bytes()
155     }
156 }
157 impl TryFrom<Bar> for Vec<u8> {
158     type Error = EncodeError;
try_from(packet: Bar) -> Result<Self, Self::Error>159     fn try_from(packet: Bar) -> Result<Self, Self::Error> {
160         packet.encode_to_vec()
161     }
162 }
163 impl Bar {
parse(bytes: &[u8]) -> Result<Self, DecodeError>164     pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
165         let mut cell = Cell::new(bytes);
166         let packet = Self::parse_inner(&mut cell)?;
167         Ok(packet)
168     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>169     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
170         let data = BarData::parse_inner(&mut bytes)?;
171         Self::new(data)
172     }
new(bar: BarData) -> Result<Self, DecodeError>173     fn new(bar: BarData) -> Result<Self, DecodeError> {
174         Ok(Self { bar })
175     }
get_x(&self) -> Foo176     pub fn get_x(&self) -> Foo {
177         self.bar.x
178     }
write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError>179     fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> {
180         self.bar.write_to(buffer)
181     }
get_size(&self) -> usize182     pub fn get_size(&self) -> usize {
183         self.bar.get_size()
184     }
185 }
186 impl BarBuilder {
build(self) -> Bar187     pub fn build(self) -> Bar {
188         let bar = BarData { x: self.x };
189         Bar::new(bar).unwrap()
190     }
191 }
192 impl From<BarBuilder> for Bar {
from(builder: BarBuilder) -> Bar193     fn from(builder: BarBuilder) -> Bar {
194         builder.build().into()
195     }
196 }
197