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 Foo { 29 pub a: u8, 30 pub b: u8, 31 pub c: u8, 32 pub d: u32, 33 pub e: u16, 34 pub f: u8, 35 } 36 impl Foo { conforms(bytes: &[u8]) -> bool37 fn conforms(bytes: &[u8]) -> bool { 38 bytes.len() >= 7 39 } parse(bytes: &[u8]) -> Result<Self, DecodeError>40 pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> { 41 let mut cell = Cell::new(bytes); 42 let packet = Self::parse_inner(&mut cell)?; 43 Ok(packet) 44 } parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>45 fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> { 46 if bytes.get().remaining() < 2 { 47 return Err(DecodeError::InvalidLengthError { 48 obj: "Foo", 49 wanted: 2, 50 got: bytes.get().remaining(), 51 }); 52 } 53 let chunk = bytes.get_mut().get_u16_le(); 54 let a = (chunk & 0x7) as u8; 55 let b = (chunk >> 3) as u8; 56 let c = ((chunk >> 11) & 0x1f) as u8; 57 if bytes.get().remaining() < 3 { 58 return Err(DecodeError::InvalidLengthError { 59 obj: "Foo", 60 wanted: 3, 61 got: bytes.get().remaining(), 62 }); 63 } 64 let d = bytes.get_mut().get_uint_le(3) as u32; 65 if bytes.get().remaining() < 2 { 66 return Err(DecodeError::InvalidLengthError { 67 obj: "Foo", 68 wanted: 2, 69 got: bytes.get().remaining(), 70 }); 71 } 72 let chunk = bytes.get_mut().get_u16_le(); 73 let e = (chunk & 0xfff); 74 let f = ((chunk >> 12) & 0xf) as u8; 75 Ok(Self { a, b, c, d, e, f }) 76 } write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>77 fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> { 78 if self.a > 0x7 { 79 return Err(EncodeError::InvalidScalarValue { 80 packet: "Foo", 81 field: "a", 82 value: self.a as u64, 83 maximum_value: 0x7, 84 }); 85 } 86 if self.c > 0x1f { 87 return Err(EncodeError::InvalidScalarValue { 88 packet: "Foo", 89 field: "c", 90 value: self.c as u64, 91 maximum_value: 0x1f, 92 }); 93 } 94 let value = (self.a as u16) | ((self.b as u16) << 3) | ((self.c as u16) << 11); 95 buffer.put_u16_le(value); 96 if self.d > 0xff_ffff { 97 return Err(EncodeError::InvalidScalarValue { 98 packet: "Foo", 99 field: "d", 100 value: self.d as u64, 101 maximum_value: 0xff_ffff, 102 }); 103 } 104 buffer.put_uint_le(self.d as u64, 3); 105 if self.e > 0xfff { 106 return Err(EncodeError::InvalidScalarValue { 107 packet: "Foo", 108 field: "e", 109 value: self.e as u64, 110 maximum_value: 0xfff, 111 }); 112 } 113 if self.f > 0xf { 114 return Err(EncodeError::InvalidScalarValue { 115 packet: "Foo", 116 field: "f", 117 value: self.f as u64, 118 maximum_value: 0xf, 119 }); 120 } 121 let value = self.e | ((self.f as u16) << 12); 122 buffer.put_u16_le(value); 123 Ok(()) 124 } get_total_size(&self) -> usize125 fn get_total_size(&self) -> usize { 126 self.get_size() 127 } get_size(&self) -> usize128 fn get_size(&self) -> usize { 129 7 130 } 131 } 132