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 a: u8, 30 b: u32, 31 c: u8, 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 a: u8, 43 pub b: u32, 44 pub c: u8, 45 } 46 impl FooData { conforms(bytes: &[u8]) -> bool47 fn conforms(bytes: &[u8]) -> bool { 48 bytes.len() >= 4 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() < 4 { 57 return Err(DecodeError::InvalidLengthError { 58 obj: "Foo", 59 wanted: 4, 60 got: bytes.get().remaining(), 61 }); 62 } 63 let chunk = bytes.get_mut().get_u32(); 64 let a = (chunk & 0x3) as u8; 65 let b = ((chunk >> 2) & 0xff_ffff); 66 let c = ((chunk >> 26) & 0x3f) as u8; 67 Ok(Self { a, b, c }) 68 } write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>69 fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> { 70 if self.a > 0x3 { 71 return Err(EncodeError::InvalidScalarValue { 72 packet: "Foo", 73 field: "a", 74 value: self.a as u64, 75 maximum_value: 0x3, 76 }); 77 } 78 if self.b > 0xff_ffff { 79 return Err(EncodeError::InvalidScalarValue { 80 packet: "Foo", 81 field: "b", 82 value: self.b as u64, 83 maximum_value: 0xff_ffff, 84 }); 85 } 86 if self.c > 0x3f { 87 return Err(EncodeError::InvalidScalarValue { 88 packet: "Foo", 89 field: "c", 90 value: self.c as u64, 91 maximum_value: 0x3f, 92 }); 93 } 94 let value = (self.a as u32) | (self.b << 2) | ((self.c as u32) << 26); 95 buffer.put_u32(value); 96 Ok(()) 97 } get_total_size(&self) -> usize98 fn get_total_size(&self) -> usize { 99 self.get_size() 100 } get_size(&self) -> usize101 fn get_size(&self) -> usize { 102 4 103 } 104 } 105 impl Packet for Foo { encoded_len(&self) -> usize106 fn encoded_len(&self) -> usize { 107 self.get_size() 108 } encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>109 fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> { 110 self.foo.write_to(buf) 111 } decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError>112 fn decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError> { 113 unimplemented!("Rust legacy does not implement full packet trait") 114 } 115 } 116 impl TryFrom<Foo> for Bytes { 117 type Error = EncodeError; try_from(packet: Foo) -> Result<Self, Self::Error>118 fn try_from(packet: Foo) -> Result<Self, Self::Error> { 119 packet.encode_to_bytes() 120 } 121 } 122 impl TryFrom<Foo> for Vec<u8> { 123 type Error = EncodeError; try_from(packet: Foo) -> Result<Self, Self::Error>124 fn try_from(packet: Foo) -> Result<Self, Self::Error> { 125 packet.encode_to_vec() 126 } 127 } 128 impl Foo { parse(bytes: &[u8]) -> Result<Self, DecodeError>129 pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> { 130 let mut cell = Cell::new(bytes); 131 let packet = Self::parse_inner(&mut cell)?; 132 Ok(packet) 133 } parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>134 fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> { 135 let data = FooData::parse_inner(&mut bytes)?; 136 Self::new(data) 137 } new(foo: FooData) -> Result<Self, DecodeError>138 fn new(foo: FooData) -> Result<Self, DecodeError> { 139 Ok(Self { foo }) 140 } get_a(&self) -> u8141 pub fn get_a(&self) -> u8 { 142 self.foo.a 143 } get_b(&self) -> u32144 pub fn get_b(&self) -> u32 { 145 self.foo.b 146 } get_c(&self) -> u8147 pub fn get_c(&self) -> u8 { 148 self.foo.c 149 } write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError>150 fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> { 151 self.foo.write_to(buffer) 152 } get_size(&self) -> usize153 pub fn get_size(&self) -> usize { 154 self.foo.get_size() 155 } 156 } 157 impl FooBuilder { build(self) -> Foo158 pub fn build(self) -> Foo { 159 let foo = FooData { 160 a: self.a, 161 b: self.b, 162 c: self.c, 163 }; 164 Foo::new(foo).unwrap() 165 } 166 } 167 impl From<FooBuilder> for Foo { from(builder: FooBuilder) -> Foo168 fn from(builder: FooBuilder) -> Foo { 169 builder.build().into() 170 } 171 } 172