• 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 = "u64", into = "u64"))]
30 pub enum Foo {
31     FooBar = 0x1,
32     Baz = 0x2,
33 }
34 impl TryFrom<u64> for Foo {
35     type Error = u64;
try_from(value: u64) -> Result<Self, Self::Error>36     fn try_from(value: u64) -> 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 u64 {
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 u64 {
from(value: Foo) -> Self53     fn from(value: Foo) -> Self {
54         (&value).into()
55     }
56 }
57 #[derive(Debug, Clone, PartialEq, Eq)]
58 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
59 pub struct BarData {
60     x: [Foo; 7],
61 }
62 #[derive(Debug, Clone, PartialEq, Eq)]
63 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
64 pub struct Bar {
65     #[cfg_attr(feature = "serde", serde(flatten))]
66     bar: BarData,
67 }
68 #[derive(Debug)]
69 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
70 pub struct BarBuilder {
71     pub x: [Foo; 7],
72 }
73 impl BarData {
conforms(bytes: &[u8]) -> bool74     fn conforms(bytes: &[u8]) -> bool {
75         bytes.len() >= 56
76     }
parse(bytes: &[u8]) -> Result<Self, DecodeError>77     fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
78         let mut cell = Cell::new(bytes);
79         let packet = Self::parse_inner(&mut cell)?;
80         Ok(packet)
81     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>82     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
83         if bytes.get().remaining() < 7 * 8 {
84             return Err(DecodeError::InvalidLengthError {
85                 obj: "Bar",
86                 wanted: 7 * 8,
87                 got: bytes.get().remaining(),
88             });
89         }
90         let x = (0..7)
91             .map(|_| {
92                 Foo::try_from(bytes.get_mut().get_u64())
93                     .map_err(|unknown_val| DecodeError::InvalidEnumValueError {
94                         obj: "Bar",
95                         field: "",
96                         value: unknown_val as u64,
97                         type_: "Foo",
98                     })
99             })
100             .collect::<Result<Vec<_>, DecodeError>>()?
101             .try_into()
102             .map_err(|_| DecodeError::InvalidPacketError)?;
103         Ok(Self { x })
104     }
write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>105     fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> {
106         for elem in &self.x {
107             buffer.put_u64(u64::from(elem));
108         }
109         Ok(())
110     }
get_total_size(&self) -> usize111     fn get_total_size(&self) -> usize {
112         self.get_size()
113     }
get_size(&self) -> usize114     fn get_size(&self) -> usize {
115         56
116     }
117 }
118 impl Packet for Bar {
encoded_len(&self) -> usize119     fn encoded_len(&self) -> usize {
120         self.get_size()
121     }
encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>122     fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> {
123         self.bar.write_to(buf)
124     }
decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError>125     fn decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError> {
126         unimplemented!("Rust legacy does not implement full packet trait")
127     }
128 }
129 impl TryFrom<Bar> for Bytes {
130     type Error = EncodeError;
try_from(packet: Bar) -> Result<Self, Self::Error>131     fn try_from(packet: Bar) -> Result<Self, Self::Error> {
132         packet.encode_to_bytes()
133     }
134 }
135 impl TryFrom<Bar> for Vec<u8> {
136     type Error = EncodeError;
try_from(packet: Bar) -> Result<Self, Self::Error>137     fn try_from(packet: Bar) -> Result<Self, Self::Error> {
138         packet.encode_to_vec()
139     }
140 }
141 impl Bar {
parse(bytes: &[u8]) -> Result<Self, DecodeError>142     pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
143         let mut cell = Cell::new(bytes);
144         let packet = Self::parse_inner(&mut cell)?;
145         Ok(packet)
146     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>147     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
148         let data = BarData::parse_inner(&mut bytes)?;
149         Self::new(data)
150     }
new(bar: BarData) -> Result<Self, DecodeError>151     fn new(bar: BarData) -> Result<Self, DecodeError> {
152         Ok(Self { bar })
153     }
get_x(&self) -> &[Foo; 7]154     pub fn get_x(&self) -> &[Foo; 7] {
155         &self.bar.x
156     }
write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError>157     fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> {
158         self.bar.write_to(buffer)
159     }
get_size(&self) -> usize160     pub fn get_size(&self) -> usize {
161         self.bar.get_size()
162     }
163 }
164 impl BarBuilder {
build(self) -> Bar165     pub fn build(self) -> Bar {
166         let bar = BarData { x: self.x };
167         Bar::new(bar).unwrap()
168     }
169 }
170 impl From<BarBuilder> for Bar {
from(builder: BarBuilder) -> Bar171     fn from(builder: BarBuilder) -> Bar {
172         builder.build().into()
173     }
174 }
175