blob: 019f7172128c9afea02e7a8e3056b2ad3c6e493e [file] [log] [blame]
Allen George8b96bfb2016-11-02 08:01:08 -04001// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Types used to send and receive primitives between a Thrift client and server.
19//!
20//! # Examples
21//!
Allen George8b96bfb2016-11-02 08:01:08 -040022//! Create and use a `TInputProtocol`.
23//!
24//! ```no_run
Allen George8b96bfb2016-11-02 08:01:08 -040025//! use thrift::protocol::{TBinaryInputProtocol, TInputProtocol};
Allen George0e22c362017-01-30 07:15:00 -050026//! use thrift::transport::TTcpChannel;
Allen George8b96bfb2016-11-02 08:01:08 -040027//!
28//! // create the I/O channel
Allen George0e22c362017-01-30 07:15:00 -050029//! let mut channel = TTcpChannel::new();
30//! channel.open("127.0.0.1:9090").unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -040031//!
32//! // create the protocol to decode bytes into types
Allen George0e22c362017-01-30 07:15:00 -050033//! let mut protocol = TBinaryInputProtocol::new(channel, true);
Allen George8b96bfb2016-11-02 08:01:08 -040034//!
35//! // read types from the wire
Allen George0e22c362017-01-30 07:15:00 -050036//! let field_identifier = protocol.read_field_begin().unwrap();
37//! let field_contents = protocol.read_string().unwrap();
38//! let field_end = protocol.read_field_end().unwrap();
39//! ```
40//!
41//! Create and use a `TOutputProtocol`.
42//!
43//! ```no_run
44//! use thrift::protocol::{TBinaryOutputProtocol, TFieldIdentifier, TOutputProtocol, TType};
45//! use thrift::transport::TTcpChannel;
46//!
47//! // create the I/O channel
48//! let mut channel = TTcpChannel::new();
49//! channel.open("127.0.0.1:9090").unwrap();
50//!
51//! // create the protocol to encode types into bytes
52//! let mut protocol = TBinaryOutputProtocol::new(channel, true);
53//!
54//! // write types
55//! protocol.write_field_begin(&TFieldIdentifier::new("string_thing", TType::String, 1)).unwrap();
56//! protocol.write_string("foo").unwrap();
57//! protocol.write_field_end().unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -040058//! ```
59
Danny Browningddec4312019-03-08 14:20:41 -070060use std::convert::{From, TryFrom};
Allen George8b96bfb2016-11-02 08:01:08 -040061use std::fmt;
62use std::fmt::{Display, Formatter};
Allen George8b96bfb2016-11-02 08:01:08 -040063
Allen Georgeb0d14132020-03-29 11:48:55 -040064use crate::transport::{TReadTransport, TWriteTransport};
65use crate::{ProtocolError, ProtocolErrorKind};
Allen George0e22c362017-01-30 07:15:00 -050066
67#[cfg(test)]
68macro_rules! assert_eq_written_bytes {
Allen Georgeef7a1892018-12-16 18:01:37 -050069 ($o_prot:ident, $expected_bytes:ident) => {{
70 assert_eq!($o_prot.transport.write_bytes(), &$expected_bytes);
71 }};
Allen George0e22c362017-01-30 07:15:00 -050072}
73
74// FIXME: should take both read and write
75#[cfg(test)]
76macro_rules! copy_write_buffer_to_read_buffer {
Allen Georgeef7a1892018-12-16 18:01:37 -050077 ($o_prot:ident) => {{
78 $o_prot.transport.copy_write_buffer_to_read_buffer();
79 }};
Allen George0e22c362017-01-30 07:15:00 -050080}
81
82#[cfg(test)]
83macro_rules! set_readable_bytes {
84 ($i_prot:ident, $bytes:expr) => {
85 $i_prot.transport.set_readable_bytes($bytes);
Allen Georgeef7a1892018-12-16 18:01:37 -050086 };
Allen George0e22c362017-01-30 07:15:00 -050087}
Allen George8b96bfb2016-11-02 08:01:08 -040088
89mod binary;
90mod compact;
91mod multiplexed;
92mod stored;
93
Allen Georgeef7a1892018-12-16 18:01:37 -050094pub use self::binary::{
95 TBinaryInputProtocol, TBinaryInputProtocolFactory, TBinaryOutputProtocol,
96 TBinaryOutputProtocolFactory,
97};
98pub use self::compact::{
99 TCompactInputProtocol, TCompactInputProtocolFactory, TCompactOutputProtocol,
100 TCompactOutputProtocolFactory,
101};
Allen George8b96bfb2016-11-02 08:01:08 -0400102pub use self::multiplexed::TMultiplexedOutputProtocol;
103pub use self::stored::TStoredInputProtocol;
104
Zachary Kuhn2e0a8052022-04-10 11:31:28 -0400105/// Reads and writes the struct to Thrift protocols.
106///
107/// It is implemented in generated code for Thrift `struct`, `union`, and `enum` types.
108pub trait TSerializable: Sized {
109 fn read_from_in_protocol(i_prot: &mut dyn TInputProtocol) -> crate::Result<Self>;
110 fn write_to_out_protocol(&self, o_prot: &mut dyn TOutputProtocol) -> crate::Result<()>;
111}
112
Allen George8b96bfb2016-11-02 08:01:08 -0400113// Default maximum depth to which `TInputProtocol::skip` will skip a Thrift
114// field. A default is necessary because Thrift structs or collections may
115// contain nested structs and collections, which could result in indefinite
116// recursion.
117const MAXIMUM_SKIP_DEPTH: i8 = 64;
118
119/// Converts a stream of bytes into Thrift identifiers, primitives,
120/// containers, or structs.
121///
122/// This trait does not deal with higher-level Thrift concepts like structs or
123/// exceptions - only with primitives and message or container boundaries. Once
124/// bytes are read they are deserialized and an identifier (for example
125/// `TMessageIdentifier`) or a primitive is returned.
126///
127/// All methods return a `thrift::Result`. If an `Err` is returned the protocol
128/// instance and its underlying transport should be terminated.
129///
130/// # Examples
131///
132/// Create and use a `TInputProtocol`
133///
134/// ```no_run
Allen George8b96bfb2016-11-02 08:01:08 -0400135/// use thrift::protocol::{TBinaryInputProtocol, TInputProtocol};
Allen George0e22c362017-01-30 07:15:00 -0500136/// use thrift::transport::TTcpChannel;
Allen George8b96bfb2016-11-02 08:01:08 -0400137///
Allen George0e22c362017-01-30 07:15:00 -0500138/// let mut channel = TTcpChannel::new();
139/// channel.open("127.0.0.1:9090").unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -0400140///
Allen George0e22c362017-01-30 07:15:00 -0500141/// let mut protocol = TBinaryInputProtocol::new(channel, true);
Allen George8b96bfb2016-11-02 08:01:08 -0400142///
Allen George0e22c362017-01-30 07:15:00 -0500143/// let field_identifier = protocol.read_field_begin().unwrap();
144/// let field_contents = protocol.read_string().unwrap();
145/// let field_end = protocol.read_field_end().unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -0400146/// ```
147pub trait TInputProtocol {
148 /// Read the beginning of a Thrift message.
Allen Georgeb0d14132020-03-29 11:48:55 -0400149 fn read_message_begin(&mut self) -> crate::Result<TMessageIdentifier>;
Allen George8b96bfb2016-11-02 08:01:08 -0400150 /// Read the end of a Thrift message.
Allen Georgeb0d14132020-03-29 11:48:55 -0400151 fn read_message_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400152 /// Read the beginning of a Thrift struct.
Allen Georgeb0d14132020-03-29 11:48:55 -0400153 fn read_struct_begin(&mut self) -> crate::Result<Option<TStructIdentifier>>;
Allen George8b96bfb2016-11-02 08:01:08 -0400154 /// Read the end of a Thrift struct.
Allen Georgeb0d14132020-03-29 11:48:55 -0400155 fn read_struct_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400156 /// Read the beginning of a Thrift struct field.
Allen Georgeb0d14132020-03-29 11:48:55 -0400157 fn read_field_begin(&mut self) -> crate::Result<TFieldIdentifier>;
Allen George8b96bfb2016-11-02 08:01:08 -0400158 /// Read the end of a Thrift struct field.
Allen Georgeb0d14132020-03-29 11:48:55 -0400159 fn read_field_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400160 /// Read a bool.
Allen Georgeb0d14132020-03-29 11:48:55 -0400161 fn read_bool(&mut self) -> crate::Result<bool>;
Allen George8b96bfb2016-11-02 08:01:08 -0400162 /// Read a fixed-length byte array.
Allen Georgeb0d14132020-03-29 11:48:55 -0400163 fn read_bytes(&mut self) -> crate::Result<Vec<u8>>;
Allen George8b96bfb2016-11-02 08:01:08 -0400164 /// Read a word.
Allen Georgeb0d14132020-03-29 11:48:55 -0400165 fn read_i8(&mut self) -> crate::Result<i8>;
Allen George8b96bfb2016-11-02 08:01:08 -0400166 /// Read a 16-bit signed integer.
Allen Georgeb0d14132020-03-29 11:48:55 -0400167 fn read_i16(&mut self) -> crate::Result<i16>;
Allen George8b96bfb2016-11-02 08:01:08 -0400168 /// Read a 32-bit signed integer.
Allen Georgeb0d14132020-03-29 11:48:55 -0400169 fn read_i32(&mut self) -> crate::Result<i32>;
Allen George8b96bfb2016-11-02 08:01:08 -0400170 /// Read a 64-bit signed integer.
Allen Georgeb0d14132020-03-29 11:48:55 -0400171 fn read_i64(&mut self) -> crate::Result<i64>;
Allen George8b96bfb2016-11-02 08:01:08 -0400172 /// Read a 64-bit float.
Allen Georgeb0d14132020-03-29 11:48:55 -0400173 fn read_double(&mut self) -> crate::Result<f64>;
Allen George8b96bfb2016-11-02 08:01:08 -0400174 /// Read a fixed-length string (not null terminated).
Allen Georgeb0d14132020-03-29 11:48:55 -0400175 fn read_string(&mut self) -> crate::Result<String>;
Allen George8b96bfb2016-11-02 08:01:08 -0400176 /// Read the beginning of a list.
Allen Georgeb0d14132020-03-29 11:48:55 -0400177 fn read_list_begin(&mut self) -> crate::Result<TListIdentifier>;
Allen George8b96bfb2016-11-02 08:01:08 -0400178 /// Read the end of a list.
Allen Georgeb0d14132020-03-29 11:48:55 -0400179 fn read_list_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400180 /// Read the beginning of a set.
Allen Georgeb0d14132020-03-29 11:48:55 -0400181 fn read_set_begin(&mut self) -> crate::Result<TSetIdentifier>;
Allen George8b96bfb2016-11-02 08:01:08 -0400182 /// Read the end of a set.
Allen Georgeb0d14132020-03-29 11:48:55 -0400183 fn read_set_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400184 /// Read the beginning of a map.
Allen Georgeb0d14132020-03-29 11:48:55 -0400185 fn read_map_begin(&mut self) -> crate::Result<TMapIdentifier>;
Allen George8b96bfb2016-11-02 08:01:08 -0400186 /// Read the end of a map.
Allen Georgeb0d14132020-03-29 11:48:55 -0400187 fn read_map_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400188 /// Skip a field with type `field_type` recursively until the default
189 /// maximum skip depth is reached.
Allen Georgeb0d14132020-03-29 11:48:55 -0400190 fn skip(&mut self, field_type: TType) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400191 self.skip_till_depth(field_type, MAXIMUM_SKIP_DEPTH)
192 }
193 /// Skip a field with type `field_type` recursively up to `depth` levels.
Allen Georgeb0d14132020-03-29 11:48:55 -0400194 fn skip_till_depth(&mut self, field_type: TType, depth: i8) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400195 if depth == 0 {
Allen Georgeb0d14132020-03-29 11:48:55 -0400196 return Err(crate::Error::Protocol(ProtocolError {
Allen Georgeef7a1892018-12-16 18:01:37 -0500197 kind: ProtocolErrorKind::DepthLimit,
198 message: format!("cannot parse past {:?}", field_type),
199 }));
Allen George8b96bfb2016-11-02 08:01:08 -0400200 }
201
202 match field_type {
203 TType::Bool => self.read_bool().map(|_| ()),
204 TType::I08 => self.read_i8().map(|_| ()),
205 TType::I16 => self.read_i16().map(|_| ()),
206 TType::I32 => self.read_i32().map(|_| ()),
207 TType::I64 => self.read_i64().map(|_| ()),
208 TType::Double => self.read_double().map(|_| ()),
209 TType::String => self.read_string().map(|_| ()),
210 TType::Struct => {
211 self.read_struct_begin()?;
212 loop {
213 let field_ident = self.read_field_begin()?;
214 if field_ident.field_type == TType::Stop {
215 break;
216 }
217 self.skip_till_depth(field_ident.field_type, depth - 1)?;
218 }
219 self.read_struct_end()
220 }
221 TType::List => {
222 let list_ident = self.read_list_begin()?;
223 for _ in 0..list_ident.size {
224 self.skip_till_depth(list_ident.element_type, depth - 1)?;
225 }
226 self.read_list_end()
227 }
228 TType::Set => {
229 let set_ident = self.read_set_begin()?;
230 for _ in 0..set_ident.size {
231 self.skip_till_depth(set_ident.element_type, depth - 1)?;
232 }
233 self.read_set_end()
234 }
235 TType::Map => {
236 let map_ident = self.read_map_begin()?;
237 for _ in 0..map_ident.size {
Allen George0e22c362017-01-30 07:15:00 -0500238 let key_type = map_ident
239 .key_type
Allen George8b96bfb2016-11-02 08:01:08 -0400240 .expect("non-zero sized map should contain key type");
Allen George0e22c362017-01-30 07:15:00 -0500241 let val_type = map_ident
242 .value_type
Allen George8b96bfb2016-11-02 08:01:08 -0400243 .expect("non-zero sized map should contain value type");
244 self.skip_till_depth(key_type, depth - 1)?;
245 self.skip_till_depth(val_type, depth - 1)?;
246 }
247 self.read_map_end()
248 }
Allen Georgeb0d14132020-03-29 11:48:55 -0400249 u => Err(crate::Error::Protocol(ProtocolError {
Allen Georgeef7a1892018-12-16 18:01:37 -0500250 kind: ProtocolErrorKind::Unknown,
251 message: format!("cannot skip field type {:?}", &u),
252 })),
Allen George8b96bfb2016-11-02 08:01:08 -0400253 }
254 }
255
256 // utility (DO NOT USE IN GENERATED CODE!!!!)
257 //
258
259 /// Read an unsigned byte.
260 ///
261 /// This method should **never** be used in generated code.
Allen Georgeb0d14132020-03-29 11:48:55 -0400262 fn read_byte(&mut self) -> crate::Result<u8>;
Allen George8b96bfb2016-11-02 08:01:08 -0400263}
264
265/// Converts Thrift identifiers, primitives, containers or structs into a
266/// stream of bytes.
267///
268/// This trait does not deal with higher-level Thrift concepts like structs or
269/// exceptions - only with primitives and message or container boundaries.
270/// Write methods take an identifier (for example, `TMessageIdentifier`) or a
271/// primitive. Any or all of the fields in an identifier may be omitted when
272/// writing to the transport. Write methods may even be noops. All of this is
273/// transparent to the caller; as long as a matching `TInputProtocol`
274/// implementation is used, received messages will be decoded correctly.
275///
276/// All methods return a `thrift::Result`. If an `Err` is returned the protocol
277/// instance and its underlying transport should be terminated.
278///
279/// # Examples
280///
281/// Create and use a `TOutputProtocol`
282///
283/// ```no_run
Allen George8b96bfb2016-11-02 08:01:08 -0400284/// use thrift::protocol::{TBinaryOutputProtocol, TFieldIdentifier, TOutputProtocol, TType};
Allen George0e22c362017-01-30 07:15:00 -0500285/// use thrift::transport::TTcpChannel;
Allen George8b96bfb2016-11-02 08:01:08 -0400286///
Allen George0e22c362017-01-30 07:15:00 -0500287/// let mut channel = TTcpChannel::new();
288/// channel.open("127.0.0.1:9090").unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -0400289///
Allen George0e22c362017-01-30 07:15:00 -0500290/// let mut protocol = TBinaryOutputProtocol::new(channel, true);
Allen George8b96bfb2016-11-02 08:01:08 -0400291///
Allen George0e22c362017-01-30 07:15:00 -0500292/// protocol.write_field_begin(&TFieldIdentifier::new("string_thing", TType::String, 1)).unwrap();
293/// protocol.write_string("foo").unwrap();
294/// protocol.write_field_end().unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -0400295/// ```
296pub trait TOutputProtocol {
297 /// Write the beginning of a Thrift message.
Allen Georgeb0d14132020-03-29 11:48:55 -0400298 fn write_message_begin(&mut self, identifier: &TMessageIdentifier) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400299 /// Write the end of a Thrift message.
Allen Georgeb0d14132020-03-29 11:48:55 -0400300 fn write_message_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400301 /// Write the beginning of a Thrift struct.
Allen Georgeb0d14132020-03-29 11:48:55 -0400302 fn write_struct_begin(&mut self, identifier: &TStructIdentifier) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400303 /// Write the end of a Thrift struct.
Allen Georgeb0d14132020-03-29 11:48:55 -0400304 fn write_struct_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400305 /// Write the beginning of a Thrift field.
Allen Georgeb0d14132020-03-29 11:48:55 -0400306 fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400307 /// Write the end of a Thrift field.
Allen Georgeb0d14132020-03-29 11:48:55 -0400308 fn write_field_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400309 /// Write a STOP field indicating that all the fields in a struct have been
310 /// written.
Allen Georgeb0d14132020-03-29 11:48:55 -0400311 fn write_field_stop(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400312 /// Write a bool.
Allen Georgeb0d14132020-03-29 11:48:55 -0400313 fn write_bool(&mut self, b: bool) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400314 /// Write a fixed-length byte array.
Allen Georgeb0d14132020-03-29 11:48:55 -0400315 fn write_bytes(&mut self, b: &[u8]) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400316 /// Write an 8-bit signed integer.
Allen Georgeb0d14132020-03-29 11:48:55 -0400317 fn write_i8(&mut self, i: i8) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400318 /// Write a 16-bit signed integer.
Allen Georgeb0d14132020-03-29 11:48:55 -0400319 fn write_i16(&mut self, i: i16) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400320 /// Write a 32-bit signed integer.
Allen Georgeb0d14132020-03-29 11:48:55 -0400321 fn write_i32(&mut self, i: i32) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400322 /// Write a 64-bit signed integer.
Allen Georgeb0d14132020-03-29 11:48:55 -0400323 fn write_i64(&mut self, i: i64) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400324 /// Write a 64-bit float.
Allen Georgeb0d14132020-03-29 11:48:55 -0400325 fn write_double(&mut self, d: f64) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400326 /// Write a fixed-length string.
Allen Georgeb0d14132020-03-29 11:48:55 -0400327 fn write_string(&mut self, s: &str) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400328 /// Write the beginning of a list.
Allen Georgeb0d14132020-03-29 11:48:55 -0400329 fn write_list_begin(&mut self, identifier: &TListIdentifier) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400330 /// Write the end of a list.
Allen Georgeb0d14132020-03-29 11:48:55 -0400331 fn write_list_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400332 /// Write the beginning of a set.
Allen Georgeb0d14132020-03-29 11:48:55 -0400333 fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400334 /// Write the end of a set.
Allen Georgeb0d14132020-03-29 11:48:55 -0400335 fn write_set_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400336 /// Write the beginning of a map.
Allen Georgeb0d14132020-03-29 11:48:55 -0400337 fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400338 /// Write the end of a map.
Allen Georgeb0d14132020-03-29 11:48:55 -0400339 fn write_map_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400340 /// Flush buffered bytes to the underlying transport.
Allen Georgeb0d14132020-03-29 11:48:55 -0400341 fn flush(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400342
343 // utility (DO NOT USE IN GENERATED CODE!!!!)
344 //
345
346 /// Write an unsigned byte.
347 ///
348 /// This method should **never** be used in generated code.
Allen Georgeb0d14132020-03-29 11:48:55 -0400349 fn write_byte(&mut self, b: u8) -> crate::Result<()>; // FIXME: REMOVE
Allen George8b96bfb2016-11-02 08:01:08 -0400350}
351
Allen George0e22c362017-01-30 07:15:00 -0500352impl<P> TInputProtocol for Box<P>
353where
354 P: TInputProtocol + ?Sized,
355{
Allen Georgeb0d14132020-03-29 11:48:55 -0400356 fn read_message_begin(&mut self) -> crate::Result<TMessageIdentifier> {
Allen George0e22c362017-01-30 07:15:00 -0500357 (**self).read_message_begin()
358 }
359
Allen Georgeb0d14132020-03-29 11:48:55 -0400360 fn read_message_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500361 (**self).read_message_end()
362 }
363
Allen Georgeb0d14132020-03-29 11:48:55 -0400364 fn read_struct_begin(&mut self) -> crate::Result<Option<TStructIdentifier>> {
Allen George0e22c362017-01-30 07:15:00 -0500365 (**self).read_struct_begin()
366 }
367
Allen Georgeb0d14132020-03-29 11:48:55 -0400368 fn read_struct_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500369 (**self).read_struct_end()
370 }
371
Allen Georgeb0d14132020-03-29 11:48:55 -0400372 fn read_field_begin(&mut self) -> crate::Result<TFieldIdentifier> {
Allen George0e22c362017-01-30 07:15:00 -0500373 (**self).read_field_begin()
374 }
375
Allen Georgeb0d14132020-03-29 11:48:55 -0400376 fn read_field_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500377 (**self).read_field_end()
378 }
379
Allen Georgeb0d14132020-03-29 11:48:55 -0400380 fn read_bool(&mut self) -> crate::Result<bool> {
Allen George0e22c362017-01-30 07:15:00 -0500381 (**self).read_bool()
382 }
383
Allen Georgeb0d14132020-03-29 11:48:55 -0400384 fn read_bytes(&mut self) -> crate::Result<Vec<u8>> {
Allen George0e22c362017-01-30 07:15:00 -0500385 (**self).read_bytes()
386 }
387
Allen Georgeb0d14132020-03-29 11:48:55 -0400388 fn read_i8(&mut self) -> crate::Result<i8> {
Allen George0e22c362017-01-30 07:15:00 -0500389 (**self).read_i8()
390 }
391
Allen Georgeb0d14132020-03-29 11:48:55 -0400392 fn read_i16(&mut self) -> crate::Result<i16> {
Allen George0e22c362017-01-30 07:15:00 -0500393 (**self).read_i16()
394 }
395
Allen Georgeb0d14132020-03-29 11:48:55 -0400396 fn read_i32(&mut self) -> crate::Result<i32> {
Allen George0e22c362017-01-30 07:15:00 -0500397 (**self).read_i32()
398 }
399
Allen Georgeb0d14132020-03-29 11:48:55 -0400400 fn read_i64(&mut self) -> crate::Result<i64> {
Allen George0e22c362017-01-30 07:15:00 -0500401 (**self).read_i64()
402 }
403
Allen Georgeb0d14132020-03-29 11:48:55 -0400404 fn read_double(&mut self) -> crate::Result<f64> {
Allen George0e22c362017-01-30 07:15:00 -0500405 (**self).read_double()
406 }
407
Allen Georgeb0d14132020-03-29 11:48:55 -0400408 fn read_string(&mut self) -> crate::Result<String> {
Allen George0e22c362017-01-30 07:15:00 -0500409 (**self).read_string()
410 }
411
Allen Georgeb0d14132020-03-29 11:48:55 -0400412 fn read_list_begin(&mut self) -> crate::Result<TListIdentifier> {
Allen George0e22c362017-01-30 07:15:00 -0500413 (**self).read_list_begin()
414 }
415
Allen Georgeb0d14132020-03-29 11:48:55 -0400416 fn read_list_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500417 (**self).read_list_end()
418 }
419
Allen Georgeb0d14132020-03-29 11:48:55 -0400420 fn read_set_begin(&mut self) -> crate::Result<TSetIdentifier> {
Allen George0e22c362017-01-30 07:15:00 -0500421 (**self).read_set_begin()
422 }
423
Allen Georgeb0d14132020-03-29 11:48:55 -0400424 fn read_set_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500425 (**self).read_set_end()
426 }
427
Allen Georgeb0d14132020-03-29 11:48:55 -0400428 fn read_map_begin(&mut self) -> crate::Result<TMapIdentifier> {
Allen George0e22c362017-01-30 07:15:00 -0500429 (**self).read_map_begin()
430 }
431
Allen Georgeb0d14132020-03-29 11:48:55 -0400432 fn read_map_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500433 (**self).read_map_end()
434 }
435
Allen Georgeb0d14132020-03-29 11:48:55 -0400436 fn read_byte(&mut self) -> crate::Result<u8> {
Allen George0e22c362017-01-30 07:15:00 -0500437 (**self).read_byte()
438 }
439}
440
441impl<P> TOutputProtocol for Box<P>
442where
443 P: TOutputProtocol + ?Sized,
444{
Allen Georgeb0d14132020-03-29 11:48:55 -0400445 fn write_message_begin(&mut self, identifier: &TMessageIdentifier) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500446 (**self).write_message_begin(identifier)
447 }
448
Allen Georgeb0d14132020-03-29 11:48:55 -0400449 fn write_message_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500450 (**self).write_message_end()
451 }
452
Allen Georgeb0d14132020-03-29 11:48:55 -0400453 fn write_struct_begin(&mut self, identifier: &TStructIdentifier) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500454 (**self).write_struct_begin(identifier)
455 }
456
Allen Georgeb0d14132020-03-29 11:48:55 -0400457 fn write_struct_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500458 (**self).write_struct_end()
459 }
460
Allen Georgeb0d14132020-03-29 11:48:55 -0400461 fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500462 (**self).write_field_begin(identifier)
463 }
464
Allen Georgeb0d14132020-03-29 11:48:55 -0400465 fn write_field_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500466 (**self).write_field_end()
467 }
468
Allen Georgeb0d14132020-03-29 11:48:55 -0400469 fn write_field_stop(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500470 (**self).write_field_stop()
471 }
472
Allen Georgeb0d14132020-03-29 11:48:55 -0400473 fn write_bool(&mut self, b: bool) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500474 (**self).write_bool(b)
475 }
476
Allen Georgeb0d14132020-03-29 11:48:55 -0400477 fn write_bytes(&mut self, b: &[u8]) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500478 (**self).write_bytes(b)
479 }
480
Allen Georgeb0d14132020-03-29 11:48:55 -0400481 fn write_i8(&mut self, i: i8) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500482 (**self).write_i8(i)
483 }
484
Allen Georgeb0d14132020-03-29 11:48:55 -0400485 fn write_i16(&mut self, i: i16) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500486 (**self).write_i16(i)
487 }
488
Allen Georgeb0d14132020-03-29 11:48:55 -0400489 fn write_i32(&mut self, i: i32) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500490 (**self).write_i32(i)
491 }
492
Allen Georgeb0d14132020-03-29 11:48:55 -0400493 fn write_i64(&mut self, i: i64) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500494 (**self).write_i64(i)
495 }
496
Allen Georgeb0d14132020-03-29 11:48:55 -0400497 fn write_double(&mut self, d: f64) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500498 (**self).write_double(d)
499 }
500
Allen Georgeb0d14132020-03-29 11:48:55 -0400501 fn write_string(&mut self, s: &str) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500502 (**self).write_string(s)
503 }
504
Allen Georgeb0d14132020-03-29 11:48:55 -0400505 fn write_list_begin(&mut self, identifier: &TListIdentifier) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500506 (**self).write_list_begin(identifier)
507 }
508
Allen Georgeb0d14132020-03-29 11:48:55 -0400509 fn write_list_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500510 (**self).write_list_end()
511 }
512
Allen Georgeb0d14132020-03-29 11:48:55 -0400513 fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500514 (**self).write_set_begin(identifier)
515 }
516
Allen Georgeb0d14132020-03-29 11:48:55 -0400517 fn write_set_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500518 (**self).write_set_end()
519 }
520
Allen Georgeb0d14132020-03-29 11:48:55 -0400521 fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500522 (**self).write_map_begin(identifier)
523 }
524
Allen Georgeb0d14132020-03-29 11:48:55 -0400525 fn write_map_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500526 (**self).write_map_end()
527 }
528
Allen Georgeb0d14132020-03-29 11:48:55 -0400529 fn flush(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500530 (**self).flush()
531 }
532
Allen Georgeb0d14132020-03-29 11:48:55 -0400533 fn write_byte(&mut self, b: u8) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500534 (**self).write_byte(b)
535 }
536}
537
Allen George8b96bfb2016-11-02 08:01:08 -0400538/// Helper type used by servers to create `TInputProtocol` instances for
539/// accepted client connections.
540///
541/// # Examples
542///
543/// Create a `TInputProtocolFactory` and use it to create a `TInputProtocol`.
544///
545/// ```no_run
Allen George8b96bfb2016-11-02 08:01:08 -0400546/// use thrift::protocol::{TBinaryInputProtocolFactory, TInputProtocolFactory};
Allen George0e22c362017-01-30 07:15:00 -0500547/// use thrift::transport::TTcpChannel;
Allen George8b96bfb2016-11-02 08:01:08 -0400548///
Allen George0e22c362017-01-30 07:15:00 -0500549/// let mut channel = TTcpChannel::new();
550/// channel.open("127.0.0.1:9090").unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -0400551///
Allen George0e22c362017-01-30 07:15:00 -0500552/// let factory = TBinaryInputProtocolFactory::new();
553/// let protocol = factory.create(Box::new(channel));
Allen George8b96bfb2016-11-02 08:01:08 -0400554/// ```
555pub trait TInputProtocolFactory {
Allen George0e22c362017-01-30 07:15:00 -0500556 // Create a `TInputProtocol` that reads bytes from `transport`.
Danny Browning77d96c12019-08-21 13:41:07 -0600557 fn create(&self, transport: Box<dyn TReadTransport + Send>) -> Box<dyn TInputProtocol + Send>;
Allen George0e22c362017-01-30 07:15:00 -0500558}
559
560impl<T> TInputProtocolFactory for Box<T>
561where
562 T: TInputProtocolFactory + ?Sized,
563{
Danny Browning77d96c12019-08-21 13:41:07 -0600564 fn create(&self, transport: Box<dyn TReadTransport + Send>) -> Box<dyn TInputProtocol + Send> {
Allen George0e22c362017-01-30 07:15:00 -0500565 (**self).create(transport)
566 }
Allen George8b96bfb2016-11-02 08:01:08 -0400567}
568
569/// Helper type used by servers to create `TOutputProtocol` instances for
570/// accepted client connections.
571///
572/// # Examples
573///
574/// Create a `TOutputProtocolFactory` and use it to create a `TOutputProtocol`.
575///
576/// ```no_run
Allen George8b96bfb2016-11-02 08:01:08 -0400577/// use thrift::protocol::{TBinaryOutputProtocolFactory, TOutputProtocolFactory};
Allen George0e22c362017-01-30 07:15:00 -0500578/// use thrift::transport::TTcpChannel;
Allen George8b96bfb2016-11-02 08:01:08 -0400579///
Allen George0e22c362017-01-30 07:15:00 -0500580/// let mut channel = TTcpChannel::new();
581/// channel.open("127.0.0.1:9090").unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -0400582///
Allen George0e22c362017-01-30 07:15:00 -0500583/// let factory = TBinaryOutputProtocolFactory::new();
584/// let protocol = factory.create(Box::new(channel));
Allen George8b96bfb2016-11-02 08:01:08 -0400585/// ```
586pub trait TOutputProtocolFactory {
587 /// Create a `TOutputProtocol` that writes bytes to `transport`.
Allen George55c3e4c2021-03-01 23:19:52 -0500588 fn create(&self, transport: Box<dyn TWriteTransport + Send>)
589 -> Box<dyn TOutputProtocol + Send>;
Allen George0e22c362017-01-30 07:15:00 -0500590}
591
592impl<T> TOutputProtocolFactory for Box<T>
593where
594 T: TOutputProtocolFactory + ?Sized,
595{
Allen George55c3e4c2021-03-01 23:19:52 -0500596 fn create(
597 &self,
598 transport: Box<dyn TWriteTransport + Send>,
599 ) -> Box<dyn TOutputProtocol + Send> {
Allen George0e22c362017-01-30 07:15:00 -0500600 (**self).create(transport)
601 }
Allen George8b96bfb2016-11-02 08:01:08 -0400602}
603
604/// Thrift message identifier.
605#[derive(Clone, Debug, Eq, PartialEq)]
606pub struct TMessageIdentifier {
607 /// Service call the message is associated with.
608 pub name: String,
609 /// Message type.
610 pub message_type: TMessageType,
611 /// Ordered sequence number identifying the message.
612 pub sequence_number: i32,
613}
614
615impl TMessageIdentifier {
616 /// Create a `TMessageIdentifier` for a Thrift service-call named `name`
617 /// with message type `message_type` and sequence number `sequence_number`.
Allen George0e22c362017-01-30 07:15:00 -0500618 pub fn new<S: Into<String>>(
619 name: S,
620 message_type: TMessageType,
621 sequence_number: i32,
622 ) -> TMessageIdentifier {
Allen George8b96bfb2016-11-02 08:01:08 -0400623 TMessageIdentifier {
624 name: name.into(),
Allen George7ddbcc02020-11-08 09:51:19 -0500625 message_type,
626 sequence_number,
Allen George8b96bfb2016-11-02 08:01:08 -0400627 }
628 }
629}
630
631/// Thrift struct identifier.
632#[derive(Clone, Debug, Eq, PartialEq)]
633pub struct TStructIdentifier {
634 /// Name of the encoded Thrift struct.
635 pub name: String,
636}
637
638impl TStructIdentifier {
639 /// Create a `TStructIdentifier` for a struct named `name`.
640 pub fn new<S: Into<String>>(name: S) -> TStructIdentifier {
641 TStructIdentifier { name: name.into() }
642 }
643}
644
645/// Thrift field identifier.
646#[derive(Clone, Debug, Eq, PartialEq)]
647pub struct TFieldIdentifier {
648 /// Name of the Thrift field.
649 ///
650 /// `None` if it's not sent over the wire.
651 pub name: Option<String>,
652 /// Field type.
653 ///
654 /// This may be a primitive, container, or a struct.
655 pub field_type: TType,
656 /// Thrift field id.
657 ///
658 /// `None` only if `field_type` is `TType::Stop`.
659 pub id: Option<i16>,
660}
661
662impl TFieldIdentifier {
663 /// Create a `TFieldIdentifier` for a field named `name` with type
664 /// `field_type` and field id `id`.
665 ///
666 /// `id` should be `None` if `field_type` is `TType::Stop`.
667 pub fn new<N, S, I>(name: N, field_type: TType, id: I) -> TFieldIdentifier
Allen George0e22c362017-01-30 07:15:00 -0500668 where
669 N: Into<Option<S>>,
670 S: Into<String>,
671 I: Into<Option<i16>>,
Allen George8b96bfb2016-11-02 08:01:08 -0400672 {
673 TFieldIdentifier {
674 name: name.into().map(|n| n.into()),
Allen George7ddbcc02020-11-08 09:51:19 -0500675 field_type,
Allen George8b96bfb2016-11-02 08:01:08 -0400676 id: id.into(),
677 }
678 }
679}
680
681/// Thrift list identifier.
682#[derive(Clone, Debug, Eq, PartialEq)]
683pub struct TListIdentifier {
684 /// Type of the elements in the list.
685 pub element_type: TType,
686 /// Number of elements in the list.
687 pub size: i32,
688}
689
690impl TListIdentifier {
691 /// Create a `TListIdentifier` for a list with `size` elements of type
692 /// `element_type`.
693 pub fn new(element_type: TType, size: i32) -> TListIdentifier {
Allen George55c3e4c2021-03-01 23:19:52 -0500694 TListIdentifier { element_type, size }
Allen George8b96bfb2016-11-02 08:01:08 -0400695 }
696}
697
698/// Thrift set identifier.
699#[derive(Clone, Debug, Eq, PartialEq)]
700pub struct TSetIdentifier {
701 /// Type of the elements in the set.
702 pub element_type: TType,
703 /// Number of elements in the set.
704 pub size: i32,
705}
706
707impl TSetIdentifier {
708 /// Create a `TSetIdentifier` for a set with `size` elements of type
709 /// `element_type`.
710 pub fn new(element_type: TType, size: i32) -> TSetIdentifier {
Allen George55c3e4c2021-03-01 23:19:52 -0500711 TSetIdentifier { element_type, size }
Allen George8b96bfb2016-11-02 08:01:08 -0400712 }
713}
714
715/// Thrift map identifier.
716#[derive(Clone, Debug, Eq, PartialEq)]
717pub struct TMapIdentifier {
718 /// Map key type.
719 pub key_type: Option<TType>,
720 /// Map value type.
721 pub value_type: Option<TType>,
722 /// Number of entries in the map.
723 pub size: i32,
724}
725
726impl TMapIdentifier {
727 /// Create a `TMapIdentifier` for a map with `size` entries of type
728 /// `key_type -> value_type`.
729 pub fn new<K, V>(key_type: K, value_type: V, size: i32) -> TMapIdentifier
Allen George0e22c362017-01-30 07:15:00 -0500730 where
731 K: Into<Option<TType>>,
732 V: Into<Option<TType>>,
Allen George8b96bfb2016-11-02 08:01:08 -0400733 {
734 TMapIdentifier {
735 key_type: key_type.into(),
736 value_type: value_type.into(),
Allen George7ddbcc02020-11-08 09:51:19 -0500737 size,
Allen George8b96bfb2016-11-02 08:01:08 -0400738 }
739 }
740}
741
742/// Thrift message types.
743#[derive(Clone, Copy, Debug, Eq, PartialEq)]
744pub enum TMessageType {
745 /// Service-call request.
746 Call,
747 /// Service-call response.
748 Reply,
749 /// Unexpected error in the remote service.
750 Exception,
751 /// One-way service-call request (no response is expected).
752 OneWay,
753}
754
755impl Display for TMessageType {
Allen George7ddbcc02020-11-08 09:51:19 -0500756 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Allen George8b96bfb2016-11-02 08:01:08 -0400757 match *self {
758 TMessageType::Call => write!(f, "Call"),
759 TMessageType::Reply => write!(f, "Reply"),
760 TMessageType::Exception => write!(f, "Exception"),
761 TMessageType::OneWay => write!(f, "OneWay"),
762 }
763 }
764}
765
766impl From<TMessageType> for u8 {
767 fn from(message_type: TMessageType) -> Self {
768 match message_type {
769 TMessageType::Call => 0x01,
770 TMessageType::Reply => 0x02,
771 TMessageType::Exception => 0x03,
772 TMessageType::OneWay => 0x04,
773 }
774 }
775}
776
777impl TryFrom<u8> for TMessageType {
Allen Georgeb0d14132020-03-29 11:48:55 -0400778 type Error = crate::Error;
Danny Browningddec4312019-03-08 14:20:41 -0700779 fn try_from(b: u8) -> Result<Self, Self::Error> {
Allen George8b96bfb2016-11-02 08:01:08 -0400780 match b {
781 0x01 => Ok(TMessageType::Call),
782 0x02 => Ok(TMessageType::Reply),
783 0x03 => Ok(TMessageType::Exception),
784 0x04 => Ok(TMessageType::OneWay),
Allen Georgeb0d14132020-03-29 11:48:55 -0400785 unkn => Err(crate::Error::Protocol(ProtocolError {
Allen Georgeef7a1892018-12-16 18:01:37 -0500786 kind: ProtocolErrorKind::InvalidData,
787 message: format!("cannot convert {} to TMessageType", unkn),
788 })),
Allen George8b96bfb2016-11-02 08:01:08 -0400789 }
790 }
791}
792
793/// Thrift struct-field types.
794#[derive(Clone, Copy, Debug, Eq, PartialEq)]
795pub enum TType {
796 /// Indicates that there are no more serialized fields in this Thrift struct.
797 Stop,
798 /// Void (`()`) field.
799 Void,
800 /// Boolean.
801 Bool,
802 /// Signed 8-bit int.
803 I08,
804 /// Double-precision number.
805 Double,
806 /// Signed 16-bit int.
807 I16,
808 /// Signed 32-bit int.
809 I32,
810 /// Signed 64-bit int.
811 I64,
812 /// UTF-8 string.
813 String,
814 /// UTF-7 string. *Unsupported*.
815 Utf7,
816 /// Thrift struct.
817 Struct,
818 /// Map.
819 Map,
820 /// Set.
821 Set,
822 /// List.
823 List,
824 /// UTF-8 string.
825 Utf8,
826 /// UTF-16 string. *Unsupported*.
827 Utf16,
828}
829
830impl Display for TType {
Allen George7ddbcc02020-11-08 09:51:19 -0500831 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Allen George8b96bfb2016-11-02 08:01:08 -0400832 match *self {
833 TType::Stop => write!(f, "STOP"),
834 TType::Void => write!(f, "void"),
835 TType::Bool => write!(f, "bool"),
836 TType::I08 => write!(f, "i08"),
837 TType::Double => write!(f, "double"),
838 TType::I16 => write!(f, "i16"),
839 TType::I32 => write!(f, "i32"),
840 TType::I64 => write!(f, "i64"),
841 TType::String => write!(f, "string"),
842 TType::Utf7 => write!(f, "UTF7"),
843 TType::Struct => write!(f, "struct"),
844 TType::Map => write!(f, "map"),
845 TType::Set => write!(f, "set"),
846 TType::List => write!(f, "list"),
847 TType::Utf8 => write!(f, "UTF8"),
848 TType::Utf16 => write!(f, "UTF16"),
849 }
850 }
851}
852
853/// Compare the expected message sequence number `expected` with the received
854/// message sequence number `actual`.
855///
856/// Return `()` if `actual == expected`, `Err` otherwise.
Allen Georgeb0d14132020-03-29 11:48:55 -0400857pub fn verify_expected_sequence_number(expected: i32, actual: i32) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400858 if expected == actual {
859 Ok(())
860 } else {
Allen Georgeb0d14132020-03-29 11:48:55 -0400861 Err(crate::Error::Application(crate::ApplicationError {
862 kind: crate::ApplicationErrorKind::BadSequenceId,
Allen Georgeef7a1892018-12-16 18:01:37 -0500863 message: format!("expected {} got {}", expected, actual),
864 }))
Allen George8b96bfb2016-11-02 08:01:08 -0400865 }
866}
867
868/// Compare the expected service-call name `expected` with the received
869/// service-call name `actual`.
870///
871/// Return `()` if `actual == expected`, `Err` otherwise.
Allen Georgeb0d14132020-03-29 11:48:55 -0400872pub fn verify_expected_service_call(expected: &str, actual: &str) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400873 if expected == actual {
874 Ok(())
875 } else {
Allen Georgeb0d14132020-03-29 11:48:55 -0400876 Err(crate::Error::Application(crate::ApplicationError {
877 kind: crate::ApplicationErrorKind::WrongMethodName,
Allen Georgeef7a1892018-12-16 18:01:37 -0500878 message: format!("expected {} got {}", expected, actual),
879 }))
Allen George8b96bfb2016-11-02 08:01:08 -0400880 }
881}
882
883/// Compare the expected message type `expected` with the received message type
884/// `actual`.
885///
886/// Return `()` if `actual == expected`, `Err` otherwise.
Allen George55c3e4c2021-03-01 23:19:52 -0500887pub fn verify_expected_message_type(
888 expected: TMessageType,
889 actual: TMessageType,
890) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400891 if expected == actual {
892 Ok(())
893 } else {
Allen Georgeb0d14132020-03-29 11:48:55 -0400894 Err(crate::Error::Application(crate::ApplicationError {
895 kind: crate::ApplicationErrorKind::InvalidMessageType,
Allen Georgeef7a1892018-12-16 18:01:37 -0500896 message: format!("expected {} got {}", expected, actual),
897 }))
Allen George8b96bfb2016-11-02 08:01:08 -0400898 }
899}
900
901/// Check if a required Thrift struct field exists.
902///
903/// Return `()` if it does, `Err` otherwise.
Allen Georgeb0d14132020-03-29 11:48:55 -0400904pub fn verify_required_field_exists<T>(field_name: &str, field: &Option<T>) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400905 match *field {
906 Some(_) => Ok(()),
Allen Georgeb0d14132020-03-29 11:48:55 -0400907 None => Err(crate::Error::Protocol(crate::ProtocolError {
908 kind: crate::ProtocolErrorKind::Unknown,
Allen Georgeef7a1892018-12-16 18:01:37 -0500909 message: format!("missing required field {}", field_name),
910 })),
Allen George8b96bfb2016-11-02 08:01:08 -0400911 }
912}
913
914/// Extract the field id from a Thrift field identifier.
915///
916/// `field_ident` must *not* have `TFieldIdentifier.field_type` of type `TType::Stop`.
917///
918/// Return `TFieldIdentifier.id` if an id exists, `Err` otherwise.
Allen Georgeb0d14132020-03-29 11:48:55 -0400919pub fn field_id(field_ident: &TFieldIdentifier) -> crate::Result<i16> {
Allen Georgeef7a1892018-12-16 18:01:37 -0500920 field_ident.id.ok_or_else(|| {
Allen Georgeb0d14132020-03-29 11:48:55 -0400921 crate::Error::Protocol(crate::ProtocolError {
922 kind: crate::ProtocolErrorKind::Unknown,
60067815d642022-09-01 16:05:12 +0800923 message: format!("missing field id in {:?}", field_ident),
Allen Georgeef7a1892018-12-16 18:01:37 -0500924 })
925 })
Allen George0e22c362017-01-30 07:15:00 -0500926}
927
928#[cfg(test)]
929mod tests {
930
931 use std::io::Cursor;
932
933 use super::*;
Allen Georgeb0d14132020-03-29 11:48:55 -0400934 use crate::transport::{TReadTransport, TWriteTransport};
Allen George0e22c362017-01-30 07:15:00 -0500935
936 #[test]
937 fn must_create_usable_input_protocol_from_concrete_input_protocol() {
Danny Browning77d96c12019-08-21 13:41:07 -0600938 let r: Box<dyn TReadTransport> = Box::new(Cursor::new([0, 1, 2]));
Allen George0e22c362017-01-30 07:15:00 -0500939 let mut t = TCompactInputProtocol::new(r);
940 takes_input_protocol(&mut t)
941 }
942
943 #[test]
944 fn must_create_usable_input_protocol_from_boxed_input() {
Danny Browning77d96c12019-08-21 13:41:07 -0600945 let r: Box<dyn TReadTransport> = Box::new(Cursor::new([0, 1, 2]));
946 let mut t: Box<dyn TInputProtocol> = Box::new(TCompactInputProtocol::new(r));
Allen George0e22c362017-01-30 07:15:00 -0500947 takes_input_protocol(&mut t)
948 }
949
950 #[test]
951 fn must_create_usable_output_protocol_from_concrete_output_protocol() {
Danny Browning77d96c12019-08-21 13:41:07 -0600952 let w: Box<dyn TWriteTransport> = Box::new(vec![0u8; 10]);
Allen George0e22c362017-01-30 07:15:00 -0500953 let mut t = TCompactOutputProtocol::new(w);
954 takes_output_protocol(&mut t)
955 }
956
957 #[test]
958 fn must_create_usable_output_protocol_from_boxed_output() {
Danny Browning77d96c12019-08-21 13:41:07 -0600959 let w: Box<dyn TWriteTransport> = Box::new(vec![0u8; 10]);
960 let mut t: Box<dyn TOutputProtocol> = Box::new(TCompactOutputProtocol::new(w));
Allen George0e22c362017-01-30 07:15:00 -0500961 takes_output_protocol(&mut t)
962 }
963
964 fn takes_input_protocol<R>(t: &mut R)
965 where
966 R: TInputProtocol,
967 {
968 t.read_byte().unwrap();
969 }
970
971 fn takes_output_protocol<W>(t: &mut W)
972 where
973 W: TOutputProtocol,
974 {
975 t.flush().unwrap();
976 }
Allen George8b96bfb2016-11-02 08:01:08 -0400977}