pilota 0.1.0

Pilota is a thrift and protobuf implementation in pure rust with high performance and extensibility.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
pub mod binary;
pub mod error;
pub mod rw_ext;

use std::sync::Arc;

use bytes::{Buf, BufMut};
pub use error::*;
use tokio::io::AsyncRead;

pub use self::binary::TAsyncBinaryProtocol;

const MAXIMUM_SKIP_DEPTH: i8 = 64;

lazy_static::lazy_static! {
    pub static ref VOID_IDENT: TStructIdentifier = TStructIdentifier { name: "void" };
}

#[async_trait::async_trait]
pub trait Message: Sized + Send {
    fn encode<T: TOutputProtocol>(&self, protocol: &mut T) -> Result<(), Error>;

    fn decode<T: TInputProtocol>(protocol: &mut T) -> Result<Self, Error>;

    async fn decode_async<R>(protocol: &mut TAsyncBinaryProtocol<R>) -> Result<Self, Error>
    where
        R: AsyncRead + Unpin + Send;
}

#[async_trait::async_trait]
pub trait EntryMessage: Sized + Send {
    fn encode<T: TOutputProtocol>(&self, protocol: &mut T) -> Result<(), Error>;

    fn decode<T: TInputProtocol>(
        protocol: &mut T,
        msg_ident: &TMessageIdentifier,
    ) -> Result<Self, Error>;

    async fn decode_async<R>(
        protocol: &mut TAsyncBinaryProtocol<R>,
        msg_ident: &TMessageIdentifier,
    ) -> Result<Self, Error>
    where
        R: AsyncRead + Unpin + Send;
}

pub trait TInputProtocol {
    type Buf: Buf;
    /// Read the beginning of a Thrift message.
    fn read_message_begin(&mut self) -> Result<TMessageIdentifier, Error>;
    /// Read the end of a Thrift message.
    fn read_message_end(&mut self) -> Result<(), Error>;
    /// Read the beginning of a Thrift struct.
    fn read_struct_begin(&mut self) -> Result<Option<TStructIdentifier>, Error>;
    /// Read the end of a Thrift struct.
    fn read_struct_end(&mut self) -> Result<(), Error>;
    /// Read the beginning of a Thrift struct field.
    fn read_field_begin(&mut self) -> Result<TFieldIdentifier, Error>;
    /// Read the end of a Thrift struct field.
    fn read_field_end(&mut self) -> Result<(), Error>;
    /// Read a bool.
    fn read_bool(&mut self) -> Result<bool, Error>;
    /// Read a fixed-length byte array.
    fn read_bytes(&mut self) -> Result<Vec<u8>, Error>;
    /// Read a word.
    fn read_i8(&mut self) -> Result<i8, Error>;
    /// Read a 16-bit signed integer.
    fn read_i16(&mut self) -> Result<i16, Error>;
    /// Read a 32-bit signed integer.
    fn read_i32(&mut self) -> Result<i32, Error>;
    /// Read a 64-bit signed integer.
    fn read_i64(&mut self) -> Result<i64, Error>;
    /// Read a 64-bit float.
    fn read_double(&mut self) -> Result<f64, Error>;
    /// Read a fixed-length string (not null terminated).

    fn read_string(&mut self) -> Result<String, Error>;
    /// Read the beginning of a list.
    fn read_list_begin(&mut self) -> Result<TListIdentifier, Error>;
    /// Read the end of a list.
    fn read_list_end(&mut self) -> Result<(), Error>;
    /// Read the beginning of a set.
    fn read_set_begin(&mut self) -> Result<TSetIdentifier, Error>;
    /// Read the end of a set.
    fn read_set_end(&mut self) -> Result<(), Error>;
    /// Read the beginning of a map.
    fn read_map_begin(&mut self) -> Result<TMapIdentifier, Error>;
    /// Read the end of a map.
    fn read_map_end(&mut self) -> Result<(), Error>;
    /// Skip a field with type `field_type` recursively until the default
    /// maximum skip depth is reached.
    fn skip(&mut self, field_type: TType) -> Result<(), Error> {
        self.skip_till_depth(field_type, MAXIMUM_SKIP_DEPTH)
    }
    /// Skip a field with type `field_type` recursively up to `depth` levels.
    fn skip_till_depth(&mut self, field_type: TType, depth: i8) -> Result<(), Error> {
        if depth == 0 {
            return Err(new_protocol_error(
                ProtocolErrorKind::DepthLimit,
                format!("cannot parse past {:?}", field_type),
            ));
        }

        match field_type {
            TType::Bool => self.read_bool().map(|_| ()),
            TType::I08 => self.read_i8().map(|_| ()),
            TType::I16 => self.read_i16().map(|_| ()),
            TType::I32 => self.read_i32().map(|_| ()),
            TType::I64 => self.read_i64().map(|_| ()),
            TType::Double => self.read_double().map(|_| ()),
            TType::String => self.read_string().map(|_| ()),
            TType::Struct => {
                self.read_struct_begin()?;
                loop {
                    let field_ident = self.read_field_begin()?;
                    if field_ident.field_type == TType::Stop {
                        break;
                    }
                    self.skip_till_depth(field_ident.field_type, depth - 1)?;
                }
                self.read_struct_end()
            }
            TType::List => {
                let list_ident = self.read_list_begin()?;
                for _ in 0..list_ident.size {
                    self.skip_till_depth(list_ident.element_type, depth - 1)?;
                }
                self.read_list_end()
            }
            TType::Set => {
                let set_ident = self.read_set_begin()?;
                for _ in 0..set_ident.size {
                    self.skip_till_depth(set_ident.element_type, depth - 1)?;
                }
                self.read_set_end()
            }
            TType::Map => {
                let map_ident = self.read_map_begin()?;
                for _ in 0..map_ident.size {
                    let key_type = map_ident.key_type;
                    let val_type = map_ident.value_type;
                    self.skip_till_depth(key_type, depth - 1)?;
                    self.skip_till_depth(val_type, depth - 1)?;
                }
                self.read_map_end()
            }
            u => Err(new_protocol_error(
                ProtocolErrorKind::DepthLimit,
                format!("cannot skip field type {:?}", &u),
            )),
        }
    }

    // utility (DO NOT USE IN GENERATED CODE!!!!)
    //

    /// Read an unsigned byte.
    ///
    /// This method should **never** be used in generated code.
    fn read_byte(&mut self) -> Result<u8, Error>;

    fn buf_mut(&mut self) -> &mut Self::Buf;
}

pub trait TLengthProtocol {
    // size

    fn write_message_begin_len(&self, identifier: &TMessageIdentifier) -> usize;

    fn write_message_end_len(&self) -> usize;

    fn write_struct_begin_len(&self, identifier: &TStructIdentifier) -> usize;

    fn write_struct_end_len(&self) -> usize;

    fn write_field_begin_len(&self, identifier: &TFieldIdentifier) -> usize;

    fn write_field_end_len(&self) -> usize;

    fn write_field_stop_len(&self) -> usize;

    fn write_bool_len(&self, b: bool) -> usize;

    fn write_bytes_len(&self, b: &[u8]) -> usize;

    fn write_byte_len(&self, b: u8) -> usize;

    fn write_i8_len(&self, i: i8) -> usize;

    fn write_i16_len(&self, i: i16) -> usize;

    fn write_i32_len(&self, i: i32) -> usize;

    fn write_i64_len(&self, i: i64) -> usize;

    fn write_double_len(&self, d: f64) -> usize;

    fn write_string_len(&self, s: &str) -> usize;

    fn write_list_begin_len(&self, identifier: &TListIdentifier) -> usize;

    fn write_list_end_len(&self) -> usize;

    fn write_set_begin_len(&self, identifier: &TSetIdentifier) -> usize;

    fn write_set_end_len(&self) -> usize;

    fn write_map_begin_len(&self, identifier: &TMapIdentifier) -> usize;

    fn write_map_end_len(&self) -> usize;
}

pub trait TOutputProtocol: TLengthProtocol {
    type Buf: BufMut;

    /// Write the beginning of a Thrift message.
    fn write_message_begin(&mut self, identifier: &TMessageIdentifier) -> Result<(), Error>;
    /// Write the end of a Thrift message.
    fn write_message_end(&mut self) -> Result<(), Error>;
    /// Write the beginning of a Thrift struct.
    fn write_struct_begin(&mut self, identifier: &TStructIdentifier) -> Result<(), Error>;
    /// Write the end of a Thrift struct.
    fn write_struct_end(&mut self) -> Result<(), Error>;
    /// Write the beginning of a Thrift field.
    fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> Result<(), Error>;
    /// Write the end of a Thrift field.
    fn write_field_end(&mut self) -> Result<(), Error>;
    /// Write a STOP field indicating that all the fields in a struct have been
    /// written.
    fn write_field_stop(&mut self) -> Result<(), Error>;
    /// Write a bool.
    fn write_bool(&mut self, b: bool) -> Result<(), Error>;
    /// Write a fixed-length byte array.
    fn write_bytes(&mut self, b: &[u8]) -> Result<(), Error>;

    fn write_byte(&mut self, b: u8) -> Result<(), Error>;
    /// Write an 8-bit signed integer.
    fn write_i8(&mut self, i: i8) -> Result<(), Error>;
    /// Write a 16-bit signed integer.
    fn write_i16(&mut self, i: i16) -> Result<(), Error>;
    /// Write a 32-bit signed integer.
    fn write_i32(&mut self, i: i32) -> Result<(), Error>;
    /// Write a 64-bit signed integer.
    fn write_i64(&mut self, i: i64) -> Result<(), Error>;
    /// Write a 64-bit float.
    fn write_double(&mut self, d: f64) -> Result<(), Error>;
    /// Write a fixed-length string.
    fn write_string(&mut self, s: &str) -> Result<(), Error>;
    /// Write the beginning of a list.
    fn write_list_begin(&mut self, identifier: &TListIdentifier) -> Result<(), Error>;
    /// Write the end of a list.
    fn write_list_end(&mut self) -> Result<(), Error>;
    /// Write the beginning of a set.
    fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> Result<(), Error>;
    /// Write the end of a set.
    fn write_set_end(&mut self) -> Result<(), Error>;
    /// Write the beginning of a map.
    fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> Result<(), Error>;
    /// Write the end of a map.
    fn write_map_end(&mut self) -> Result<(), Error>;
    /// Flush buffered bytes to the underlying transport.
    fn flush(&mut self) -> Result<(), Error>;

    fn reserve(&mut self, size: usize);

    fn buf_mut(&mut self) -> &mut Self::Buf;
}

// Thrift struct identifier.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TStructIdentifier {
    /// Name of the encoded Thrift struct.
    pub name: &'static str,
}

impl TStructIdentifier {
    /// Create a `TStructIdentifier` for a struct named `name`.
    pub fn new(name: &'static str) -> TStructIdentifier {
        TStructIdentifier { name }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u8)]
pub enum TType {
    Stop = 0,
    Void = 1,
    Bool = 2,
    I08 = 3,
    Double = 4,
    I16 = 6,
    I32 = 8,
    I64 = 10,
    String = 11,
    Struct = 12,
    Map = 13,
    Set = 14,
    List = 15,
    Utf8 = 16,
    Utf16 = 17,
}

impl From<TType> for u8 {
    fn from(ttype: TType) -> Self {
        ttype as u8
    }
}

impl TryFrom<u8> for TType {
    type Error = Error;

    #[inline]
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(TType::Stop),
            1 => Ok(TType::Void),
            2 => Ok(TType::Bool),
            3 => Ok(TType::I08),
            4 => Ok(TType::Double),
            6 => Ok(TType::I16),
            8 => Ok(TType::I32),
            10 => Ok(TType::I64),
            11 => Ok(TType::String),
            12 => Ok(TType::Struct),
            13 => Ok(TType::Map),
            14 => Ok(TType::Set),
            15 => Ok(TType::List),
            16 => Ok(TType::Utf8),
            17 => Ok(TType::Utf16),
            _ => Err(new_protocol_error(
                ProtocolErrorKind::InvalidData,
                format!("invalid ttype {}", value),
            )),
        }
    }
}

/// Thrift message types.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u8)]
pub enum TMessageType {
    /// Service-call request.
    Call = 1,
    /// Service-call response.
    Reply = 2,
    /// Unexpected error in the remote service.
    Exception = 3,
    /// One-way service-call request (no response is expected).
    OneWay = 4,
}

impl TryFrom<u8> for TMessageType {
    type Error = Error;

    #[inline]
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            1 => Ok(TMessageType::Call),
            2 => Ok(TMessageType::Reply),
            3 => Ok(TMessageType::Exception),
            4 => Ok(TMessageType::OneWay),
            _ => Err(new_protocol_error(
                ProtocolErrorKind::InvalidData,
                format!("invalid tmessage type {}", value),
            )),
        }
    }
}

impl From<TMessageType> for u8 {
    fn from(t: TMessageType) -> Self {
        t as u8
    }
}

/// Thrift message identifier.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TMessageIdentifier {
    /// Service call the message is associated with.
    pub name: smol_str::SmolStr,
    /// Message type.
    pub message_type: TMessageType,
    /// Ordered sequence number identifying the message.
    pub sequence_number: i32,
}

impl TMessageIdentifier {
    /// Create a `TMessageIdentifier` for a Thrift service-call named `name`
    /// with message type `message_type` and sequence number `sequence_number`.
    pub fn new(
        name: smol_str::SmolStr,
        message_type: TMessageType,
        sequence_number: i32,
    ) -> TMessageIdentifier {
        TMessageIdentifier {
            name,
            message_type,
            sequence_number,
        }
    }
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TListIdentifier {
    /// Type of the elements in the list.
    pub element_type: TType,
    /// Number of elements in the list.
    pub size: usize,
}

impl TListIdentifier {
    /// Create a `TListIdentifier` for a list with `size` elements of type
    /// `element_type`.
    pub fn new(element_type: TType, size: usize) -> TListIdentifier {
        TListIdentifier { element_type, size }
    }
}

/// Thrift set identifier.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TSetIdentifier {
    /// Type of the elements in the set.
    pub element_type: TType,
    /// Number of elements in the set.
    pub size: usize,
}

impl TSetIdentifier {
    /// Create a `TSetIdentifier` for a set with `size` elements of type
    /// `element_type`.
    pub fn new(element_type: TType, size: usize) -> TSetIdentifier {
        TSetIdentifier { element_type, size }
    }
}

/// Thrift field identifier.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TFieldIdentifier {
    /// Name of the Thrift field.
    ///
    /// `None` if it's not sent over the wire.
    pub name: Option<&'static str>,
    /// Field type.
    ///
    /// This may be a primitive, container, or a struct.
    pub field_type: TType,
    /// Thrift field id.
    ///
    /// `None` only if `field_type` is `TType::Stop`.
    pub id: Option<i16>,
}

impl TFieldIdentifier {
    /// Create a `TFieldIdentifier` for a field named `name` with type
    /// `field_type` and field id `id`.
    ///
    /// `id` should be `None` if `field_type` is `TType::Stop`.
    pub fn new<N, I>(name: N, field_type: TType, id: I) -> TFieldIdentifier
    where
        N: Into<Option<&'static str>>,
        I: Into<Option<i16>>,
    {
        TFieldIdentifier {
            name: name.into(),
            field_type,
            id: id.into(),
        }
    }
}

/// Thrift map identifier.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TMapIdentifier {
    /// Map key type.
    pub key_type: TType,
    /// Map value type.
    pub value_type: TType,
    /// Number of entries in the map.
    pub size: usize,
}

impl TMapIdentifier {
    /// Create a `TMapIdentifier` for a map with `size` entries of type
    /// `key_type -> value_type`.
    pub fn new<K, V>(key_type: K, value_type: V, size: usize) -> TMapIdentifier
    where
        K: Into<TType>,
        V: Into<TType>,
    {
        TMapIdentifier {
            key_type: key_type.into(),
            value_type: value_type.into(),
            size,
        }
    }
}

pub trait Size {
    fn size<T: TLengthProtocol>(&self, protocol: &T) -> usize;
}

#[async_trait::async_trait]
impl<Message> EntryMessage for Arc<Message>
where
    Message: EntryMessage + Sync,
{
    fn encode<T: TOutputProtocol>(&self, protocol: &mut T) -> Result<(), Error> {
        (**self).encode(protocol)
    }

    fn decode<T: TInputProtocol>(
        protocol: &mut T,
        msg_ident: &TMessageIdentifier,
    ) -> Result<Self, Error> {
        Message::decode(protocol, msg_ident).map(Arc::new)
    }

    async fn decode_async<R>(
        protocol: &mut TAsyncBinaryProtocol<R>,
        msg_ident: &TMessageIdentifier,
    ) -> Result<Self, Error>
    where
        R: AsyncRead + Unpin + Send,
    {
        Message::decode_async(protocol, msg_ident)
            .await
            .map(Arc::new)
    }
}

impl<Message> Size for Arc<Message>
where
    Message: Size + Sync,
{
    fn size<T: TLengthProtocol>(&self, protocol: &T) -> usize {
        (**self).size(protocol)
    }
}