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: u32, 30 } 31 #[derive(Debug, Clone, PartialEq, Eq)] 32 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] 33 pub struct Foo { 34 #[cfg_attr(feature = "serde", serde(flatten))] 35 foo: FooData, 36 } 37 #[derive(Debug)] 38 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] 39 pub struct FooBuilder { 40 pub x: u32, 41 } 42 impl FooData { conforms(bytes: &[u8]) -> bool43 fn conforms(bytes: &[u8]) -> bool { 44 bytes.len() >= 3 45 } parse(bytes: &[u8]) -> Result<Self, DecodeError>46 fn parse(bytes: &[u8]) -> Result<Self, DecodeError> { 47 let mut cell = Cell::new(bytes); 48 let packet = Self::parse_inner(&mut cell)?; 49 Ok(packet) 50 } parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>51 fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> { 52 if bytes.get().remaining() < 3 { 53 return Err(DecodeError::InvalidLengthError { 54 obj: "Foo", 55 wanted: 3, 56 got: bytes.get().remaining(), 57 }); 58 } 59 let x = bytes.get_mut().get_uint(3) as u32; 60 Ok(Self { x }) 61 } write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>62 fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> { 63 if self.x > 0xff_ffff { 64 return Err(EncodeError::InvalidScalarValue { 65 packet: "Foo", 66 field: "x", 67 value: self.x as u64, 68 maximum_value: 0xff_ffff, 69 }); 70 } 71 buffer.put_uint(self.x as u64, 3); 72 Ok(()) 73 } get_total_size(&self) -> usize74 fn get_total_size(&self) -> usize { 75 self.get_size() 76 } get_size(&self) -> usize77 fn get_size(&self) -> usize { 78 3 79 } 80 } 81 impl Packet for Foo { encoded_len(&self) -> usize82 fn encoded_len(&self) -> usize { 83 self.get_size() 84 } encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>85 fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> { 86 self.foo.write_to(buf) 87 } decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError>88 fn decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError> { 89 unimplemented!("Rust legacy does not implement full packet trait") 90 } 91 } 92 impl TryFrom<Foo> for Bytes { 93 type Error = EncodeError; try_from(packet: Foo) -> Result<Self, Self::Error>94 fn try_from(packet: Foo) -> Result<Self, Self::Error> { 95 packet.encode_to_bytes() 96 } 97 } 98 impl TryFrom<Foo> for Vec<u8> { 99 type Error = EncodeError; try_from(packet: Foo) -> Result<Self, Self::Error>100 fn try_from(packet: Foo) -> Result<Self, Self::Error> { 101 packet.encode_to_vec() 102 } 103 } 104 impl Foo { parse(bytes: &[u8]) -> Result<Self, DecodeError>105 pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> { 106 let mut cell = Cell::new(bytes); 107 let packet = Self::parse_inner(&mut cell)?; 108 Ok(packet) 109 } parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>110 fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> { 111 let data = FooData::parse_inner(&mut bytes)?; 112 Self::new(data) 113 } new(foo: FooData) -> Result<Self, DecodeError>114 fn new(foo: FooData) -> Result<Self, DecodeError> { 115 Ok(Self { foo }) 116 } get_x(&self) -> u32117 pub fn get_x(&self) -> u32 { 118 self.foo.x 119 } write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError>120 fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> { 121 self.foo.write_to(buffer) 122 } get_size(&self) -> usize123 pub fn get_size(&self) -> usize { 124 self.foo.get_size() 125 } 126 } 127 impl FooBuilder { build(self) -> Foo128 pub fn build(self) -> Foo { 129 let foo = FooData { x: self.x }; 130 Foo::new(foo).unwrap() 131 } 132 } 133 impl From<FooBuilder> for Foo { from(builder: FooBuilder) -> Foo134 fn from(builder: FooBuilder) -> Foo { 135 builder.build().into() 136 } 137 } 138