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