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 = "u32", into = "u32"))] 30 pub enum Foo { 31 A = 0x1, 32 B = 0x2, 33 } 34 impl TryFrom<u32> for Foo { 35 type Error = u32; try_from(value: u32) -> Result<Self, Self::Error>36 fn try_from(value: u32) -> 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 u32 { 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 u32 { from(value: Foo) -> Self53 fn from(value: Foo) -> Self { 54 (&value).into() 55 } 56 } 57 impl From<Foo> for i32 { from(value: Foo) -> Self58 fn from(value: Foo) -> Self { 59 u32::from(value) as Self 60 } 61 } 62 impl From<Foo> for i64 { from(value: Foo) -> Self63 fn from(value: Foo) -> Self { 64 u32::from(value) as Self 65 } 66 } 67 impl From<Foo> for u64 { from(value: Foo) -> Self68 fn from(value: Foo) -> Self { 69 u32::from(value) as Self 70 } 71 } 72 #[derive(Debug, Clone, PartialEq, Eq)] 73 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] 74 pub struct BarData { 75 x: Foo, 76 } 77 #[derive(Debug, Clone, PartialEq, Eq)] 78 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] 79 pub struct Bar { 80 #[cfg_attr(feature = "serde", serde(flatten))] 81 bar: BarData, 82 } 83 #[derive(Debug)] 84 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] 85 pub struct BarBuilder { 86 pub x: Foo, 87 } 88 impl BarData { conforms(bytes: &[u8]) -> bool89 fn conforms(bytes: &[u8]) -> bool { 90 bytes.len() >= 3 91 } parse(bytes: &[u8]) -> Result<Self, DecodeError>92 fn parse(bytes: &[u8]) -> Result<Self, DecodeError> { 93 let mut cell = Cell::new(bytes); 94 let packet = Self::parse_inner(&mut cell)?; 95 Ok(packet) 96 } parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>97 fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> { 98 if bytes.get().remaining() < 3 { 99 return Err(DecodeError::InvalidLengthError { 100 obj: "Bar", 101 wanted: 3, 102 got: bytes.get().remaining(), 103 }); 104 } 105 let x = Foo::try_from(bytes.get_mut().get_uint(3) as u32) 106 .map_err(|unknown_val| DecodeError::InvalidEnumValueError { 107 obj: "Bar", 108 field: "x", 109 value: unknown_val as u64, 110 type_: "Foo", 111 })?; 112 Ok(Self { x }) 113 } write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>114 fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> { 115 buffer.put_uint(u32::from(self.x) as u64, 3); 116 Ok(()) 117 } get_total_size(&self) -> usize118 fn get_total_size(&self) -> usize { 119 self.get_size() 120 } get_size(&self) -> usize121 fn get_size(&self) -> usize { 122 3 123 } 124 } 125 impl Packet for Bar { encoded_len(&self) -> usize126 fn encoded_len(&self) -> usize { 127 self.get_size() 128 } encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>129 fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> { 130 self.bar.write_to(buf) 131 } decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError>132 fn decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError> { 133 unimplemented!("Rust legacy does not implement full packet trait") 134 } 135 } 136 impl TryFrom<Bar> for Bytes { 137 type Error = EncodeError; try_from(packet: Bar) -> Result<Self, Self::Error>138 fn try_from(packet: Bar) -> Result<Self, Self::Error> { 139 packet.encode_to_bytes() 140 } 141 } 142 impl TryFrom<Bar> for Vec<u8> { 143 type Error = EncodeError; try_from(packet: Bar) -> Result<Self, Self::Error>144 fn try_from(packet: Bar) -> Result<Self, Self::Error> { 145 packet.encode_to_vec() 146 } 147 } 148 impl Bar { parse(bytes: &[u8]) -> Result<Self, DecodeError>149 pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> { 150 let mut cell = Cell::new(bytes); 151 let packet = Self::parse_inner(&mut cell)?; 152 Ok(packet) 153 } parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>154 fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> { 155 let data = BarData::parse_inner(&mut bytes)?; 156 Self::new(data) 157 } new(bar: BarData) -> Result<Self, DecodeError>158 fn new(bar: BarData) -> Result<Self, DecodeError> { 159 Ok(Self { bar }) 160 } get_x(&self) -> Foo161 pub fn get_x(&self) -> Foo { 162 self.bar.x 163 } write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError>164 fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> { 165 self.bar.write_to(buffer) 166 } get_size(&self) -> usize167 pub fn get_size(&self) -> usize { 168 self.bar.get_size() 169 } 170 } 171 impl BarBuilder { build(self) -> Bar172 pub fn build(self) -> Bar { 173 let bar = BarData { x: self.x }; 174 Bar::new(bar).unwrap() 175 } 176 } 177 impl From<BarBuilder> for Bar { from(builder: BarBuilder) -> Bar178 fn from(builder: BarBuilder) -> Bar { 179 builder.build().into() 180 } 181 } 182