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 = "u64", into = "u64"))] 30 pub enum Foo { 31 A = 0x1, 32 B = 0x2, 33 } 34 impl TryFrom<u64> for Foo { 35 type Error = u64; try_from(value: u64) -> Result<Self, Self::Error>36 fn try_from(value: u64) -> Result<Self, Self::Error> { 37 match value { 38 0x1 => Ok(Foo::A), 39 0x2 => Ok(Foo::B), 40 _ => Err(value), 41 } 42 } 43 } 44 impl From<&Foo> for u64 { from(value: &Foo) -> Self45 fn from(value: &Foo) -> Self { 46 match value { 47 Foo::A => 0x1, 48 Foo::B => 0x2, 49 } 50 } 51 } 52 impl From<Foo> for u64 { from(value: Foo) -> Self53 fn from(value: Foo) -> Self { 54 (&value).into() 55 } 56 } 57 #[derive(Debug, Clone, PartialEq, Eq)] 58 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] 59 pub struct BarData { 60 x: Foo, 61 } 62 #[derive(Debug, Clone, PartialEq, Eq)] 63 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] 64 pub struct Bar { 65 #[cfg_attr(feature = "serde", serde(flatten))] 66 bar: BarData, 67 } 68 #[derive(Debug)] 69 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] 70 pub struct BarBuilder { 71 pub x: Foo, 72 } 73 impl BarData { conforms(bytes: &[u8]) -> bool74 fn conforms(bytes: &[u8]) -> bool { 75 bytes.len() >= 8 76 } parse(bytes: &[u8]) -> Result<Self, DecodeError>77 fn parse(bytes: &[u8]) -> Result<Self, DecodeError> { 78 let mut cell = Cell::new(bytes); 79 let packet = Self::parse_inner(&mut cell)?; 80 Ok(packet) 81 } parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>82 fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> { 83 if bytes.get().remaining() < 8 { 84 return Err(DecodeError::InvalidLengthError { 85 obj: "Bar", 86 wanted: 8, 87 got: bytes.get().remaining(), 88 }); 89 } 90 let x = Foo::try_from(bytes.get_mut().get_u64()) 91 .map_err(|unknown_val| DecodeError::InvalidEnumValueError { 92 obj: "Bar", 93 field: "x", 94 value: unknown_val as u64, 95 type_: "Foo", 96 })?; 97 Ok(Self { x }) 98 } write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>99 fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> { 100 buffer.put_u64(u64::from(self.x)); 101 Ok(()) 102 } get_total_size(&self) -> usize103 fn get_total_size(&self) -> usize { 104 self.get_size() 105 } get_size(&self) -> usize106 fn get_size(&self) -> usize { 107 8 108 } 109 } 110 impl Packet for Bar { encoded_len(&self) -> usize111 fn encoded_len(&self) -> usize { 112 self.get_size() 113 } encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>114 fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> { 115 self.bar.write_to(buf) 116 } decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError>117 fn decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError> { 118 unimplemented!("Rust legacy does not implement full packet trait") 119 } 120 } 121 impl TryFrom<Bar> for Bytes { 122 type Error = EncodeError; try_from(packet: Bar) -> Result<Self, Self::Error>123 fn try_from(packet: Bar) -> Result<Self, Self::Error> { 124 packet.encode_to_bytes() 125 } 126 } 127 impl TryFrom<Bar> for Vec<u8> { 128 type Error = EncodeError; try_from(packet: Bar) -> Result<Self, Self::Error>129 fn try_from(packet: Bar) -> Result<Self, Self::Error> { 130 packet.encode_to_vec() 131 } 132 } 133 impl Bar { parse(bytes: &[u8]) -> Result<Self, DecodeError>134 pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> { 135 let mut cell = Cell::new(bytes); 136 let packet = Self::parse_inner(&mut cell)?; 137 Ok(packet) 138 } parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>139 fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> { 140 let data = BarData::parse_inner(&mut bytes)?; 141 Self::new(data) 142 } new(bar: BarData) -> Result<Self, DecodeError>143 fn new(bar: BarData) -> Result<Self, DecodeError> { 144 Ok(Self { bar }) 145 } get_x(&self) -> Foo146 pub fn get_x(&self) -> Foo { 147 self.bar.x 148 } write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError>149 fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> { 150 self.bar.write_to(buffer) 151 } get_size(&self) -> usize152 pub fn get_size(&self) -> usize { 153 self.bar.get_size() 154 } 155 } 156 impl BarBuilder { build(self) -> Bar157 pub fn build(self) -> Bar { 158 let bar = BarData { x: self.x }; 159 Bar::new(bar).unwrap() 160 } 161 } 162 impl From<BarBuilder> for Bar { from(builder: BarBuilder) -> Bar163 fn from(builder: BarBuilder) -> Bar { 164 builder.build().into() 165 } 166 } 167