starpc 0.46.0 → 0.46.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,286 +0,0 @@
1
- //! Protocol buffer types for rpcstream.
2
- //!
3
- //! These types are defined manually to match the rpcstream.proto definitions.
4
- //! They will be generated via protoc-gen-prost in a future update.
5
-
6
- use bytes::{Buf, BufMut, Bytes};
7
- use prost::{DecodeError, Message};
8
-
9
- /// RpcStreamPacket is a packet encapsulating data for a RPC stream.
10
- #[derive(Clone, PartialEq, Debug, Default)]
11
- pub struct RpcStreamPacket {
12
- /// Body of the packet.
13
- pub body: Option<RpcStreamPacketBody>,
14
- }
15
-
16
- /// Body variants for RpcStreamPacket.
17
- #[derive(Clone, PartialEq, Debug)]
18
- pub enum RpcStreamPacketBody {
19
- /// Init is the first packet in the stream, sent by the initiator.
20
- Init(RpcStreamInit),
21
- /// Ack is sent in response to Init, by the server.
22
- Ack(RpcAck),
23
- /// Data is the encapsulated data packet.
24
- Data(Bytes),
25
- }
26
-
27
- /// RpcStreamInit is the first message in a RPC stream.
28
- #[derive(Clone, PartialEq, Debug, Default)]
29
- pub struct RpcStreamInit {
30
- /// ComponentId is the identifier of the component making the request.
31
- pub component_id: String,
32
- }
33
-
34
- /// RpcAck is the acknowledgment message in a RPC stream.
35
- #[derive(Clone, PartialEq, Debug, Default)]
36
- pub struct RpcAck {
37
- /// Error indicates there was some error setting up the stream.
38
- pub error: String,
39
- }
40
-
41
- // Manual Message implementation for RpcStreamInit
42
- impl Message for RpcStreamInit {
43
- fn encode_raw(&self, buf: &mut impl BufMut)
44
- where
45
- Self: Sized,
46
- {
47
- if !self.component_id.is_empty() {
48
- prost::encoding::string::encode(1, &self.component_id, buf);
49
- }
50
- }
51
-
52
- fn merge_field(
53
- &mut self,
54
- tag: u32,
55
- wire_type: prost::encoding::WireType,
56
- buf: &mut impl Buf,
57
- ctx: prost::encoding::DecodeContext,
58
- ) -> Result<(), DecodeError>
59
- where
60
- Self: Sized,
61
- {
62
- match tag {
63
- 1 => prost::encoding::string::merge(wire_type, &mut self.component_id, buf, ctx),
64
- _ => prost::encoding::skip_field(wire_type, tag, buf, ctx),
65
- }
66
- }
67
-
68
- fn encoded_len(&self) -> usize {
69
- let mut len = 0;
70
- if !self.component_id.is_empty() {
71
- len += prost::encoding::string::encoded_len(1, &self.component_id);
72
- }
73
- len
74
- }
75
-
76
- fn clear(&mut self) {
77
- self.component_id.clear();
78
- }
79
- }
80
-
81
- // Manual Message implementation for RpcAck
82
- impl Message for RpcAck {
83
- fn encode_raw(&self, buf: &mut impl BufMut)
84
- where
85
- Self: Sized,
86
- {
87
- if !self.error.is_empty() {
88
- prost::encoding::string::encode(1, &self.error, buf);
89
- }
90
- }
91
-
92
- fn merge_field(
93
- &mut self,
94
- tag: u32,
95
- wire_type: prost::encoding::WireType,
96
- buf: &mut impl Buf,
97
- ctx: prost::encoding::DecodeContext,
98
- ) -> Result<(), DecodeError>
99
- where
100
- Self: Sized,
101
- {
102
- match tag {
103
- 1 => prost::encoding::string::merge(wire_type, &mut self.error, buf, ctx),
104
- _ => prost::encoding::skip_field(wire_type, tag, buf, ctx),
105
- }
106
- }
107
-
108
- fn encoded_len(&self) -> usize {
109
- let mut len = 0;
110
- if !self.error.is_empty() {
111
- len += prost::encoding::string::encoded_len(1, &self.error);
112
- }
113
- len
114
- }
115
-
116
- fn clear(&mut self) {
117
- self.error.clear();
118
- }
119
- }
120
-
121
- // Manual Message implementation for RpcStreamPacket
122
- impl Message for RpcStreamPacket {
123
- fn encode_raw(&self, buf: &mut impl BufMut)
124
- where
125
- Self: Sized,
126
- {
127
- match &self.body {
128
- Some(RpcStreamPacketBody::Init(init)) => {
129
- // Tag 1, wire type 2 (length-delimited)
130
- prost::encoding::message::encode(1, init, buf);
131
- }
132
- Some(RpcStreamPacketBody::Ack(ack)) => {
133
- // Tag 2, wire type 2 (length-delimited)
134
- prost::encoding::message::encode(2, ack, buf);
135
- }
136
- Some(RpcStreamPacketBody::Data(data)) => {
137
- // Tag 3, wire type 2 (length-delimited)
138
- prost::encoding::bytes::encode(3, data, buf);
139
- }
140
- None => {}
141
- }
142
- }
143
-
144
- fn merge_field(
145
- &mut self,
146
- tag: u32,
147
- wire_type: prost::encoding::WireType,
148
- buf: &mut impl Buf,
149
- ctx: prost::encoding::DecodeContext,
150
- ) -> Result<(), DecodeError>
151
- where
152
- Self: Sized,
153
- {
154
- match tag {
155
- 1 => {
156
- let mut init = RpcStreamInit::default();
157
- prost::encoding::message::merge(wire_type, &mut init, buf, ctx)?;
158
- self.body = Some(RpcStreamPacketBody::Init(init));
159
- Ok(())
160
- }
161
- 2 => {
162
- let mut ack = RpcAck::default();
163
- prost::encoding::message::merge(wire_type, &mut ack, buf, ctx)?;
164
- self.body = Some(RpcStreamPacketBody::Ack(ack));
165
- Ok(())
166
- }
167
- 3 => {
168
- let mut data = Bytes::default();
169
- prost::encoding::bytes::merge(wire_type, &mut data, buf, ctx)?;
170
- self.body = Some(RpcStreamPacketBody::Data(data));
171
- Ok(())
172
- }
173
- _ => prost::encoding::skip_field(wire_type, tag, buf, ctx),
174
- }
175
- }
176
-
177
- fn encoded_len(&self) -> usize {
178
- match &self.body {
179
- Some(RpcStreamPacketBody::Init(init)) => prost::encoding::message::encoded_len(1, init),
180
- Some(RpcStreamPacketBody::Ack(ack)) => prost::encoding::message::encoded_len(2, ack),
181
- Some(RpcStreamPacketBody::Data(data)) => prost::encoding::bytes::encoded_len(3, data),
182
- None => 0,
183
- }
184
- }
185
-
186
- fn clear(&mut self) {
187
- self.body = None;
188
- }
189
- }
190
-
191
- impl RpcStreamPacket {
192
- /// Creates a new Init packet.
193
- pub fn new_init(component_id: String) -> Self {
194
- Self {
195
- body: Some(RpcStreamPacketBody::Init(RpcStreamInit { component_id })),
196
- }
197
- }
198
-
199
- /// Creates a new Ack packet.
200
- pub fn new_ack(error: String) -> Self {
201
- Self {
202
- body: Some(RpcStreamPacketBody::Ack(RpcAck { error })),
203
- }
204
- }
205
-
206
- /// Creates a new Data packet.
207
- pub fn new_data(data: Bytes) -> Self {
208
- Self {
209
- body: Some(RpcStreamPacketBody::Data(data)),
210
- }
211
- }
212
- }
213
-
214
- #[cfg(test)]
215
- mod tests {
216
- use super::*;
217
-
218
- #[test]
219
- fn test_rpc_stream_init_encode_decode() {
220
- let init = RpcStreamInit {
221
- component_id: "test-component".to_string(),
222
- };
223
-
224
- let encoded = init.encode_to_vec();
225
- let decoded = RpcStreamInit::decode(&encoded[..]).unwrap();
226
-
227
- assert_eq!(init, decoded);
228
- }
229
-
230
- #[test]
231
- fn test_rpc_ack_encode_decode() {
232
- let ack = RpcAck {
233
- error: "test error".to_string(),
234
- };
235
-
236
- let encoded = ack.encode_to_vec();
237
- let decoded = RpcAck::decode(&encoded[..]).unwrap();
238
-
239
- assert_eq!(ack, decoded);
240
- }
241
-
242
- #[test]
243
- fn test_rpc_stream_packet_init() {
244
- let packet = RpcStreamPacket::new_init("my-component".to_string());
245
-
246
- let encoded = packet.encode_to_vec();
247
- let decoded = RpcStreamPacket::decode(&encoded[..]).unwrap();
248
-
249
- match decoded.body {
250
- Some(RpcStreamPacketBody::Init(init)) => {
251
- assert_eq!(init.component_id, "my-component");
252
- }
253
- _ => panic!("Expected Init body"),
254
- }
255
- }
256
-
257
- #[test]
258
- fn test_rpc_stream_packet_ack() {
259
- let packet = RpcStreamPacket::new_ack("".to_string());
260
-
261
- let encoded = packet.encode_to_vec();
262
- let decoded = RpcStreamPacket::decode(&encoded[..]).unwrap();
263
-
264
- match decoded.body {
265
- Some(RpcStreamPacketBody::Ack(ack)) => {
266
- assert!(ack.error.is_empty());
267
- }
268
- _ => panic!("Expected Ack body"),
269
- }
270
- }
271
-
272
- #[test]
273
- fn test_rpc_stream_packet_data() {
274
- let packet = RpcStreamPacket::new_data(Bytes::from(vec![1, 2, 3, 4]));
275
-
276
- let encoded = packet.encode_to_vec();
277
- let decoded = RpcStreamPacket::decode(&encoded[..]).unwrap();
278
-
279
- match decoded.body {
280
- Some(RpcStreamPacketBody::Data(data)) => {
281
- assert_eq!(&data[..], &[1, 2, 3, 4]);
282
- }
283
- _ => panic!("Expected Data body"),
284
- }
285
- }
286
- }