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
//! This crate provides functionalities for encoding/deconding [Thrift][thrift] protocol.
//!
//! # References
//!
//! - [Thrift Protocol Structure][protocol-structure]
//! - [Thrift Binary protocol encoding][binary-encoding]
//! - [Thrift Compact protocol encoding][compact-encoding]
//!
//! [thrift]: https://thrift.apache.org/
//! [protocol-structure]: https://github.com/apache/thrift/blob/master/doc/specs/thrift-protocol-spec.md
//! [binary-encoding]: https://github.com/apache/thrift/blob/master/doc/specs/thrift-binary-protocol.md
//! [compact-encoding]: https://github.com/apache/thrift/blob/master/doc/specs/thrift-compact-protocol.md
//!
//! # Examples
//!
//! Encodes a message:
//!
//! ```
//! use thrift_codec::CompactEncode;
//! use thrift_codec::data::Struct;
//! use thrift_codec::message::Message;
//!
//! let message = Message::oneway("foo_method", 1, Struct::from(("arg1", 2)));
//! let mut buf = Vec::new();
//! message.compact_encode(&mut buf).unwrap();
//! assert_eq!(
//!     buf,
//!     [130, 129, 1, 10, 102, 111, 111, 95, 109, 101, 116,
//!     104, 111, 100, 24, 4, 97, 114, 103, 49, 21, 4, 0]
//! );
//! ```
//!
//! Decodes the above binary:
//!
//! ```
//! use thrift_codec::CompactDecode;
//! use thrift_codec::data::Struct;
//! use thrift_codec::message::Message;
//!
//! let bytes = [
//!     130, 129, 1, 10, 102, 111, 111, 95, 109, 101, 116,
//!     104, 111, 100, 24, 4, 97, 114, 103, 49, 21, 4, 0
//! ];
//!
//! let message = Message::compact_decode(&mut &bytes[..]).unwrap();
//! let expected = Message::oneway("foo_method", 1, Struct::from(("arg1", 2)));
//! assert_eq!(message, expected);
//! ```
//!
#![warn(missing_docs)]
#[macro_use]
extern crate trackable;
#[cfg(feature = "serde")]
#[macro_use]
extern crate serde;

macro_rules! track_io {
    ($expr:expr) => {
        track!($expr.map_err(<crate::Error as From<std::io::Error>>::from))
    };
}

pub use decode::{BinaryDecode, CompactDecode};
pub use encode::{BinaryEncode, CompactEncode};
pub use error::{Error, ErrorKind};

pub mod data;
pub mod message;

mod constants;
mod decode;
mod encode;
mod error;
mod zigzag;

/// This crate specific `Result` type.
pub type Result<T> = std::result::Result<T, Error>;

#[cfg(test)]
mod test {
    use super::*;
    use data::Struct;
    use message::Message;

    #[test]
    #[rustfmt::skip]
    fn it_works() {
        let message = Message::oneway("foo_method", 1, Struct::from(("arg1", 2)));
        let mut buf = Vec::new();
        track_try_unwrap!(message.compact_encode(&mut buf));
        assert_eq!(
            buf,
            [130, 129, 1, 10, 102, 111, 111, 95, 109, 101, 116,
             104, 111, 100, 24, 4, 97, 114, 103, 49, 21, 4, 0]
        );

        let decoded = track_try_unwrap!(Message::compact_decode(&mut &buf[..]));
        assert_eq!(decoded, message);
    }
}