use std::cell::RefCell;
use std::fmt;
use std::fmt::{Display, Formatter};
use std::convert::From;
use std::rc::Rc;
use try_from::TryFrom;
use ::{ProtocolError, ProtocolErrorKind};
use ::transport::TTransport;
mod binary;
mod compact;
mod multiplexed;
pub use self::binary::{TBinaryInputProtocol, TBinaryInputProtocolFactory, TBinaryOutputProtocol, TBinaryOutputProtocolFactory};
pub use self::compact::{TCompactInputProtocol, TCompactInputProtocolFactory, TCompactOutputProtocol, TCompactOutputProtocolFactory};
pub use self::multiplexed::{TMultiplexedInputProtocol, TMultiplexedOutputProtocol};
const MAXIMUM_SKIP_DEPTH: i8 = 64;
pub trait TInputProtocol {
fn read_message_begin(&mut self) -> ::Result<TMessageIdentifier>;
fn read_message_end(&mut self) -> ::Result<()>;
fn read_struct_begin(&mut self) -> ::Result<Option<TStructIdentifier>>;
fn read_struct_end(&mut self) -> ::Result<()>;
fn read_field_begin(&mut self) -> ::Result<TFieldIdentifier>;
fn read_field_end(&mut self) -> ::Result<()>;
fn read_bool(&mut self) -> ::Result<bool>;
fn read_bytes(&mut self) -> ::Result<Vec<u8>>;
fn read_i8(&mut self) -> ::Result<i8>;
fn read_i16(&mut self) -> ::Result<i16>;
fn read_i32(&mut self) -> ::Result<i32>;
fn read_i64(&mut self) -> ::Result<i64>;
fn read_double(&mut self) -> ::Result<f64>;
fn read_string(&mut self) -> ::Result<String>;
fn read_list_begin(&mut self) -> ::Result<TListIdentifier>;
fn read_list_end(&mut self) -> ::Result<()>;
fn read_set_begin(&mut self) -> ::Result<TSetIdentifier>;
fn read_set_end(&mut self) -> ::Result<()>;
fn read_map_begin(&mut self) -> ::Result<TMapIdentifier>;
fn read_map_end(&mut self) -> ::Result<()>;
fn skip(&mut self, field_type: TType) -> ::Result<()> {
self.skip_till_depth(field_type, MAXIMUM_SKIP_DEPTH)
}
fn skip_till_depth(&mut self, field_type: TType, remaining_depth: i8) -> ::Result<()> {
if remaining_depth == 0 {
return Err(
::Error::Protocol(
ProtocolError {
kind: ProtocolErrorKind::DepthLimit,
message: 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 => {
try!(self.read_struct_begin());
loop {
let field_ident = try!(self.read_field_begin());
if field_ident.field_type == TType::Stop { break; }
try!(self.skip_till_depth(field_ident.field_type, remaining_depth - 1));
}
self.read_struct_end()
},
TType::List => {
let list_ident = try!(self.read_list_begin());
for _ in 0..list_ident.size {
try!(self.skip_till_depth(list_ident.element_type, remaining_depth - 1));
}
self.read_list_end()
},
TType::Set => {
let set_ident = try!(self.read_set_begin());
for _ in 0..set_ident.size {
try!(self.skip_till_depth(set_ident.element_type, remaining_depth - 1));
}
self.read_set_end()
},
TType::Map => {
let map_ident = try!(self.read_map_begin());
for _ in 0..map_ident.size {
let key_type = map_ident.key_type.expect("non-zero sized map should contain key type");
let val_type = map_ident.value_type.expect("non-zero sized map should contain value type");
try!(self.skip_till_depth(key_type, remaining_depth - 1));
try!(self.skip_till_depth(val_type, remaining_depth - 1));
}
self.read_map_end()
},
u => {
Err(
::Error::Protocol(
ProtocolError {
kind: ProtocolErrorKind::Unknown,
message: format!("cannot skip field type {:?}", &u),
}
)
)
},
}
}
fn read_byte(&mut self) -> ::Result<u8>;
}
pub trait TOutputProtocol {
fn write_message_begin(&mut self, identifier: &TMessageIdentifier) -> ::Result<()>;
fn write_message_end(&mut self) -> ::Result<()>;
fn write_struct_begin(&mut self, identifier: &TStructIdentifier) -> ::Result<()>;
fn write_struct_end(&mut self) -> ::Result<()>;
fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> ::Result<()>;
fn write_field_end(&mut self) -> ::Result<()>;
fn write_field_stop(&mut self) -> ::Result<()>;
fn write_bool(&mut self, b: bool) -> ::Result<()>;
fn write_bytes(&mut self, b: &[u8]) -> ::Result<()>;
fn write_i8(&mut self, i: i8) -> ::Result<()>;
fn write_i16(&mut self, i: i16) -> ::Result<()>;
fn write_i32(&mut self, i: i32) -> ::Result<()>;
fn write_i64(&mut self, i: i64) -> ::Result<()>;
fn write_double(&mut self, d: f64) -> ::Result<()>;
fn write_string(&mut self, s: &str) -> ::Result<()>;
fn write_list_begin(&mut self, identifier: &TListIdentifier) -> ::Result<()>;
fn write_list_end(&mut self) -> ::Result<()>;
fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> ::Result<()>;
fn write_set_end(&mut self) -> ::Result<()>;
fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> ::Result<()>;
fn write_map_end(&mut self) -> ::Result<()>;
fn flush(&mut self) -> ::Result<()>;
fn write_byte(&mut self, b: u8) -> ::Result<()>; }
pub trait TInputProtocolFactory {
fn create(&mut self, transport: Rc<RefCell<Box<TTransport>>>) -> Box<TInputProtocol>;
}
pub trait TOutputProtocolFactory {
fn create(&mut self, transport: Rc<RefCell<Box<TTransport>>>) -> Box<TOutputProtocol>;
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TMessageIdentifier {
pub name: String,
pub message_type: TMessageType,
pub sequence_number: i32,
}
impl TMessageIdentifier {
pub fn new<S: Into<String>>(name: S, message_type: TMessageType, sequence_number: i32) -> TMessageIdentifier {
TMessageIdentifier {
name: name.into(),
message_type: message_type,
sequence_number: sequence_number
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TStructIdentifier {
pub name: String,
}
impl TStructIdentifier {
pub fn new<S: Into<String>>(name: S) -> TStructIdentifier {
TStructIdentifier { name: name.into() }
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TFieldIdentifier {
pub name: Option<String>,
pub field_type: TType,
pub id: Option<i16>,
}
impl TFieldIdentifier {
pub fn new<N, S, I>(name: N, field_type: TType, id: I) -> TFieldIdentifier
where
N: Into<Option<S>>,
S: Into<String>,
I: Into<Option<i16>>
{
TFieldIdentifier {
name: name.into().map(|n| n.into()),
field_type: field_type,
id: id.into()
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TListIdentifier {
pub element_type: TType,
pub size: i32,
}
impl TListIdentifier {
pub fn new(element_type: TType, size: i32) -> TListIdentifier {
TListIdentifier { element_type: element_type, size: size }
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TSetIdentifier {
pub element_type: TType,
pub size: i32,
}
impl TSetIdentifier {
pub fn new(element_type: TType, size: i32) -> TSetIdentifier {
TSetIdentifier { element_type: element_type, size: size }
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TMapIdentifier {
pub key_type: Option<TType>,
pub value_type: Option<TType>,
pub size: i32,
}
impl TMapIdentifier {
pub fn new<K, V>(key_type: K, value_type: V, size: i32) -> TMapIdentifier
where
K: Into<Option<TType>>,
V: Into<Option<TType>>
{
TMapIdentifier { key_type: key_type.into(), value_type: value_type.into(), size: size }
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TMessageType {
Call,
Reply,
Exception,
OneWay,
}
impl Display for TMessageType {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
TMessageType::Call => write!(f, "Call"),
TMessageType::Reply => write!(f, "Reply"),
TMessageType::Exception => write!(f, "Exception"),
TMessageType::OneWay => write!(f, "OneWay"),
}
}
}
impl From<TMessageType> for u8 {
fn from(message_type: TMessageType) -> Self {
match message_type {
TMessageType::Call => 0x01,
TMessageType::Reply => 0x02,
TMessageType::Exception => 0x03,
TMessageType::OneWay => 0x04,
}
}
}
impl TryFrom<u8> for TMessageType {
type Err = ::Error;
fn try_from(b: u8) -> ::Result<Self> {
match b {
0x01 => Ok(TMessageType::Call),
0x02 => Ok(TMessageType::Reply),
0x03 => Ok(TMessageType::Exception),
0x04 => Ok(TMessageType::OneWay),
unkn => Err(
::Error::Protocol(
ProtocolError {
kind: ProtocolErrorKind::InvalidData,
message: format!("cannot convert {} to TMessageType", unkn),
}
)
)
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TType {
Stop,
Void,
Bool,
I08,
Double,
I16,
I32,
I64,
String,
Utf7,
Struct,
Map,
Set,
List,
Utf8,
Utf16,
}
impl Display for TType {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
TType::Stop => write!(f, "STOP"),
TType::Void => write!(f, "void"),
TType::Bool => write!(f, "bool"),
TType::I08 => write!(f, "i08"),
TType::Double => write!(f, "double"),
TType::I16 => write!(f, "i16"),
TType::I32 => write!(f, "i32"),
TType::I64 => write!(f, "i64"),
TType::String => write!(f, "string"),
TType::Utf7 => write!(f, "UTF7"),
TType::Struct => write!(f, "struct"),
TType::Map => write!(f, "map"),
TType::Set => write!(f, "set"),
TType::List => write!(f, "list"),
TType::Utf8 => write!(f, "UTF8"),
TType::Utf16 => write!(f, "UTF16"),
}
}
}
pub fn verify_expected_sequence_number(expected: i32, actual: i32) -> ::Result<()> {
if expected == actual {
Ok(())
} else {
Err(
::Error::Application(
::ApplicationError {
kind: ::ApplicationErrorKind::BadSequenceId,
message: format!("expected {} got {}", expected, actual)
}
)
)
}
}
pub fn verify_expected_service_call(expected: &str, actual: &str) -> ::Result<()> {
if expected == actual {
Ok(())
} else {
Err(
::Error::Application(
::ApplicationError {
kind: ::ApplicationErrorKind::WrongMethodName,
message: format!("expected {} got {}", expected, actual)
}
)
)
}
}
pub fn verify_expected_message_type(expected: TMessageType, actual: TMessageType) -> ::Result<()> {
if expected == actual {
Ok(())
} else {
Err(
::Error::Application(
::ApplicationError {
kind: ::ApplicationErrorKind::InvalidMessageType,
message: format!("expected {} got {}", expected, actual)
}
)
)
}
}
pub fn verify_required_field_exists<T>(field_name: &str, field: &Option<T>) -> ::Result<()> {
match *field {
Some(_) => Ok(()),
None => Err(
::Error::Protocol(
::ProtocolError {
kind: ::ProtocolErrorKind::Unknown,
message: format!("missing required field {}", field_name)
}
)
),
}
}
pub fn field_id(field_ident: &TFieldIdentifier) -> ::Result<i16> {
field_ident.id.ok_or(
::Error::Protocol(
::ProtocolError {
kind: ::ProtocolErrorKind::Unknown,
message: format!("missing field in in {:?}", field_ident)
}
)
)
}