blob: 573978a758b7586ce66086c9d65b752f1e88b55f [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};
Hasnain Lakhani42d0b712025-07-17 19:57:05 -070065use crate::{ProtocolError, ProtocolErrorKind, TConfiguration};
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>;
Jiayu Liub6b6dc72022-10-08 14:28:44 +0800174 /// Read a UUID.
175 fn read_uuid(&mut self) -> crate::Result<uuid::Uuid>;
Allen George8b96bfb2016-11-02 08:01:08 -0400176 /// Read a fixed-length string (not null terminated).
Allen Georgeb0d14132020-03-29 11:48:55 -0400177 fn read_string(&mut self) -> crate::Result<String>;
Allen George8b96bfb2016-11-02 08:01:08 -0400178 /// Read the beginning of a list.
Allen Georgeb0d14132020-03-29 11:48:55 -0400179 fn read_list_begin(&mut self) -> crate::Result<TListIdentifier>;
Allen George8b96bfb2016-11-02 08:01:08 -0400180 /// Read the end of a list.
Allen Georgeb0d14132020-03-29 11:48:55 -0400181 fn read_list_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400182 /// Read the beginning of a set.
Allen Georgeb0d14132020-03-29 11:48:55 -0400183 fn read_set_begin(&mut self) -> crate::Result<TSetIdentifier>;
Allen George8b96bfb2016-11-02 08:01:08 -0400184 /// Read the end of a set.
Allen Georgeb0d14132020-03-29 11:48:55 -0400185 fn read_set_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400186 /// Read the beginning of a map.
Allen Georgeb0d14132020-03-29 11:48:55 -0400187 fn read_map_begin(&mut self) -> crate::Result<TMapIdentifier>;
Allen George8b96bfb2016-11-02 08:01:08 -0400188 /// Read the end of a map.
Allen Georgeb0d14132020-03-29 11:48:55 -0400189 fn read_map_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400190 /// Skip a field with type `field_type` recursively until the default
191 /// maximum skip depth is reached.
Allen Georgeb0d14132020-03-29 11:48:55 -0400192 fn skip(&mut self, field_type: TType) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400193 self.skip_till_depth(field_type, MAXIMUM_SKIP_DEPTH)
194 }
195 /// Skip a field with type `field_type` recursively up to `depth` levels.
Allen Georgeb0d14132020-03-29 11:48:55 -0400196 fn skip_till_depth(&mut self, field_type: TType, depth: i8) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400197 if depth == 0 {
Allen Georgeb0d14132020-03-29 11:48:55 -0400198 return Err(crate::Error::Protocol(ProtocolError {
Allen Georgeef7a1892018-12-16 18:01:37 -0500199 kind: ProtocolErrorKind::DepthLimit,
200 message: format!("cannot parse past {:?}", field_type),
201 }));
Allen George8b96bfb2016-11-02 08:01:08 -0400202 }
203
204 match field_type {
205 TType::Bool => self.read_bool().map(|_| ()),
206 TType::I08 => self.read_i8().map(|_| ()),
207 TType::I16 => self.read_i16().map(|_| ()),
208 TType::I32 => self.read_i32().map(|_| ()),
209 TType::I64 => self.read_i64().map(|_| ()),
210 TType::Double => self.read_double().map(|_| ()),
211 TType::String => self.read_string().map(|_| ()),
212 TType::Struct => {
213 self.read_struct_begin()?;
214 loop {
215 let field_ident = self.read_field_begin()?;
216 if field_ident.field_type == TType::Stop {
217 break;
218 }
219 self.skip_till_depth(field_ident.field_type, depth - 1)?;
220 }
221 self.read_struct_end()
222 }
223 TType::List => {
224 let list_ident = self.read_list_begin()?;
225 for _ in 0..list_ident.size {
226 self.skip_till_depth(list_ident.element_type, depth - 1)?;
227 }
228 self.read_list_end()
229 }
230 TType::Set => {
231 let set_ident = self.read_set_begin()?;
232 for _ in 0..set_ident.size {
233 self.skip_till_depth(set_ident.element_type, depth - 1)?;
234 }
235 self.read_set_end()
236 }
237 TType::Map => {
238 let map_ident = self.read_map_begin()?;
239 for _ in 0..map_ident.size {
Allen George0e22c362017-01-30 07:15:00 -0500240 let key_type = map_ident
241 .key_type
Allen George8b96bfb2016-11-02 08:01:08 -0400242 .expect("non-zero sized map should contain key type");
Allen George0e22c362017-01-30 07:15:00 -0500243 let val_type = map_ident
244 .value_type
Allen George8b96bfb2016-11-02 08:01:08 -0400245 .expect("non-zero sized map should contain value type");
246 self.skip_till_depth(key_type, depth - 1)?;
247 self.skip_till_depth(val_type, depth - 1)?;
248 }
249 self.read_map_end()
250 }
Allen Georgeb0d14132020-03-29 11:48:55 -0400251 u => Err(crate::Error::Protocol(ProtocolError {
Allen Georgeef7a1892018-12-16 18:01:37 -0500252 kind: ProtocolErrorKind::Unknown,
253 message: format!("cannot skip field type {:?}", &u),
254 })),
Allen George8b96bfb2016-11-02 08:01:08 -0400255 }
256 }
257
258 // utility (DO NOT USE IN GENERATED CODE!!!!)
259 //
260
261 /// Read an unsigned byte.
262 ///
263 /// This method should **never** be used in generated code.
Allen Georgeb0d14132020-03-29 11:48:55 -0400264 fn read_byte(&mut self) -> crate::Result<u8>;
Hasnain Lakhani42d0b712025-07-17 19:57:05 -0700265
266 /// Get the minimum number of bytes a type will consume on the wire.
267 /// This picks the minimum possible across all protocols (so currently matches the compact protocol).
268 ///
269 /// This is used for pre-allocation size checks.
270 /// The actual data may be larger (e.g., for strings, lists, etc.).
271 fn min_serialized_size(&self, field_type: TType) -> usize {
272 self::compact::compact_protocol_min_serialized_size(field_type)
273 }
Allen George8b96bfb2016-11-02 08:01:08 -0400274}
275
276/// Converts Thrift identifiers, primitives, containers or structs into a
277/// stream of bytes.
278///
279/// This trait does not deal with higher-level Thrift concepts like structs or
280/// exceptions - only with primitives and message or container boundaries.
281/// Write methods take an identifier (for example, `TMessageIdentifier`) or a
282/// primitive. Any or all of the fields in an identifier may be omitted when
283/// writing to the transport. Write methods may even be noops. All of this is
284/// transparent to the caller; as long as a matching `TInputProtocol`
285/// implementation is used, received messages will be decoded correctly.
286///
287/// All methods return a `thrift::Result`. If an `Err` is returned the protocol
288/// instance and its underlying transport should be terminated.
289///
290/// # Examples
291///
292/// Create and use a `TOutputProtocol`
293///
294/// ```no_run
Allen George8b96bfb2016-11-02 08:01:08 -0400295/// use thrift::protocol::{TBinaryOutputProtocol, TFieldIdentifier, TOutputProtocol, TType};
Allen George0e22c362017-01-30 07:15:00 -0500296/// use thrift::transport::TTcpChannel;
Allen George8b96bfb2016-11-02 08:01:08 -0400297///
Allen George0e22c362017-01-30 07:15:00 -0500298/// let mut channel = TTcpChannel::new();
299/// channel.open("127.0.0.1:9090").unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -0400300///
Allen George0e22c362017-01-30 07:15:00 -0500301/// let mut protocol = TBinaryOutputProtocol::new(channel, true);
Allen George8b96bfb2016-11-02 08:01:08 -0400302///
Allen George0e22c362017-01-30 07:15:00 -0500303/// protocol.write_field_begin(&TFieldIdentifier::new("string_thing", TType::String, 1)).unwrap();
304/// protocol.write_string("foo").unwrap();
305/// protocol.write_field_end().unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -0400306/// ```
307pub trait TOutputProtocol {
308 /// Write the beginning of a Thrift message.
Allen Georgeb0d14132020-03-29 11:48:55 -0400309 fn write_message_begin(&mut self, identifier: &TMessageIdentifier) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400310 /// Write the end of a Thrift message.
Allen Georgeb0d14132020-03-29 11:48:55 -0400311 fn write_message_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400312 /// Write the beginning of a Thrift struct.
Allen Georgeb0d14132020-03-29 11:48:55 -0400313 fn write_struct_begin(&mut self, identifier: &TStructIdentifier) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400314 /// Write the end of a Thrift struct.
Allen Georgeb0d14132020-03-29 11:48:55 -0400315 fn write_struct_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400316 /// Write the beginning of a Thrift field.
Allen Georgeb0d14132020-03-29 11:48:55 -0400317 fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400318 /// Write the end of a Thrift field.
Allen Georgeb0d14132020-03-29 11:48:55 -0400319 fn write_field_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400320 /// Write a STOP field indicating that all the fields in a struct have been
321 /// written.
Allen Georgeb0d14132020-03-29 11:48:55 -0400322 fn write_field_stop(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400323 /// Write a bool.
Allen Georgeb0d14132020-03-29 11:48:55 -0400324 fn write_bool(&mut self, b: bool) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400325 /// Write a fixed-length byte array.
Allen Georgeb0d14132020-03-29 11:48:55 -0400326 fn write_bytes(&mut self, b: &[u8]) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400327 /// Write an 8-bit signed integer.
Allen Georgeb0d14132020-03-29 11:48:55 -0400328 fn write_i8(&mut self, i: i8) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400329 /// Write a 16-bit signed integer.
Allen Georgeb0d14132020-03-29 11:48:55 -0400330 fn write_i16(&mut self, i: i16) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400331 /// Write a 32-bit signed integer.
Allen Georgeb0d14132020-03-29 11:48:55 -0400332 fn write_i32(&mut self, i: i32) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400333 /// Write a 64-bit signed integer.
Allen Georgeb0d14132020-03-29 11:48:55 -0400334 fn write_i64(&mut self, i: i64) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400335 /// Write a 64-bit float.
Allen Georgeb0d14132020-03-29 11:48:55 -0400336 fn write_double(&mut self, d: f64) -> crate::Result<()>;
Jiayu Liub6b6dc72022-10-08 14:28:44 +0800337 /// Write a UUID
338 fn write_uuid(&mut self, uuid: &uuid::Uuid) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400339 /// Write a fixed-length string.
Allen Georgeb0d14132020-03-29 11:48:55 -0400340 fn write_string(&mut self, s: &str) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400341 /// Write the beginning of a list.
Allen Georgeb0d14132020-03-29 11:48:55 -0400342 fn write_list_begin(&mut self, identifier: &TListIdentifier) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400343 /// Write the end of a list.
Allen Georgeb0d14132020-03-29 11:48:55 -0400344 fn write_list_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400345 /// Write the beginning of a set.
Allen Georgeb0d14132020-03-29 11:48:55 -0400346 fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400347 /// Write the end of a set.
Allen Georgeb0d14132020-03-29 11:48:55 -0400348 fn write_set_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400349 /// Write the beginning of a map.
Allen Georgeb0d14132020-03-29 11:48:55 -0400350 fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400351 /// Write the end of a map.
Allen Georgeb0d14132020-03-29 11:48:55 -0400352 fn write_map_end(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400353 /// Flush buffered bytes to the underlying transport.
Allen Georgeb0d14132020-03-29 11:48:55 -0400354 fn flush(&mut self) -> crate::Result<()>;
Allen George8b96bfb2016-11-02 08:01:08 -0400355
356 // utility (DO NOT USE IN GENERATED CODE!!!!)
357 //
358
359 /// Write an unsigned byte.
360 ///
361 /// This method should **never** be used in generated code.
Allen Georgeb0d14132020-03-29 11:48:55 -0400362 fn write_byte(&mut self, b: u8) -> crate::Result<()>; // FIXME: REMOVE
Allen George8b96bfb2016-11-02 08:01:08 -0400363}
364
Allen George0e22c362017-01-30 07:15:00 -0500365impl<P> TInputProtocol for Box<P>
366where
367 P: TInputProtocol + ?Sized,
368{
Allen Georgeb0d14132020-03-29 11:48:55 -0400369 fn read_message_begin(&mut self) -> crate::Result<TMessageIdentifier> {
Allen George0e22c362017-01-30 07:15:00 -0500370 (**self).read_message_begin()
371 }
372
Allen Georgeb0d14132020-03-29 11:48:55 -0400373 fn read_message_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500374 (**self).read_message_end()
375 }
376
Allen Georgeb0d14132020-03-29 11:48:55 -0400377 fn read_struct_begin(&mut self) -> crate::Result<Option<TStructIdentifier>> {
Allen George0e22c362017-01-30 07:15:00 -0500378 (**self).read_struct_begin()
379 }
380
Allen Georgeb0d14132020-03-29 11:48:55 -0400381 fn read_struct_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500382 (**self).read_struct_end()
383 }
384
Allen Georgeb0d14132020-03-29 11:48:55 -0400385 fn read_field_begin(&mut self) -> crate::Result<TFieldIdentifier> {
Allen George0e22c362017-01-30 07:15:00 -0500386 (**self).read_field_begin()
387 }
388
Allen Georgeb0d14132020-03-29 11:48:55 -0400389 fn read_field_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500390 (**self).read_field_end()
391 }
392
Allen Georgeb0d14132020-03-29 11:48:55 -0400393 fn read_bool(&mut self) -> crate::Result<bool> {
Allen George0e22c362017-01-30 07:15:00 -0500394 (**self).read_bool()
395 }
396
Allen Georgeb0d14132020-03-29 11:48:55 -0400397 fn read_bytes(&mut self) -> crate::Result<Vec<u8>> {
Allen George0e22c362017-01-30 07:15:00 -0500398 (**self).read_bytes()
399 }
400
Allen Georgeb0d14132020-03-29 11:48:55 -0400401 fn read_i8(&mut self) -> crate::Result<i8> {
Allen George0e22c362017-01-30 07:15:00 -0500402 (**self).read_i8()
403 }
404
Allen Georgeb0d14132020-03-29 11:48:55 -0400405 fn read_i16(&mut self) -> crate::Result<i16> {
Allen George0e22c362017-01-30 07:15:00 -0500406 (**self).read_i16()
407 }
408
Allen Georgeb0d14132020-03-29 11:48:55 -0400409 fn read_i32(&mut self) -> crate::Result<i32> {
Allen George0e22c362017-01-30 07:15:00 -0500410 (**self).read_i32()
411 }
412
Allen Georgeb0d14132020-03-29 11:48:55 -0400413 fn read_i64(&mut self) -> crate::Result<i64> {
Allen George0e22c362017-01-30 07:15:00 -0500414 (**self).read_i64()
415 }
416
Allen Georgeb0d14132020-03-29 11:48:55 -0400417 fn read_double(&mut self) -> crate::Result<f64> {
Allen George0e22c362017-01-30 07:15:00 -0500418 (**self).read_double()
419 }
420
Jiayu Liub6b6dc72022-10-08 14:28:44 +0800421 fn read_uuid(&mut self) -> crate::Result<uuid::Uuid> {
422 (**self).read_uuid()
423 }
424
Allen Georgeb0d14132020-03-29 11:48:55 -0400425 fn read_string(&mut self) -> crate::Result<String> {
Allen George0e22c362017-01-30 07:15:00 -0500426 (**self).read_string()
427 }
428
Allen Georgeb0d14132020-03-29 11:48:55 -0400429 fn read_list_begin(&mut self) -> crate::Result<TListIdentifier> {
Allen George0e22c362017-01-30 07:15:00 -0500430 (**self).read_list_begin()
431 }
432
Allen Georgeb0d14132020-03-29 11:48:55 -0400433 fn read_list_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500434 (**self).read_list_end()
435 }
436
Allen Georgeb0d14132020-03-29 11:48:55 -0400437 fn read_set_begin(&mut self) -> crate::Result<TSetIdentifier> {
Allen George0e22c362017-01-30 07:15:00 -0500438 (**self).read_set_begin()
439 }
440
Allen Georgeb0d14132020-03-29 11:48:55 -0400441 fn read_set_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500442 (**self).read_set_end()
443 }
444
Allen Georgeb0d14132020-03-29 11:48:55 -0400445 fn read_map_begin(&mut self) -> crate::Result<TMapIdentifier> {
Allen George0e22c362017-01-30 07:15:00 -0500446 (**self).read_map_begin()
447 }
448
Allen Georgeb0d14132020-03-29 11:48:55 -0400449 fn read_map_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500450 (**self).read_map_end()
451 }
452
Allen Georgeb0d14132020-03-29 11:48:55 -0400453 fn read_byte(&mut self) -> crate::Result<u8> {
Allen George0e22c362017-01-30 07:15:00 -0500454 (**self).read_byte()
455 }
Hasnain Lakhani42d0b712025-07-17 19:57:05 -0700456
457 fn min_serialized_size(&self, field_type: TType) -> usize {
458 (**self).min_serialized_size(field_type)
459 }
Allen George0e22c362017-01-30 07:15:00 -0500460}
461
462impl<P> TOutputProtocol for Box<P>
463where
464 P: TOutputProtocol + ?Sized,
465{
Allen Georgeb0d14132020-03-29 11:48:55 -0400466 fn write_message_begin(&mut self, identifier: &TMessageIdentifier) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500467 (**self).write_message_begin(identifier)
468 }
469
Allen Georgeb0d14132020-03-29 11:48:55 -0400470 fn write_message_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500471 (**self).write_message_end()
472 }
473
Allen Georgeb0d14132020-03-29 11:48:55 -0400474 fn write_struct_begin(&mut self, identifier: &TStructIdentifier) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500475 (**self).write_struct_begin(identifier)
476 }
477
Allen Georgeb0d14132020-03-29 11:48:55 -0400478 fn write_struct_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500479 (**self).write_struct_end()
480 }
481
Allen Georgeb0d14132020-03-29 11:48:55 -0400482 fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500483 (**self).write_field_begin(identifier)
484 }
485
Allen Georgeb0d14132020-03-29 11:48:55 -0400486 fn write_field_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500487 (**self).write_field_end()
488 }
489
Allen Georgeb0d14132020-03-29 11:48:55 -0400490 fn write_field_stop(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500491 (**self).write_field_stop()
492 }
493
Allen Georgeb0d14132020-03-29 11:48:55 -0400494 fn write_bool(&mut self, b: bool) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500495 (**self).write_bool(b)
496 }
497
Allen Georgeb0d14132020-03-29 11:48:55 -0400498 fn write_bytes(&mut self, b: &[u8]) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500499 (**self).write_bytes(b)
500 }
501
Allen Georgeb0d14132020-03-29 11:48:55 -0400502 fn write_i8(&mut self, i: i8) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500503 (**self).write_i8(i)
504 }
505
Allen Georgeb0d14132020-03-29 11:48:55 -0400506 fn write_i16(&mut self, i: i16) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500507 (**self).write_i16(i)
508 }
509
Allen Georgeb0d14132020-03-29 11:48:55 -0400510 fn write_i32(&mut self, i: i32) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500511 (**self).write_i32(i)
512 }
513
Allen Georgeb0d14132020-03-29 11:48:55 -0400514 fn write_i64(&mut self, i: i64) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500515 (**self).write_i64(i)
516 }
517
Allen Georgeb0d14132020-03-29 11:48:55 -0400518 fn write_double(&mut self, d: f64) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500519 (**self).write_double(d)
520 }
521
Jiayu Liub6b6dc72022-10-08 14:28:44 +0800522 fn write_uuid(&mut self, uuid: &uuid::Uuid) -> crate::Result<()> {
523 (**self).write_uuid(uuid)
524 }
525
Allen Georgeb0d14132020-03-29 11:48:55 -0400526 fn write_string(&mut self, s: &str) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500527 (**self).write_string(s)
528 }
529
Allen Georgeb0d14132020-03-29 11:48:55 -0400530 fn write_list_begin(&mut self, identifier: &TListIdentifier) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500531 (**self).write_list_begin(identifier)
532 }
533
Allen Georgeb0d14132020-03-29 11:48:55 -0400534 fn write_list_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500535 (**self).write_list_end()
536 }
537
Allen Georgeb0d14132020-03-29 11:48:55 -0400538 fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500539 (**self).write_set_begin(identifier)
540 }
541
Allen Georgeb0d14132020-03-29 11:48:55 -0400542 fn write_set_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500543 (**self).write_set_end()
544 }
545
Allen Georgeb0d14132020-03-29 11:48:55 -0400546 fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500547 (**self).write_map_begin(identifier)
548 }
549
Allen Georgeb0d14132020-03-29 11:48:55 -0400550 fn write_map_end(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500551 (**self).write_map_end()
552 }
553
Allen Georgeb0d14132020-03-29 11:48:55 -0400554 fn flush(&mut self) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500555 (**self).flush()
556 }
557
Allen Georgeb0d14132020-03-29 11:48:55 -0400558 fn write_byte(&mut self, b: u8) -> crate::Result<()> {
Allen George0e22c362017-01-30 07:15:00 -0500559 (**self).write_byte(b)
560 }
561}
562
Allen George8b96bfb2016-11-02 08:01:08 -0400563/// Helper type used by servers to create `TInputProtocol` instances for
564/// accepted client connections.
565///
566/// # Examples
567///
568/// Create a `TInputProtocolFactory` and use it to create a `TInputProtocol`.
569///
570/// ```no_run
Allen George8b96bfb2016-11-02 08:01:08 -0400571/// use thrift::protocol::{TBinaryInputProtocolFactory, TInputProtocolFactory};
Allen George0e22c362017-01-30 07:15:00 -0500572/// use thrift::transport::TTcpChannel;
Allen George8b96bfb2016-11-02 08:01:08 -0400573///
Allen George0e22c362017-01-30 07:15:00 -0500574/// let mut channel = TTcpChannel::new();
575/// channel.open("127.0.0.1:9090").unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -0400576///
Allen George0e22c362017-01-30 07:15:00 -0500577/// let factory = TBinaryInputProtocolFactory::new();
578/// let protocol = factory.create(Box::new(channel));
Allen George8b96bfb2016-11-02 08:01:08 -0400579/// ```
580pub trait TInputProtocolFactory {
Hasnain Lakhani42d0b712025-07-17 19:57:05 -0700581 /// Create a `TInputProtocol` that reads bytes from `transport`.
Danny Browning77d96c12019-08-21 13:41:07 -0600582 fn create(&self, transport: Box<dyn TReadTransport + Send>) -> Box<dyn TInputProtocol + Send>;
Allen George0e22c362017-01-30 07:15:00 -0500583}
584
585impl<T> TInputProtocolFactory for Box<T>
586where
587 T: TInputProtocolFactory + ?Sized,
588{
Danny Browning77d96c12019-08-21 13:41:07 -0600589 fn create(&self, transport: Box<dyn TReadTransport + Send>) -> Box<dyn TInputProtocol + Send> {
Allen George0e22c362017-01-30 07:15:00 -0500590 (**self).create(transport)
591 }
Allen George8b96bfb2016-11-02 08:01:08 -0400592}
593
594/// Helper type used by servers to create `TOutputProtocol` instances for
595/// accepted client connections.
596///
597/// # Examples
598///
599/// Create a `TOutputProtocolFactory` and use it to create a `TOutputProtocol`.
600///
601/// ```no_run
Allen George8b96bfb2016-11-02 08:01:08 -0400602/// use thrift::protocol::{TBinaryOutputProtocolFactory, TOutputProtocolFactory};
Allen George0e22c362017-01-30 07:15:00 -0500603/// use thrift::transport::TTcpChannel;
Allen George8b96bfb2016-11-02 08:01:08 -0400604///
Allen George0e22c362017-01-30 07:15:00 -0500605/// let mut channel = TTcpChannel::new();
606/// channel.open("127.0.0.1:9090").unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -0400607///
Allen George0e22c362017-01-30 07:15:00 -0500608/// let factory = TBinaryOutputProtocolFactory::new();
609/// let protocol = factory.create(Box::new(channel));
Allen George8b96bfb2016-11-02 08:01:08 -0400610/// ```
611pub trait TOutputProtocolFactory {
612 /// Create a `TOutputProtocol` that writes bytes to `transport`.
Allen George55c3e4c2021-03-01 23:19:52 -0500613 fn create(&self, transport: Box<dyn TWriteTransport + Send>)
614 -> Box<dyn TOutputProtocol + Send>;
Allen George0e22c362017-01-30 07:15:00 -0500615}
616
617impl<T> TOutputProtocolFactory for Box<T>
618where
619 T: TOutputProtocolFactory + ?Sized,
620{
Allen George55c3e4c2021-03-01 23:19:52 -0500621 fn create(
622 &self,
623 transport: Box<dyn TWriteTransport + Send>,
624 ) -> Box<dyn TOutputProtocol + Send> {
Allen George0e22c362017-01-30 07:15:00 -0500625 (**self).create(transport)
626 }
Allen George8b96bfb2016-11-02 08:01:08 -0400627}
628
629/// Thrift message identifier.
630#[derive(Clone, Debug, Eq, PartialEq)]
631pub struct TMessageIdentifier {
632 /// Service call the message is associated with.
633 pub name: String,
634 /// Message type.
635 pub message_type: TMessageType,
636 /// Ordered sequence number identifying the message.
637 pub sequence_number: i32,
638}
639
640impl TMessageIdentifier {
641 /// Create a `TMessageIdentifier` for a Thrift service-call named `name`
642 /// with message type `message_type` and sequence number `sequence_number`.
Allen George0e22c362017-01-30 07:15:00 -0500643 pub fn new<S: Into<String>>(
644 name: S,
645 message_type: TMessageType,
646 sequence_number: i32,
647 ) -> TMessageIdentifier {
Allen George8b96bfb2016-11-02 08:01:08 -0400648 TMessageIdentifier {
649 name: name.into(),
Allen George7ddbcc02020-11-08 09:51:19 -0500650 message_type,
651 sequence_number,
Allen George8b96bfb2016-11-02 08:01:08 -0400652 }
653 }
654}
655
656/// Thrift struct identifier.
657#[derive(Clone, Debug, Eq, PartialEq)]
658pub struct TStructIdentifier {
659 /// Name of the encoded Thrift struct.
660 pub name: String,
661}
662
663impl TStructIdentifier {
664 /// Create a `TStructIdentifier` for a struct named `name`.
665 pub fn new<S: Into<String>>(name: S) -> TStructIdentifier {
666 TStructIdentifier { name: name.into() }
667 }
668}
669
670/// Thrift field identifier.
671#[derive(Clone, Debug, Eq, PartialEq)]
672pub struct TFieldIdentifier {
673 /// Name of the Thrift field.
674 ///
675 /// `None` if it's not sent over the wire.
676 pub name: Option<String>,
677 /// Field type.
678 ///
679 /// This may be a primitive, container, or a struct.
680 pub field_type: TType,
681 /// Thrift field id.
682 ///
683 /// `None` only if `field_type` is `TType::Stop`.
684 pub id: Option<i16>,
685}
686
687impl TFieldIdentifier {
688 /// Create a `TFieldIdentifier` for a field named `name` with type
689 /// `field_type` and field id `id`.
690 ///
691 /// `id` should be `None` if `field_type` is `TType::Stop`.
692 pub fn new<N, S, I>(name: N, field_type: TType, id: I) -> TFieldIdentifier
Allen George0e22c362017-01-30 07:15:00 -0500693 where
694 N: Into<Option<S>>,
695 S: Into<String>,
696 I: Into<Option<i16>>,
Allen George8b96bfb2016-11-02 08:01:08 -0400697 {
698 TFieldIdentifier {
699 name: name.into().map(|n| n.into()),
Allen George7ddbcc02020-11-08 09:51:19 -0500700 field_type,
Allen George8b96bfb2016-11-02 08:01:08 -0400701 id: id.into(),
702 }
703 }
704}
705
706/// Thrift list identifier.
707#[derive(Clone, Debug, Eq, PartialEq)]
708pub struct TListIdentifier {
709 /// Type of the elements in the list.
710 pub element_type: TType,
711 /// Number of elements in the list.
712 pub size: i32,
713}
714
715impl TListIdentifier {
716 /// Create a `TListIdentifier` for a list with `size` elements of type
717 /// `element_type`.
718 pub fn new(element_type: TType, size: i32) -> TListIdentifier {
Allen George55c3e4c2021-03-01 23:19:52 -0500719 TListIdentifier { element_type, size }
Allen George8b96bfb2016-11-02 08:01:08 -0400720 }
721}
722
723/// Thrift set identifier.
724#[derive(Clone, Debug, Eq, PartialEq)]
725pub struct TSetIdentifier {
726 /// Type of the elements in the set.
727 pub element_type: TType,
728 /// Number of elements in the set.
729 pub size: i32,
730}
731
732impl TSetIdentifier {
733 /// Create a `TSetIdentifier` for a set with `size` elements of type
734 /// `element_type`.
735 pub fn new(element_type: TType, size: i32) -> TSetIdentifier {
Allen George55c3e4c2021-03-01 23:19:52 -0500736 TSetIdentifier { element_type, size }
Allen George8b96bfb2016-11-02 08:01:08 -0400737 }
738}
739
740/// Thrift map identifier.
741#[derive(Clone, Debug, Eq, PartialEq)]
742pub struct TMapIdentifier {
743 /// Map key type.
744 pub key_type: Option<TType>,
745 /// Map value type.
746 pub value_type: Option<TType>,
747 /// Number of entries in the map.
748 pub size: i32,
749}
750
751impl TMapIdentifier {
752 /// Create a `TMapIdentifier` for a map with `size` entries of type
753 /// `key_type -> value_type`.
754 pub fn new<K, V>(key_type: K, value_type: V, size: i32) -> TMapIdentifier
Allen George0e22c362017-01-30 07:15:00 -0500755 where
756 K: Into<Option<TType>>,
757 V: Into<Option<TType>>,
Allen George8b96bfb2016-11-02 08:01:08 -0400758 {
759 TMapIdentifier {
760 key_type: key_type.into(),
761 value_type: value_type.into(),
Allen George7ddbcc02020-11-08 09:51:19 -0500762 size,
Allen George8b96bfb2016-11-02 08:01:08 -0400763 }
764 }
765}
766
767/// Thrift message types.
768#[derive(Clone, Copy, Debug, Eq, PartialEq)]
769pub enum TMessageType {
770 /// Service-call request.
771 Call,
772 /// Service-call response.
773 Reply,
774 /// Unexpected error in the remote service.
775 Exception,
776 /// One-way service-call request (no response is expected).
777 OneWay,
778}
779
780impl Display for TMessageType {
Allen George7ddbcc02020-11-08 09:51:19 -0500781 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Allen George8b96bfb2016-11-02 08:01:08 -0400782 match *self {
783 TMessageType::Call => write!(f, "Call"),
784 TMessageType::Reply => write!(f, "Reply"),
785 TMessageType::Exception => write!(f, "Exception"),
786 TMessageType::OneWay => write!(f, "OneWay"),
787 }
788 }
789}
790
791impl From<TMessageType> for u8 {
792 fn from(message_type: TMessageType) -> Self {
793 match message_type {
794 TMessageType::Call => 0x01,
795 TMessageType::Reply => 0x02,
796 TMessageType::Exception => 0x03,
797 TMessageType::OneWay => 0x04,
798 }
799 }
800}
801
802impl TryFrom<u8> for TMessageType {
Allen Georgeb0d14132020-03-29 11:48:55 -0400803 type Error = crate::Error;
Danny Browningddec4312019-03-08 14:20:41 -0700804 fn try_from(b: u8) -> Result<Self, Self::Error> {
Allen George8b96bfb2016-11-02 08:01:08 -0400805 match b {
806 0x01 => Ok(TMessageType::Call),
807 0x02 => Ok(TMessageType::Reply),
808 0x03 => Ok(TMessageType::Exception),
809 0x04 => Ok(TMessageType::OneWay),
Allen Georgeb0d14132020-03-29 11:48:55 -0400810 unkn => Err(crate::Error::Protocol(ProtocolError {
Allen Georgeef7a1892018-12-16 18:01:37 -0500811 kind: ProtocolErrorKind::InvalidData,
812 message: format!("cannot convert {} to TMessageType", unkn),
813 })),
Allen George8b96bfb2016-11-02 08:01:08 -0400814 }
815 }
816}
817
818/// Thrift struct-field types.
819#[derive(Clone, Copy, Debug, Eq, PartialEq)]
820pub enum TType {
821 /// Indicates that there are no more serialized fields in this Thrift struct.
822 Stop,
823 /// Void (`()`) field.
824 Void,
825 /// Boolean.
826 Bool,
827 /// Signed 8-bit int.
828 I08,
829 /// Double-precision number.
830 Double,
831 /// Signed 16-bit int.
832 I16,
833 /// Signed 32-bit int.
834 I32,
835 /// Signed 64-bit int.
836 I64,
837 /// UTF-8 string.
838 String,
839 /// UTF-7 string. *Unsupported*.
840 Utf7,
841 /// Thrift struct.
842 Struct,
843 /// Map.
844 Map,
845 /// Set.
846 Set,
847 /// List.
848 List,
Jiayu Liub6b6dc72022-10-08 14:28:44 +0800849 /// Uuid.
850 Uuid,
Allen George8b96bfb2016-11-02 08:01:08 -0400851}
852
853impl Display for TType {
Allen George7ddbcc02020-11-08 09:51:19 -0500854 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Allen George8b96bfb2016-11-02 08:01:08 -0400855 match *self {
856 TType::Stop => write!(f, "STOP"),
857 TType::Void => write!(f, "void"),
858 TType::Bool => write!(f, "bool"),
859 TType::I08 => write!(f, "i08"),
860 TType::Double => write!(f, "double"),
861 TType::I16 => write!(f, "i16"),
862 TType::I32 => write!(f, "i32"),
863 TType::I64 => write!(f, "i64"),
864 TType::String => write!(f, "string"),
865 TType::Utf7 => write!(f, "UTF7"),
866 TType::Struct => write!(f, "struct"),
867 TType::Map => write!(f, "map"),
868 TType::Set => write!(f, "set"),
869 TType::List => write!(f, "list"),
Jiayu Liub6b6dc72022-10-08 14:28:44 +0800870 TType::Uuid => write!(f, "UUID"),
Allen George8b96bfb2016-11-02 08:01:08 -0400871 }
872 }
873}
874
875/// Compare the expected message sequence number `expected` with the received
876/// message sequence number `actual`.
877///
878/// Return `()` if `actual == expected`, `Err` otherwise.
Allen Georgeb0d14132020-03-29 11:48:55 -0400879pub fn verify_expected_sequence_number(expected: i32, actual: i32) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400880 if expected == actual {
881 Ok(())
882 } else {
Allen Georgeb0d14132020-03-29 11:48:55 -0400883 Err(crate::Error::Application(crate::ApplicationError {
884 kind: crate::ApplicationErrorKind::BadSequenceId,
Allen Georgeef7a1892018-12-16 18:01:37 -0500885 message: format!("expected {} got {}", expected, actual),
886 }))
Allen George8b96bfb2016-11-02 08:01:08 -0400887 }
888}
889
890/// Compare the expected service-call name `expected` with the received
891/// service-call name `actual`.
892///
893/// Return `()` if `actual == expected`, `Err` otherwise.
Allen Georgeb0d14132020-03-29 11:48:55 -0400894pub fn verify_expected_service_call(expected: &str, actual: &str) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400895 if expected == actual {
896 Ok(())
897 } else {
Allen Georgeb0d14132020-03-29 11:48:55 -0400898 Err(crate::Error::Application(crate::ApplicationError {
899 kind: crate::ApplicationErrorKind::WrongMethodName,
Allen Georgeef7a1892018-12-16 18:01:37 -0500900 message: format!("expected {} got {}", expected, actual),
901 }))
Allen George8b96bfb2016-11-02 08:01:08 -0400902 }
903}
904
905/// Compare the expected message type `expected` with the received message type
906/// `actual`.
907///
908/// Return `()` if `actual == expected`, `Err` otherwise.
Allen George55c3e4c2021-03-01 23:19:52 -0500909pub fn verify_expected_message_type(
910 expected: TMessageType,
911 actual: TMessageType,
912) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400913 if expected == actual {
914 Ok(())
915 } else {
Allen Georgeb0d14132020-03-29 11:48:55 -0400916 Err(crate::Error::Application(crate::ApplicationError {
917 kind: crate::ApplicationErrorKind::InvalidMessageType,
Allen Georgeef7a1892018-12-16 18:01:37 -0500918 message: format!("expected {} got {}", expected, actual),
919 }))
Allen George8b96bfb2016-11-02 08:01:08 -0400920 }
921}
922
923/// Check if a required Thrift struct field exists.
924///
925/// Return `()` if it does, `Err` otherwise.
Allen Georgeb0d14132020-03-29 11:48:55 -0400926pub fn verify_required_field_exists<T>(field_name: &str, field: &Option<T>) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400927 match *field {
928 Some(_) => Ok(()),
Allen Georgeb0d14132020-03-29 11:48:55 -0400929 None => Err(crate::Error::Protocol(crate::ProtocolError {
930 kind: crate::ProtocolErrorKind::Unknown,
Allen Georgeef7a1892018-12-16 18:01:37 -0500931 message: format!("missing required field {}", field_name),
932 })),
Allen George8b96bfb2016-11-02 08:01:08 -0400933 }
934}
935
Hasnain Lakhani42d0b712025-07-17 19:57:05 -0700936/// Common container size validation used by all protocols.
937///
938/// Checks that:
939/// - Container size is not negative
940/// - Container size doesn't exceed configured maximum
941/// - Container size * element size doesn't overflow
942/// - Container memory requirements don't exceed message size limit
943pub(crate) fn check_container_size(
944 config: &TConfiguration,
945 container_size: i32,
946 element_size: usize,
947) -> crate::Result<()> {
948 // Check for negative size
949 if container_size < 0 {
950 return Err(crate::Error::Protocol(ProtocolError::new(
951 ProtocolErrorKind::NegativeSize,
952 format!("Negative container size: {}", container_size),
953 )));
954 }
955
956 let size_as_usize = container_size as usize;
957
958 // Check against configured max container size
959 if let Some(max_size) = config.max_container_size() {
960 if size_as_usize > max_size {
961 return Err(crate::Error::Protocol(ProtocolError::new(
962 ProtocolErrorKind::SizeLimit,
963 format!(
964 "Container size {} exceeds maximum allowed size of {}",
965 container_size, max_size
966 ),
967 )));
968 }
969 }
970
971 // Check for potential overflow
972 if let Some(min_bytes_needed) = size_as_usize.checked_mul(element_size) {
973 // TODO: When Rust trait specialization stabilizes, we can add more precise checks
974 // for transports that track exact remaining bytes. For now, we use the message
975 // size limit as a best-effort check.
976 if let Some(max_message_size) = config.max_message_size() {
977 if min_bytes_needed > max_message_size {
978 return Err(crate::Error::Protocol(ProtocolError::new(
979 ProtocolErrorKind::SizeLimit,
980 format!(
981 "Container would require {} bytes, exceeding message size limit of {}",
982 min_bytes_needed, max_message_size
983 ),
984 )));
985 }
986 }
987 Ok(())
988 } else {
989 Err(crate::Error::Protocol(ProtocolError::new(
990 ProtocolErrorKind::SizeLimit,
991 format!(
992 "Container size {} with element size {} bytes would result in overflow",
993 container_size, element_size
994 ),
995 )))
996 }
997}
998
Allen George8b96bfb2016-11-02 08:01:08 -0400999/// Extract the field id from a Thrift field identifier.
1000///
1001/// `field_ident` must *not* have `TFieldIdentifier.field_type` of type `TType::Stop`.
1002///
1003/// Return `TFieldIdentifier.id` if an id exists, `Err` otherwise.
Allen Georgeb0d14132020-03-29 11:48:55 -04001004pub fn field_id(field_ident: &TFieldIdentifier) -> crate::Result<i16> {
Allen Georgeef7a1892018-12-16 18:01:37 -05001005 field_ident.id.ok_or_else(|| {
Allen Georgeb0d14132020-03-29 11:48:55 -04001006 crate::Error::Protocol(crate::ProtocolError {
1007 kind: crate::ProtocolErrorKind::Unknown,
60067815d642022-09-01 16:05:12 +08001008 message: format!("missing field id in {:?}", field_ident),
Allen Georgeef7a1892018-12-16 18:01:37 -05001009 })
1010 })
Allen George0e22c362017-01-30 07:15:00 -05001011}
1012
1013#[cfg(test)]
1014mod tests {
1015
1016 use std::io::Cursor;
1017
1018 use super::*;
Allen Georgeb0d14132020-03-29 11:48:55 -04001019 use crate::transport::{TReadTransport, TWriteTransport};
Allen George0e22c362017-01-30 07:15:00 -05001020
1021 #[test]
1022 fn must_create_usable_input_protocol_from_concrete_input_protocol() {
Danny Browning77d96c12019-08-21 13:41:07 -06001023 let r: Box<dyn TReadTransport> = Box::new(Cursor::new([0, 1, 2]));
Allen George0e22c362017-01-30 07:15:00 -05001024 let mut t = TCompactInputProtocol::new(r);
1025 takes_input_protocol(&mut t)
1026 }
1027
1028 #[test]
1029 fn must_create_usable_input_protocol_from_boxed_input() {
Danny Browning77d96c12019-08-21 13:41:07 -06001030 let r: Box<dyn TReadTransport> = Box::new(Cursor::new([0, 1, 2]));
1031 let mut t: Box<dyn TInputProtocol> = Box::new(TCompactInputProtocol::new(r));
Allen George0e22c362017-01-30 07:15:00 -05001032 takes_input_protocol(&mut t)
1033 }
1034
1035 #[test]
1036 fn must_create_usable_output_protocol_from_concrete_output_protocol() {
Danny Browning77d96c12019-08-21 13:41:07 -06001037 let w: Box<dyn TWriteTransport> = Box::new(vec![0u8; 10]);
Allen George0e22c362017-01-30 07:15:00 -05001038 let mut t = TCompactOutputProtocol::new(w);
1039 takes_output_protocol(&mut t)
1040 }
1041
1042 #[test]
1043 fn must_create_usable_output_protocol_from_boxed_output() {
Danny Browning77d96c12019-08-21 13:41:07 -06001044 let w: Box<dyn TWriteTransport> = Box::new(vec![0u8; 10]);
1045 let mut t: Box<dyn TOutputProtocol> = Box::new(TCompactOutputProtocol::new(w));
Allen George0e22c362017-01-30 07:15:00 -05001046 takes_output_protocol(&mut t)
1047 }
1048
1049 fn takes_input_protocol<R>(t: &mut R)
1050 where
1051 R: TInputProtocol,
1052 {
1053 t.read_byte().unwrap();
1054 }
1055
1056 fn takes_output_protocol<W>(t: &mut W)
1057 where
1058 W: TOutputProtocol,
1059 {
1060 t.flush().unwrap();
1061 }
Allen George8b96bfb2016-11-02 08:01:08 -04001062}