1use std::convert::{From, TryFrom};
20use std::fmt;
21use std::fmt::{Display, Formatter};
22
23use crate::thrift::{ApplicationError, Error, ProtocolError, ProtocolErrorKind};
24
25mod compact;
26
27pub use compact::TCompactInputProtocol;
28
29mod compact_write;
30pub use compact_write::TCompactOutputProtocol;
31
32#[cfg(feature = "async")]
33#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
34mod stream;
35#[cfg(feature = "async")]
36#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
37pub use stream::{AsyncReadThrift, TInputStreamProtocol, TOutputStreamProtocol};
38
39#[cfg(feature = "async")]
40#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
41mod compact_stream;
42#[cfg(feature = "async")]
43#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
44pub use compact_stream::TCompactInputStreamProtocol;
45
46#[cfg(feature = "async")]
47#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
48mod compact_stream_write;
49#[cfg(feature = "async")]
50#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
51pub use compact_stream_write::TCompactOutputStreamProtocol;
52
53const MAXIMUM_SKIP_DEPTH: i8 = 64;
58
59pub trait ReadThrift: Sized {
60 fn read_from_in_protocol<T: TInputProtocol>(i_prot: &mut T) -> crate::thrift::Result<Self>;
61}
62
63impl ReadThrift for String {
64 fn read_from_in_protocol<T: TInputProtocol>(i_prot: &mut T) -> crate::thrift::Result<Self> {
65 i_prot.read_string()
66 }
67}
68
69impl ReadThrift for bool {
70 fn read_from_in_protocol<T: TInputProtocol>(i_prot: &mut T) -> crate::thrift::Result<Self> {
71 i_prot.read_bool()
72 }
73}
74
75impl ReadThrift for u8 {
76 fn read_from_in_protocol<T: TInputProtocol>(i_prot: &mut T) -> crate::thrift::Result<Self> {
77 i_prot.read_byte()
78 }
79}
80
81impl ReadThrift for i64 {
82 fn read_from_in_protocol<T: TInputProtocol>(i_prot: &mut T) -> crate::thrift::Result<Self> {
83 i_prot.read_i64()
84 }
85}
86
87impl ReadThrift for Vec<u8> {
88 fn read_from_in_protocol<T: TInputProtocol>(i_prot: &mut T) -> crate::thrift::Result<Self> {
89 i_prot.read_bytes()
90 }
91}
92
93pub trait TInputProtocol: Sized {
104 fn read_message_begin(&mut self) -> crate::thrift::Result<TMessageIdentifier>;
106 fn read_message_end(&mut self) -> crate::thrift::Result<()>;
108 fn read_struct_begin(&mut self) -> crate::thrift::Result<Option<TStructIdentifier>>;
110 fn read_struct_end(&mut self) -> crate::thrift::Result<()>;
112 fn read_field_begin(&mut self) -> crate::thrift::Result<TFieldIdentifier>;
114 fn read_field_end(&mut self) -> crate::thrift::Result<()>;
116 fn read_bool(&mut self) -> crate::thrift::Result<bool>;
118 fn read_bytes(&mut self) -> crate::thrift::Result<Vec<u8>>;
120 fn read_i8(&mut self) -> crate::thrift::Result<i8>;
122 fn read_i16(&mut self) -> crate::thrift::Result<i16>;
124 fn read_i32(&mut self) -> crate::thrift::Result<i32>;
126 fn read_i64(&mut self) -> crate::thrift::Result<i64>;
128 fn read_double(&mut self) -> crate::thrift::Result<f64>;
130 fn read_string(&mut self) -> crate::thrift::Result<String>;
132 fn read_list_begin(&mut self) -> crate::thrift::Result<TListIdentifier>;
134 fn read_list_end(&mut self) -> crate::thrift::Result<()>;
136 fn read_set_begin(&mut self) -> crate::thrift::Result<TSetIdentifier>;
138 fn read_set_end(&mut self) -> crate::thrift::Result<()>;
140 fn read_map_begin(&mut self) -> crate::thrift::Result<TMapIdentifier>;
142 fn read_map_end(&mut self) -> crate::thrift::Result<()>;
144 fn skip(&mut self, field_type: TType) -> crate::thrift::Result<()> {
147 self.skip_till_depth(field_type, MAXIMUM_SKIP_DEPTH)
148 }
149 fn skip_till_depth(&mut self, field_type: TType, depth: i8) -> crate::thrift::Result<()> {
151 if depth == 0 {
152 return Err(crate::thrift::Error::Protocol(ProtocolError {
153 kind: ProtocolErrorKind::DepthLimit,
154 message: format!("cannot parse past {:?}", field_type),
155 }));
156 }
157
158 match field_type {
159 TType::Bool => self.read_bool().map(|_| ()),
160 TType::I08 => self.read_i8().map(|_| ()),
161 TType::I16 => self.read_i16().map(|_| ()),
162 TType::I32 => self.read_i32().map(|_| ()),
163 TType::I64 => self.read_i64().map(|_| ()),
164 TType::Double => self.read_double().map(|_| ()),
165 TType::String => self.read_string().map(|_| ()),
166 TType::Struct => {
167 self.read_struct_begin()?;
168 loop {
169 let field_ident = self.read_field_begin()?;
170 if field_ident.field_type == TType::Stop {
171 break;
172 }
173 self.skip_till_depth(field_ident.field_type, depth - 1)?;
174 }
175 self.read_struct_end()
176 }
177 TType::List => {
178 let list_ident = self.read_list_begin()?;
179 for _ in 0..list_ident.size {
180 self.skip_till_depth(list_ident.element_type, depth - 1)?;
181 }
182 self.read_list_end()
183 }
184 TType::Set => {
185 let set_ident = self.read_set_begin()?;
186 for _ in 0..set_ident.size {
187 self.skip_till_depth(set_ident.element_type, depth - 1)?;
188 }
189 self.read_set_end()
190 }
191 TType::Map => {
192 let map_ident = self.read_map_begin()?;
193 for _ in 0..map_ident.size {
194 let key_type = map_ident
195 .key_type
196 .expect("non-zero sized map should contain key type");
197 let val_type = map_ident
198 .value_type
199 .expect("non-zero sized map should contain value type");
200 self.skip_till_depth(key_type, depth - 1)?;
201 self.skip_till_depth(val_type, depth - 1)?;
202 }
203 self.read_map_end()
204 }
205 u => Err(crate::thrift::Error::Protocol(ProtocolError {
206 kind: ProtocolErrorKind::Unknown,
207 message: format!("cannot skip field type {:?}", &u),
208 })),
209 }
210 }
211
212 fn read_list<P: ReadThrift>(&mut self) -> crate::thrift::Result<Vec<P>> {
213 let list_ident = self.read_list_begin()?;
214 let mut val: Vec<P> = Vec::with_capacity(list_ident.size as usize);
215 for _ in 0..list_ident.size {
216 val.push(P::read_from_in_protocol(self)?);
217 }
218 self.read_list_end()?;
219 Ok(val)
220 }
221
222 fn read_byte(&mut self) -> crate::thrift::Result<u8>;
229}
230
231pub trait TOutputProtocol {
245 fn write_message_begin(
247 &mut self,
248 identifier: &TMessageIdentifier,
249 ) -> crate::thrift::Result<usize>;
250 fn write_message_end(&mut self) -> crate::thrift::Result<usize>;
252 fn write_struct_begin(
254 &mut self,
255 identifier: &TStructIdentifier,
256 ) -> crate::thrift::Result<usize>;
257 fn write_struct_end(&mut self) -> crate::thrift::Result<usize>;
259 fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> crate::thrift::Result<usize>;
261 fn write_field_end(&mut self) -> crate::thrift::Result<usize>;
263 fn write_field_stop(&mut self) -> crate::thrift::Result<usize>;
266 fn write_bool(&mut self, b: bool) -> crate::thrift::Result<usize>;
268 fn write_bytes(&mut self, b: &[u8]) -> crate::thrift::Result<usize>;
270 fn write_i8(&mut self, i: i8) -> crate::thrift::Result<usize>;
272 fn write_i16(&mut self, i: i16) -> crate::thrift::Result<usize>;
274 fn write_i32(&mut self, i: i32) -> crate::thrift::Result<usize>;
276 fn write_i64(&mut self, i: i64) -> crate::thrift::Result<usize>;
278 fn write_double(&mut self, d: f64) -> crate::thrift::Result<usize>;
280 fn write_string(&mut self, s: &str) -> crate::thrift::Result<usize>;
282 fn write_list_begin(&mut self, identifier: &TListIdentifier) -> crate::thrift::Result<usize>;
284 fn write_list_end(&mut self) -> crate::thrift::Result<usize>;
286 fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> crate::thrift::Result<usize>;
288 fn write_set_end(&mut self) -> crate::thrift::Result<usize>;
290 fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> crate::thrift::Result<usize>;
292 fn write_map_end(&mut self) -> crate::thrift::Result<usize>;
294 fn flush(&mut self) -> crate::thrift::Result<()>;
296
297 fn write_byte(&mut self, b: u8) -> crate::thrift::Result<usize>; }
305
306impl<P> TInputProtocol for Box<P>
307where
308 P: TInputProtocol,
309{
310 fn read_message_begin(&mut self) -> crate::thrift::Result<TMessageIdentifier> {
311 (**self).read_message_begin()
312 }
313
314 fn read_message_end(&mut self) -> crate::thrift::Result<()> {
315 (**self).read_message_end()
316 }
317
318 fn read_struct_begin(&mut self) -> crate::thrift::Result<Option<TStructIdentifier>> {
319 (**self).read_struct_begin()
320 }
321
322 fn read_struct_end(&mut self) -> crate::thrift::Result<()> {
323 (**self).read_struct_end()
324 }
325
326 fn read_field_begin(&mut self) -> crate::thrift::Result<TFieldIdentifier> {
327 (**self).read_field_begin()
328 }
329
330 fn read_field_end(&mut self) -> crate::thrift::Result<()> {
331 (**self).read_field_end()
332 }
333
334 fn read_bool(&mut self) -> crate::thrift::Result<bool> {
335 (**self).read_bool()
336 }
337
338 fn read_bytes(&mut self) -> crate::thrift::Result<Vec<u8>> {
339 (**self).read_bytes()
340 }
341
342 fn read_i8(&mut self) -> crate::thrift::Result<i8> {
343 (**self).read_i8()
344 }
345
346 fn read_i16(&mut self) -> crate::thrift::Result<i16> {
347 (**self).read_i16()
348 }
349
350 fn read_i32(&mut self) -> crate::thrift::Result<i32> {
351 (**self).read_i32()
352 }
353
354 fn read_i64(&mut self) -> crate::thrift::Result<i64> {
355 (**self).read_i64()
356 }
357
358 fn read_double(&mut self) -> crate::thrift::Result<f64> {
359 (**self).read_double()
360 }
361
362 fn read_string(&mut self) -> crate::thrift::Result<String> {
363 (**self).read_string()
364 }
365
366 fn read_list_begin(&mut self) -> crate::thrift::Result<TListIdentifier> {
367 (**self).read_list_begin()
368 }
369
370 fn read_list_end(&mut self) -> crate::thrift::Result<()> {
371 (**self).read_list_end()
372 }
373
374 fn read_set_begin(&mut self) -> crate::thrift::Result<TSetIdentifier> {
375 (**self).read_set_begin()
376 }
377
378 fn read_set_end(&mut self) -> crate::thrift::Result<()> {
379 (**self).read_set_end()
380 }
381
382 fn read_map_begin(&mut self) -> crate::thrift::Result<TMapIdentifier> {
383 (**self).read_map_begin()
384 }
385
386 fn read_map_end(&mut self) -> crate::thrift::Result<()> {
387 (**self).read_map_end()
388 }
389
390 fn read_byte(&mut self) -> crate::thrift::Result<u8> {
391 (**self).read_byte()
392 }
393}
394
395impl<P> TOutputProtocol for Box<P>
396where
397 P: TOutputProtocol + ?Sized,
398{
399 fn write_message_begin(
400 &mut self,
401 identifier: &TMessageIdentifier,
402 ) -> crate::thrift::Result<usize> {
403 (**self).write_message_begin(identifier)
404 }
405
406 fn write_message_end(&mut self) -> crate::thrift::Result<usize> {
407 (**self).write_message_end()
408 }
409
410 fn write_struct_begin(
411 &mut self,
412 identifier: &TStructIdentifier,
413 ) -> crate::thrift::Result<usize> {
414 (**self).write_struct_begin(identifier)
415 }
416
417 fn write_struct_end(&mut self) -> crate::thrift::Result<usize> {
418 (**self).write_struct_end()
419 }
420
421 fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> crate::thrift::Result<usize> {
422 (**self).write_field_begin(identifier)
423 }
424
425 fn write_field_end(&mut self) -> crate::thrift::Result<usize> {
426 (**self).write_field_end()
427 }
428
429 fn write_field_stop(&mut self) -> crate::thrift::Result<usize> {
430 (**self).write_field_stop()
431 }
432
433 fn write_bool(&mut self, b: bool) -> crate::thrift::Result<usize> {
434 (**self).write_bool(b)
435 }
436
437 fn write_bytes(&mut self, b: &[u8]) -> crate::thrift::Result<usize> {
438 (**self).write_bytes(b)
439 }
440
441 fn write_i8(&mut self, i: i8) -> crate::thrift::Result<usize> {
442 (**self).write_i8(i)
443 }
444
445 fn write_i16(&mut self, i: i16) -> crate::thrift::Result<usize> {
446 (**self).write_i16(i)
447 }
448
449 fn write_i32(&mut self, i: i32) -> crate::thrift::Result<usize> {
450 (**self).write_i32(i)
451 }
452
453 fn write_i64(&mut self, i: i64) -> crate::thrift::Result<usize> {
454 (**self).write_i64(i)
455 }
456
457 fn write_double(&mut self, d: f64) -> crate::thrift::Result<usize> {
458 (**self).write_double(d)
459 }
460
461 fn write_string(&mut self, s: &str) -> crate::thrift::Result<usize> {
462 (**self).write_string(s)
463 }
464
465 fn write_list_begin(&mut self, identifier: &TListIdentifier) -> crate::thrift::Result<usize> {
466 (**self).write_list_begin(identifier)
467 }
468
469 fn write_list_end(&mut self) -> crate::thrift::Result<usize> {
470 (**self).write_list_end()
471 }
472
473 fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> crate::thrift::Result<usize> {
474 (**self).write_set_begin(identifier)
475 }
476
477 fn write_set_end(&mut self) -> crate::thrift::Result<usize> {
478 (**self).write_set_end()
479 }
480
481 fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> crate::thrift::Result<usize> {
482 (**self).write_map_begin(identifier)
483 }
484
485 fn write_map_end(&mut self) -> crate::thrift::Result<usize> {
486 (**self).write_map_end()
487 }
488
489 fn flush(&mut self) -> crate::thrift::Result<()> {
490 (**self).flush()
491 }
492
493 fn write_byte(&mut self, b: u8) -> crate::thrift::Result<usize> {
494 (**self).write_byte(b)
495 }
496}
497
498#[derive(Clone, Debug, Eq, PartialEq)]
500pub struct TMessageIdentifier {
501 pub name: String,
503 pub message_type: TMessageType,
505 pub sequence_number: u32,
507}
508
509impl TMessageIdentifier {
510 pub fn new<S: Into<String>>(
513 name: S,
514 message_type: TMessageType,
515 sequence_number: u32,
516 ) -> TMessageIdentifier {
517 TMessageIdentifier {
518 name: name.into(),
519 message_type,
520 sequence_number,
521 }
522 }
523}
524
525#[derive(Clone, Debug, Eq, PartialEq)]
527pub struct TStructIdentifier {
528 pub name: String,
530}
531
532impl TStructIdentifier {
533 pub fn new<S: Into<String>>(name: S) -> TStructIdentifier {
535 TStructIdentifier { name: name.into() }
536 }
537}
538
539#[derive(Clone, Debug, Eq, PartialEq)]
541pub struct TFieldIdentifier {
542 pub name: Option<String>,
546 pub field_type: TType,
550 pub id: Option<i16>,
554}
555
556impl TFieldIdentifier {
557 pub fn new<N, S, I>(name: N, field_type: TType, id: I) -> TFieldIdentifier
562 where
563 N: Into<Option<S>>,
564 S: Into<String>,
565 I: Into<Option<i16>>,
566 {
567 TFieldIdentifier {
568 name: name.into().map(|n| n.into()),
569 field_type,
570 id: id.into(),
571 }
572 }
573}
574
575#[derive(Clone, Debug, Eq, PartialEq)]
577pub struct TListIdentifier {
578 pub element_type: TType,
580 pub size: u32,
582}
583
584impl TListIdentifier {
585 pub fn new(element_type: TType, size: u32) -> TListIdentifier {
588 TListIdentifier { element_type, size }
589 }
590}
591
592#[derive(Clone, Debug, Eq, PartialEq)]
594pub struct TSetIdentifier {
595 pub element_type: TType,
597 pub size: u32,
599}
600
601impl TSetIdentifier {
602 pub fn new(element_type: TType, size: u32) -> TSetIdentifier {
605 TSetIdentifier { element_type, size }
606 }
607}
608
609#[derive(Clone, Debug, Eq, PartialEq)]
611pub struct TMapIdentifier {
612 pub key_type: Option<TType>,
614 pub value_type: Option<TType>,
616 pub size: u32,
618}
619
620impl TMapIdentifier {
621 pub fn new<K, V>(key_type: K, value_type: V, size: u32) -> TMapIdentifier
624 where
625 K: Into<Option<TType>>,
626 V: Into<Option<TType>>,
627 {
628 TMapIdentifier {
629 key_type: key_type.into(),
630 value_type: value_type.into(),
631 size,
632 }
633 }
634}
635
636#[derive(Clone, Copy, Debug, Eq, PartialEq)]
638pub enum TMessageType {
639 Call,
641 Reply,
643 Exception,
645 OneWay,
647}
648
649impl Display for TMessageType {
650 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
651 match *self {
652 TMessageType::Call => write!(f, "Call"),
653 TMessageType::Reply => write!(f, "Reply"),
654 TMessageType::Exception => write!(f, "Exception"),
655 TMessageType::OneWay => write!(f, "OneWay"),
656 }
657 }
658}
659
660impl From<TMessageType> for u8 {
661 fn from(message_type: TMessageType) -> Self {
662 match message_type {
663 TMessageType::Call => 0x01,
664 TMessageType::Reply => 0x02,
665 TMessageType::Exception => 0x03,
666 TMessageType::OneWay => 0x04,
667 }
668 }
669}
670
671impl TryFrom<u8> for TMessageType {
672 type Error = Error;
673 fn try_from(b: u8) -> Result<Self, Self::Error> {
674 match b {
675 0x01 => Ok(TMessageType::Call),
676 0x02 => Ok(TMessageType::Reply),
677 0x03 => Ok(TMessageType::Exception),
678 0x04 => Ok(TMessageType::OneWay),
679 unkn => Err(Error::Protocol(ProtocolError {
680 kind: ProtocolErrorKind::InvalidData,
681 message: format!("cannot convert {} to TMessageType", unkn),
682 })),
683 }
684 }
685}
686
687#[derive(Clone, Copy, Debug, Eq, PartialEq)]
689pub enum TType {
690 Stop,
692 Void,
694 Bool,
696 I08,
698 Double,
700 I16,
702 I32,
704 I64,
706 String,
708 Utf7,
710 Struct,
712 Map,
714 Set,
716 List,
718 Utf8,
720 Utf16,
722}
723
724impl Display for TType {
725 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
726 match *self {
727 TType::Stop => write!(f, "STOP"),
728 TType::Void => write!(f, "void"),
729 TType::Bool => write!(f, "bool"),
730 TType::I08 => write!(f, "i08"),
731 TType::Double => write!(f, "double"),
732 TType::I16 => write!(f, "i16"),
733 TType::I32 => write!(f, "i32"),
734 TType::I64 => write!(f, "i64"),
735 TType::String => write!(f, "string"),
736 TType::Utf7 => write!(f, "UTF7"),
737 TType::Struct => write!(f, "struct"),
738 TType::Map => write!(f, "map"),
739 TType::Set => write!(f, "set"),
740 TType::List => write!(f, "list"),
741 TType::Utf8 => write!(f, "UTF8"),
742 TType::Utf16 => write!(f, "UTF16"),
743 }
744 }
745}
746
747pub fn verify_expected_sequence_number(expected: i32, actual: i32) -> crate::thrift::Result<()> {
752 if expected == actual {
753 Ok(())
754 } else {
755 Err(crate::thrift::Error::Application(
756 crate::thrift::ApplicationError {
757 kind: crate::thrift::ApplicationErrorKind::BadSequenceId,
758 message: format!("expected {} got {}", expected, actual),
759 },
760 ))
761 }
762}
763
764pub fn verify_expected_service_call(expected: &str, actual: &str) -> crate::thrift::Result<()> {
769 if expected == actual {
770 Ok(())
771 } else {
772 Err(crate::thrift::Error::Application(ApplicationError {
773 kind: crate::thrift::ApplicationErrorKind::WrongMethodName,
774 message: format!("expected {} got {}", expected, actual),
775 }))
776 }
777}
778
779pub fn verify_expected_message_type(
784 expected: TMessageType,
785 actual: TMessageType,
786) -> crate::thrift::Result<()> {
787 if expected == actual {
788 Ok(())
789 } else {
790 Err(crate::thrift::Error::Application(ApplicationError {
791 kind: crate::thrift::ApplicationErrorKind::InvalidMessageType,
792 message: format!("expected {} got {}", expected, actual),
793 }))
794 }
795}
796
797pub fn verify_required_field_exists<T>(
801 field_name: &str,
802 field: &Option<T>,
803) -> crate::thrift::Result<()> {
804 match *field {
805 Some(_) => Ok(()),
806 None => Err(Error::Protocol(ProtocolError {
807 kind: ProtocolErrorKind::Unknown,
808 message: format!("missing required field {}", field_name),
809 })),
810 }
811}
812
813pub fn field_id(field_ident: &TFieldIdentifier) -> crate::thrift::Result<i16> {
819 field_ident.id.ok_or_else(|| {
820 crate::thrift::Error::Protocol(crate::thrift::ProtocolError {
821 kind: crate::thrift::ProtocolErrorKind::Unknown,
822 message: format!("missing field in in {:?}", field_ident),
823 })
824 })
825}