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 FooData { 29 x: u8, 30 y: u16, 31 z: u32, 32 } 33 #[derive(Debug, Clone, PartialEq, Eq)] 34 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] 35 pub struct Foo { 36 #[cfg_attr(feature = "serde", serde(flatten))] 37 foo: FooData, 38 } 39 #[derive(Debug)] 40 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] 41 pub struct FooBuilder { 42 pub x: u8, 43 pub y: u16, 44 pub z: u32, 45 } 46 impl FooData { conforms(bytes: &[u8]) -> bool47 fn conforms(bytes: &[u8]) -> bool { 48 bytes.len() >= 6 49 } parse(bytes: &[u8]) -> Result<Self, DecodeError>50 fn parse(bytes: &[u8]) -> Result<Self, DecodeError> { 51 let mut cell = Cell::new(bytes); 52 let packet = Self::parse_inner(&mut cell)?; 53 Ok(packet) 54 } parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>55 fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> { 56 if bytes.get().remaining() < 1 { 57 return Err(DecodeError::InvalidLengthError { 58 obj: "Foo", 59 wanted: 1, 60 got: bytes.get().remaining(), 61 }); 62 } 63 let x = bytes.get_mut().get_u8(); 64 if bytes.get().remaining() < 2 { 65 return Err(DecodeError::InvalidLengthError { 66 obj: "Foo", 67 wanted: 2, 68 got: bytes.get().remaining(), 69 }); 70 } 71 let y = bytes.get_mut().get_u16(); 72 if bytes.get().remaining() < 3 { 73 return Err(DecodeError::InvalidLengthError { 74 obj: "Foo", 75 wanted: 3, 76 got: bytes.get().remaining(), 77 }); 78 } 79 let z = bytes.get_mut().get_uint(3) as u32; 80 Ok(Self { x, y, z }) 81 } write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>82 fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> { 83 buffer.put_u8(self.x); 84 buffer.put_u16(self.y); 85 if self.z > 0xff_ffff { 86 return Err(EncodeError::InvalidScalarValue { 87 packet: "Foo", 88 field: "z", 89 value: self.z as u64, 90 maximum_value: 0xff_ffff, 91 }); 92 } 93 buffer.put_uint(self.z as u64, 3); 94 Ok(()) 95 } get_total_size(&self) -> usize96 fn get_total_size(&self) -> usize { 97 self.get_size() 98 } get_size(&self) -> usize99 fn get_size(&self) -> usize { 100 6 101 } 102 } 103 impl Packet for Foo { encoded_len(&self) -> usize104 fn encoded_len(&self) -> usize { 105 self.get_size() 106 } encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>107 fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> { 108 self.foo.write_to(buf) 109 } decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError>110 fn decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError> { 111 unimplemented!("Rust legacy does not implement full packet trait") 112 } 113 } 114 impl TryFrom<Foo> for Bytes { 115 type Error = EncodeError; try_from(packet: Foo) -> Result<Self, Self::Error>116 fn try_from(packet: Foo) -> Result<Self, Self::Error> { 117 packet.encode_to_bytes() 118 } 119 } 120 impl TryFrom<Foo> for Vec<u8> { 121 type Error = EncodeError; try_from(packet: Foo) -> Result<Self, Self::Error>122 fn try_from(packet: Foo) -> Result<Self, Self::Error> { 123 packet.encode_to_vec() 124 } 125 } 126 impl Foo { parse(bytes: &[u8]) -> Result<Self, DecodeError>127 pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> { 128 let mut cell = Cell::new(bytes); 129 let packet = Self::parse_inner(&mut cell)?; 130 Ok(packet) 131 } parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>132 fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> { 133 let data = FooData::parse_inner(&mut bytes)?; 134 Self::new(data) 135 } new(foo: FooData) -> Result<Self, DecodeError>136 fn new(foo: FooData) -> Result<Self, DecodeError> { 137 Ok(Self { foo }) 138 } get_x(&self) -> u8139 pub fn get_x(&self) -> u8 { 140 self.foo.x 141 } get_y(&self) -> u16142 pub fn get_y(&self) -> u16 { 143 self.foo.y 144 } get_z(&self) -> u32145 pub fn get_z(&self) -> u32 { 146 self.foo.z 147 } write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError>148 fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> { 149 self.foo.write_to(buffer) 150 } get_size(&self) -> usize151 pub fn get_size(&self) -> usize { 152 self.foo.get_size() 153 } 154 } 155 impl FooBuilder { build(self) -> Foo156 pub fn build(self) -> Foo { 157 let foo = FooData { 158 x: self.x, 159 y: self.y, 160 z: self.z, 161 }; 162 Foo::new(foo).unwrap() 163 } 164 } 165 impl From<FooBuilder> for Foo { from(builder: FooBuilder) -> Foo166 fn from(builder: FooBuilder) -> Foo { 167 builder.build().into() 168 } 169 } 170