• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     b: u64,
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 b: u64,
41 }
42 impl FooData {
conforms(bytes: &[u8]) -> bool43     fn conforms(bytes: &[u8]) -> bool {
44         bytes.len() >= 8
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() < 8 {
53             return Err(DecodeError::InvalidLengthError {
54                 obj: "Foo",
55                 wanted: 8,
56                 got: bytes.get().remaining(),
57             });
58         }
59         let chunk = bytes.get_mut().get_u64();
60         let fixed_value = (chunk & 0x7f) as u8;
61         if fixed_value != 7 {
62             return Err(DecodeError::InvalidFixedValue {
63                 expected: 7,
64                 actual: fixed_value as u64,
65             });
66         }
67         let b = ((chunk >> 7) & 0x1ff_ffff_ffff_ffff_u64);
68         Ok(Self { b })
69     }
write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError>70     fn write_to<T: BufMut>(&self, buffer: &mut T) -> Result<(), EncodeError> {
71         if self.b > 0x1ff_ffff_ffff_ffff_u64 {
72             return Err(EncodeError::InvalidScalarValue {
73                 packet: "Foo",
74                 field: "b",
75                 value: self.b as u64,
76                 maximum_value: 0x1ff_ffff_ffff_ffff_u64,
77             });
78         }
79         let value = (7 as u64) | (self.b << 7);
80         buffer.put_u64(value);
81         Ok(())
82     }
get_total_size(&self) -> usize83     fn get_total_size(&self) -> usize {
84         self.get_size()
85     }
get_size(&self) -> usize86     fn get_size(&self) -> usize {
87         8
88     }
89 }
90 impl Packet for Foo {
encoded_len(&self) -> usize91     fn encoded_len(&self) -> usize {
92         self.get_size()
93     }
encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>94     fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> {
95         self.foo.write_to(buf)
96     }
decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError>97     fn decode(_: &[u8]) -> Result<(Self, &[u8]), DecodeError> {
98         unimplemented!("Rust legacy does not implement full packet trait")
99     }
100 }
101 impl TryFrom<Foo> for Bytes {
102     type Error = EncodeError;
try_from(packet: Foo) -> Result<Self, Self::Error>103     fn try_from(packet: Foo) -> Result<Self, Self::Error> {
104         packet.encode_to_bytes()
105     }
106 }
107 impl TryFrom<Foo> for Vec<u8> {
108     type Error = EncodeError;
try_from(packet: Foo) -> Result<Self, Self::Error>109     fn try_from(packet: Foo) -> Result<Self, Self::Error> {
110         packet.encode_to_vec()
111     }
112 }
113 impl Foo {
parse(bytes: &[u8]) -> Result<Self, DecodeError>114     pub fn parse(bytes: &[u8]) -> Result<Self, DecodeError> {
115         let mut cell = Cell::new(bytes);
116         let packet = Self::parse_inner(&mut cell)?;
117         Ok(packet)
118     }
parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError>119     fn parse_inner(mut bytes: &mut Cell<&[u8]>) -> Result<Self, DecodeError> {
120         let data = FooData::parse_inner(&mut bytes)?;
121         Self::new(data)
122     }
new(foo: FooData) -> Result<Self, DecodeError>123     fn new(foo: FooData) -> Result<Self, DecodeError> {
124         Ok(Self { foo })
125     }
get_b(&self) -> u64126     pub fn get_b(&self) -> u64 {
127         self.foo.b
128     }
write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError>129     fn write_to(&self, buffer: &mut impl BufMut) -> Result<(), EncodeError> {
130         self.foo.write_to(buffer)
131     }
get_size(&self) -> usize132     pub fn get_size(&self) -> usize {
133         self.foo.get_size()
134     }
135 }
136 impl FooBuilder {
build(self) -> Foo137     pub fn build(self) -> Foo {
138         let foo = FooData { b: self.b };
139         Foo::new(foo).unwrap()
140     }
141 }
142 impl From<FooBuilder> for Foo {
from(builder: FooBuilder) -> Foo143     fn from(builder: FooBuilder) -> Foo {
144         builder.build().into()
145     }
146 }
147