blob: 4f139147c9ebc9c1c41e280381ee16ab87b34184 [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
Allen George8b96bfb2016-11-02 08:01:08 -040060use std::fmt;
61use std::fmt::{Display, Formatter};
62use std::convert::From;
Allen George8b96bfb2016-11-02 08:01:08 -040063use try_from::TryFrom;
64
Allen George0e22c362017-01-30 07:15:00 -050065use {ProtocolError, ProtocolErrorKind};
66use transport::{TReadTransport, TWriteTransport};
67
68#[cfg(test)]
69macro_rules! assert_eq_written_bytes {
70 ($o_prot:ident, $expected_bytes:ident) => {
71 {
72 assert_eq!($o_prot.transport.write_bytes(), &$expected_bytes);
73 }
74 };
75}
76
77// FIXME: should take both read and write
78#[cfg(test)]
79macro_rules! copy_write_buffer_to_read_buffer {
80 ($o_prot:ident) => {
81 {
82 $o_prot.transport.copy_write_buffer_to_read_buffer();
83 }
84 };
85}
86
87#[cfg(test)]
88macro_rules! set_readable_bytes {
89 ($i_prot:ident, $bytes:expr) => {
90 $i_prot.transport.set_readable_bytes($bytes);
91 }
92}
Allen George8b96bfb2016-11-02 08:01:08 -040093
94mod binary;
95mod compact;
96mod multiplexed;
97mod stored;
98
99pub use self::binary::{TBinaryInputProtocol, TBinaryInputProtocolFactory, TBinaryOutputProtocol,
100 TBinaryOutputProtocolFactory};
101pub use self::compact::{TCompactInputProtocol, TCompactInputProtocolFactory,
102 TCompactOutputProtocol, TCompactOutputProtocolFactory};
103pub use self::multiplexed::TMultiplexedOutputProtocol;
104pub use self::stored::TStoredInputProtocol;
105
106// Default maximum depth to which `TInputProtocol::skip` will skip a Thrift
107// field. A default is necessary because Thrift structs or collections may
108// contain nested structs and collections, which could result in indefinite
109// recursion.
110const MAXIMUM_SKIP_DEPTH: i8 = 64;
111
112/// Converts a stream of bytes into Thrift identifiers, primitives,
113/// containers, or structs.
114///
115/// This trait does not deal with higher-level Thrift concepts like structs or
116/// exceptions - only with primitives and message or container boundaries. Once
117/// bytes are read they are deserialized and an identifier (for example
118/// `TMessageIdentifier`) or a primitive is returned.
119///
120/// All methods return a `thrift::Result`. If an `Err` is returned the protocol
121/// instance and its underlying transport should be terminated.
122///
123/// # Examples
124///
125/// Create and use a `TInputProtocol`
126///
127/// ```no_run
Allen George8b96bfb2016-11-02 08:01:08 -0400128/// use thrift::protocol::{TBinaryInputProtocol, TInputProtocol};
Allen George0e22c362017-01-30 07:15:00 -0500129/// use thrift::transport::TTcpChannel;
Allen George8b96bfb2016-11-02 08:01:08 -0400130///
Allen George0e22c362017-01-30 07:15:00 -0500131/// let mut channel = TTcpChannel::new();
132/// channel.open("127.0.0.1:9090").unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -0400133///
Allen George0e22c362017-01-30 07:15:00 -0500134/// let mut protocol = TBinaryInputProtocol::new(channel, true);
Allen George8b96bfb2016-11-02 08:01:08 -0400135///
Allen George0e22c362017-01-30 07:15:00 -0500136/// let field_identifier = protocol.read_field_begin().unwrap();
137/// let field_contents = protocol.read_string().unwrap();
138/// let field_end = protocol.read_field_end().unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -0400139/// ```
140pub trait TInputProtocol {
141 /// Read the beginning of a Thrift message.
142 fn read_message_begin(&mut self) -> ::Result<TMessageIdentifier>;
143 /// Read the end of a Thrift message.
144 fn read_message_end(&mut self) -> ::Result<()>;
145 /// Read the beginning of a Thrift struct.
146 fn read_struct_begin(&mut self) -> ::Result<Option<TStructIdentifier>>;
147 /// Read the end of a Thrift struct.
148 fn read_struct_end(&mut self) -> ::Result<()>;
149 /// Read the beginning of a Thrift struct field.
150 fn read_field_begin(&mut self) -> ::Result<TFieldIdentifier>;
151 /// Read the end of a Thrift struct field.
152 fn read_field_end(&mut self) -> ::Result<()>;
153 /// Read a bool.
154 fn read_bool(&mut self) -> ::Result<bool>;
155 /// Read a fixed-length byte array.
156 fn read_bytes(&mut self) -> ::Result<Vec<u8>>;
157 /// Read a word.
158 fn read_i8(&mut self) -> ::Result<i8>;
159 /// Read a 16-bit signed integer.
160 fn read_i16(&mut self) -> ::Result<i16>;
161 /// Read a 32-bit signed integer.
162 fn read_i32(&mut self) -> ::Result<i32>;
163 /// Read a 64-bit signed integer.
164 fn read_i64(&mut self) -> ::Result<i64>;
165 /// Read a 64-bit float.
166 fn read_double(&mut self) -> ::Result<f64>;
167 /// Read a fixed-length string (not null terminated).
168 fn read_string(&mut self) -> ::Result<String>;
169 /// Read the beginning of a list.
170 fn read_list_begin(&mut self) -> ::Result<TListIdentifier>;
171 /// Read the end of a list.
172 fn read_list_end(&mut self) -> ::Result<()>;
173 /// Read the beginning of a set.
174 fn read_set_begin(&mut self) -> ::Result<TSetIdentifier>;
175 /// Read the end of a set.
176 fn read_set_end(&mut self) -> ::Result<()>;
177 /// Read the beginning of a map.
178 fn read_map_begin(&mut self) -> ::Result<TMapIdentifier>;
179 /// Read the end of a map.
180 fn read_map_end(&mut self) -> ::Result<()>;
181 /// Skip a field with type `field_type` recursively until the default
182 /// maximum skip depth is reached.
183 fn skip(&mut self, field_type: TType) -> ::Result<()> {
184 self.skip_till_depth(field_type, MAXIMUM_SKIP_DEPTH)
185 }
186 /// Skip a field with type `field_type` recursively up to `depth` levels.
187 fn skip_till_depth(&mut self, field_type: TType, depth: i8) -> ::Result<()> {
188 if depth == 0 {
Allen George0e22c362017-01-30 07:15:00 -0500189 return Err(
190 ::Error::Protocol(
191 ProtocolError {
192 kind: ProtocolErrorKind::DepthLimit,
193 message: format!("cannot parse past {:?}", field_type),
194 },
195 ),
196 );
Allen George8b96bfb2016-11-02 08:01:08 -0400197 }
198
199 match field_type {
200 TType::Bool => self.read_bool().map(|_| ()),
201 TType::I08 => self.read_i8().map(|_| ()),
202 TType::I16 => self.read_i16().map(|_| ()),
203 TType::I32 => self.read_i32().map(|_| ()),
204 TType::I64 => self.read_i64().map(|_| ()),
205 TType::Double => self.read_double().map(|_| ()),
206 TType::String => self.read_string().map(|_| ()),
207 TType::Struct => {
208 self.read_struct_begin()?;
209 loop {
210 let field_ident = self.read_field_begin()?;
211 if field_ident.field_type == TType::Stop {
212 break;
213 }
214 self.skip_till_depth(field_ident.field_type, depth - 1)?;
215 }
216 self.read_struct_end()
217 }
218 TType::List => {
219 let list_ident = self.read_list_begin()?;
220 for _ in 0..list_ident.size {
221 self.skip_till_depth(list_ident.element_type, depth - 1)?;
222 }
223 self.read_list_end()
224 }
225 TType::Set => {
226 let set_ident = self.read_set_begin()?;
227 for _ in 0..set_ident.size {
228 self.skip_till_depth(set_ident.element_type, depth - 1)?;
229 }
230 self.read_set_end()
231 }
232 TType::Map => {
233 let map_ident = self.read_map_begin()?;
234 for _ in 0..map_ident.size {
Allen George0e22c362017-01-30 07:15:00 -0500235 let key_type = map_ident
236 .key_type
Allen George8b96bfb2016-11-02 08:01:08 -0400237 .expect("non-zero sized map should contain key type");
Allen George0e22c362017-01-30 07:15:00 -0500238 let val_type = map_ident
239 .value_type
Allen George8b96bfb2016-11-02 08:01:08 -0400240 .expect("non-zero sized map should contain value type");
241 self.skip_till_depth(key_type, depth - 1)?;
242 self.skip_till_depth(val_type, depth - 1)?;
243 }
244 self.read_map_end()
245 }
246 u => {
Allen George0e22c362017-01-30 07:15:00 -0500247 Err(
248 ::Error::Protocol(
249 ProtocolError {
250 kind: ProtocolErrorKind::Unknown,
251 message: format!("cannot skip field type {:?}", &u),
252 },
253 ),
254 )
Allen George8b96bfb2016-11-02 08:01:08 -0400255 }
256 }
257 }
258
259 // utility (DO NOT USE IN GENERATED CODE!!!!)
260 //
261
262 /// Read an unsigned byte.
263 ///
264 /// This method should **never** be used in generated code.
265 fn read_byte(&mut self) -> ::Result<u8>;
266}
267
268/// Converts Thrift identifiers, primitives, containers or structs into a
269/// stream of bytes.
270///
271/// This trait does not deal with higher-level Thrift concepts like structs or
272/// exceptions - only with primitives and message or container boundaries.
273/// Write methods take an identifier (for example, `TMessageIdentifier`) or a
274/// primitive. Any or all of the fields in an identifier may be omitted when
275/// writing to the transport. Write methods may even be noops. All of this is
276/// transparent to the caller; as long as a matching `TInputProtocol`
277/// implementation is used, received messages will be decoded correctly.
278///
279/// All methods return a `thrift::Result`. If an `Err` is returned the protocol
280/// instance and its underlying transport should be terminated.
281///
282/// # Examples
283///
284/// Create and use a `TOutputProtocol`
285///
286/// ```no_run
Allen George8b96bfb2016-11-02 08:01:08 -0400287/// use thrift::protocol::{TBinaryOutputProtocol, TFieldIdentifier, TOutputProtocol, TType};
Allen George0e22c362017-01-30 07:15:00 -0500288/// use thrift::transport::TTcpChannel;
Allen George8b96bfb2016-11-02 08:01:08 -0400289///
Allen George0e22c362017-01-30 07:15:00 -0500290/// let mut channel = TTcpChannel::new();
291/// channel.open("127.0.0.1:9090").unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -0400292///
Allen George0e22c362017-01-30 07:15:00 -0500293/// let mut protocol = TBinaryOutputProtocol::new(channel, true);
Allen George8b96bfb2016-11-02 08:01:08 -0400294///
Allen George0e22c362017-01-30 07:15:00 -0500295/// protocol.write_field_begin(&TFieldIdentifier::new("string_thing", TType::String, 1)).unwrap();
296/// protocol.write_string("foo").unwrap();
297/// protocol.write_field_end().unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -0400298/// ```
299pub trait TOutputProtocol {
300 /// Write the beginning of a Thrift message.
301 fn write_message_begin(&mut self, identifier: &TMessageIdentifier) -> ::Result<()>;
302 /// Write the end of a Thrift message.
303 fn write_message_end(&mut self) -> ::Result<()>;
304 /// Write the beginning of a Thrift struct.
305 fn write_struct_begin(&mut self, identifier: &TStructIdentifier) -> ::Result<()>;
306 /// Write the end of a Thrift struct.
307 fn write_struct_end(&mut self) -> ::Result<()>;
308 /// Write the beginning of a Thrift field.
309 fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> ::Result<()>;
310 /// Write the end of a Thrift field.
311 fn write_field_end(&mut self) -> ::Result<()>;
312 /// Write a STOP field indicating that all the fields in a struct have been
313 /// written.
314 fn write_field_stop(&mut self) -> ::Result<()>;
315 /// Write a bool.
316 fn write_bool(&mut self, b: bool) -> ::Result<()>;
317 /// Write a fixed-length byte array.
318 fn write_bytes(&mut self, b: &[u8]) -> ::Result<()>;
319 /// Write an 8-bit signed integer.
320 fn write_i8(&mut self, i: i8) -> ::Result<()>;
321 /// Write a 16-bit signed integer.
322 fn write_i16(&mut self, i: i16) -> ::Result<()>;
323 /// Write a 32-bit signed integer.
324 fn write_i32(&mut self, i: i32) -> ::Result<()>;
325 /// Write a 64-bit signed integer.
326 fn write_i64(&mut self, i: i64) -> ::Result<()>;
327 /// Write a 64-bit float.
328 fn write_double(&mut self, d: f64) -> ::Result<()>;
329 /// Write a fixed-length string.
330 fn write_string(&mut self, s: &str) -> ::Result<()>;
331 /// Write the beginning of a list.
332 fn write_list_begin(&mut self, identifier: &TListIdentifier) -> ::Result<()>;
333 /// Write the end of a list.
334 fn write_list_end(&mut self) -> ::Result<()>;
335 /// Write the beginning of a set.
336 fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> ::Result<()>;
337 /// Write the end of a set.
338 fn write_set_end(&mut self) -> ::Result<()>;
339 /// Write the beginning of a map.
340 fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> ::Result<()>;
341 /// Write the end of a map.
342 fn write_map_end(&mut self) -> ::Result<()>;
343 /// Flush buffered bytes to the underlying transport.
344 fn flush(&mut self) -> ::Result<()>;
345
346 // utility (DO NOT USE IN GENERATED CODE!!!!)
347 //
348
349 /// Write an unsigned byte.
350 ///
351 /// This method should **never** be used in generated code.
352 fn write_byte(&mut self, b: u8) -> ::Result<()>; // FIXME: REMOVE
353}
354
Allen George0e22c362017-01-30 07:15:00 -0500355impl<P> TInputProtocol for Box<P>
356where
357 P: TInputProtocol + ?Sized,
358{
359 fn read_message_begin(&mut self) -> ::Result<TMessageIdentifier> {
360 (**self).read_message_begin()
361 }
362
363 fn read_message_end(&mut self) -> ::Result<()> {
364 (**self).read_message_end()
365 }
366
367 fn read_struct_begin(&mut self) -> ::Result<Option<TStructIdentifier>> {
368 (**self).read_struct_begin()
369 }
370
371 fn read_struct_end(&mut self) -> ::Result<()> {
372 (**self).read_struct_end()
373 }
374
375 fn read_field_begin(&mut self) -> ::Result<TFieldIdentifier> {
376 (**self).read_field_begin()
377 }
378
379 fn read_field_end(&mut self) -> ::Result<()> {
380 (**self).read_field_end()
381 }
382
383 fn read_bool(&mut self) -> ::Result<bool> {
384 (**self).read_bool()
385 }
386
387 fn read_bytes(&mut self) -> ::Result<Vec<u8>> {
388 (**self).read_bytes()
389 }
390
391 fn read_i8(&mut self) -> ::Result<i8> {
392 (**self).read_i8()
393 }
394
395 fn read_i16(&mut self) -> ::Result<i16> {
396 (**self).read_i16()
397 }
398
399 fn read_i32(&mut self) -> ::Result<i32> {
400 (**self).read_i32()
401 }
402
403 fn read_i64(&mut self) -> ::Result<i64> {
404 (**self).read_i64()
405 }
406
407 fn read_double(&mut self) -> ::Result<f64> {
408 (**self).read_double()
409 }
410
411 fn read_string(&mut self) -> ::Result<String> {
412 (**self).read_string()
413 }
414
415 fn read_list_begin(&mut self) -> ::Result<TListIdentifier> {
416 (**self).read_list_begin()
417 }
418
419 fn read_list_end(&mut self) -> ::Result<()> {
420 (**self).read_list_end()
421 }
422
423 fn read_set_begin(&mut self) -> ::Result<TSetIdentifier> {
424 (**self).read_set_begin()
425 }
426
427 fn read_set_end(&mut self) -> ::Result<()> {
428 (**self).read_set_end()
429 }
430
431 fn read_map_begin(&mut self) -> ::Result<TMapIdentifier> {
432 (**self).read_map_begin()
433 }
434
435 fn read_map_end(&mut self) -> ::Result<()> {
436 (**self).read_map_end()
437 }
438
439 fn read_byte(&mut self) -> ::Result<u8> {
440 (**self).read_byte()
441 }
442}
443
444impl<P> TOutputProtocol for Box<P>
445where
446 P: TOutputProtocol + ?Sized,
447{
448 fn write_message_begin(&mut self, identifier: &TMessageIdentifier) -> ::Result<()> {
449 (**self).write_message_begin(identifier)
450 }
451
452 fn write_message_end(&mut self) -> ::Result<()> {
453 (**self).write_message_end()
454 }
455
456 fn write_struct_begin(&mut self, identifier: &TStructIdentifier) -> ::Result<()> {
457 (**self).write_struct_begin(identifier)
458 }
459
460 fn write_struct_end(&mut self) -> ::Result<()> {
461 (**self).write_struct_end()
462 }
463
464 fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> ::Result<()> {
465 (**self).write_field_begin(identifier)
466 }
467
468 fn write_field_end(&mut self) -> ::Result<()> {
469 (**self).write_field_end()
470 }
471
472 fn write_field_stop(&mut self) -> ::Result<()> {
473 (**self).write_field_stop()
474 }
475
476 fn write_bool(&mut self, b: bool) -> ::Result<()> {
477 (**self).write_bool(b)
478 }
479
480 fn write_bytes(&mut self, b: &[u8]) -> ::Result<()> {
481 (**self).write_bytes(b)
482 }
483
484 fn write_i8(&mut self, i: i8) -> ::Result<()> {
485 (**self).write_i8(i)
486 }
487
488 fn write_i16(&mut self, i: i16) -> ::Result<()> {
489 (**self).write_i16(i)
490 }
491
492 fn write_i32(&mut self, i: i32) -> ::Result<()> {
493 (**self).write_i32(i)
494 }
495
496 fn write_i64(&mut self, i: i64) -> ::Result<()> {
497 (**self).write_i64(i)
498 }
499
500 fn write_double(&mut self, d: f64) -> ::Result<()> {
501 (**self).write_double(d)
502 }
503
504 fn write_string(&mut self, s: &str) -> ::Result<()> {
505 (**self).write_string(s)
506 }
507
508 fn write_list_begin(&mut self, identifier: &TListIdentifier) -> ::Result<()> {
509 (**self).write_list_begin(identifier)
510 }
511
512 fn write_list_end(&mut self) -> ::Result<()> {
513 (**self).write_list_end()
514 }
515
516 fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> ::Result<()> {
517 (**self).write_set_begin(identifier)
518 }
519
520 fn write_set_end(&mut self) -> ::Result<()> {
521 (**self).write_set_end()
522 }
523
524 fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> ::Result<()> {
525 (**self).write_map_begin(identifier)
526 }
527
528 fn write_map_end(&mut self) -> ::Result<()> {
529 (**self).write_map_end()
530 }
531
532 fn flush(&mut self) -> ::Result<()> {
533 (**self).flush()
534 }
535
536 fn write_byte(&mut self, b: u8) -> ::Result<()> {
537 (**self).write_byte(b)
538 }
539}
540
Allen George8b96bfb2016-11-02 08:01:08 -0400541/// Helper type used by servers to create `TInputProtocol` instances for
542/// accepted client connections.
543///
544/// # Examples
545///
546/// Create a `TInputProtocolFactory` and use it to create a `TInputProtocol`.
547///
548/// ```no_run
Allen George8b96bfb2016-11-02 08:01:08 -0400549/// use thrift::protocol::{TBinaryInputProtocolFactory, TInputProtocolFactory};
Allen George0e22c362017-01-30 07:15:00 -0500550/// use thrift::transport::TTcpChannel;
Allen George8b96bfb2016-11-02 08:01:08 -0400551///
Allen George0e22c362017-01-30 07:15:00 -0500552/// let mut channel = TTcpChannel::new();
553/// channel.open("127.0.0.1:9090").unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -0400554///
Allen George0e22c362017-01-30 07:15:00 -0500555/// let factory = TBinaryInputProtocolFactory::new();
556/// let protocol = factory.create(Box::new(channel));
Allen George8b96bfb2016-11-02 08:01:08 -0400557/// ```
558pub trait TInputProtocolFactory {
Allen George0e22c362017-01-30 07:15:00 -0500559 // Create a `TInputProtocol` that reads bytes from `transport`.
560 fn create(&self, transport: Box<TReadTransport + Send>) -> Box<TInputProtocol + Send>;
561}
562
563impl<T> TInputProtocolFactory for Box<T>
564where
565 T: TInputProtocolFactory + ?Sized,
566{
567 fn create(&self, transport: Box<TReadTransport + Send>) -> Box<TInputProtocol + Send> {
568 (**self).create(transport)
569 }
Allen George8b96bfb2016-11-02 08:01:08 -0400570}
571
572/// Helper type used by servers to create `TOutputProtocol` instances for
573/// accepted client connections.
574///
575/// # Examples
576///
577/// Create a `TOutputProtocolFactory` and use it to create a `TOutputProtocol`.
578///
579/// ```no_run
Allen George8b96bfb2016-11-02 08:01:08 -0400580/// use thrift::protocol::{TBinaryOutputProtocolFactory, TOutputProtocolFactory};
Allen George0e22c362017-01-30 07:15:00 -0500581/// use thrift::transport::TTcpChannel;
Allen George8b96bfb2016-11-02 08:01:08 -0400582///
Allen George0e22c362017-01-30 07:15:00 -0500583/// let mut channel = TTcpChannel::new();
584/// channel.open("127.0.0.1:9090").unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -0400585///
Allen George0e22c362017-01-30 07:15:00 -0500586/// let factory = TBinaryOutputProtocolFactory::new();
587/// let protocol = factory.create(Box::new(channel));
Allen George8b96bfb2016-11-02 08:01:08 -0400588/// ```
589pub trait TOutputProtocolFactory {
590 /// Create a `TOutputProtocol` that writes bytes to `transport`.
Allen George0e22c362017-01-30 07:15:00 -0500591 fn create(&self, transport: Box<TWriteTransport + Send>) -> Box<TOutputProtocol + Send>;
592}
593
594impl<T> TOutputProtocolFactory for Box<T>
595where
596 T: TOutputProtocolFactory + ?Sized,
597{
598 fn create(&self, transport: Box<TWriteTransport + Send>) -> Box<TOutputProtocol + Send> {
599 (**self).create(transport)
600 }
Allen George8b96bfb2016-11-02 08:01:08 -0400601}
602
603/// Thrift message identifier.
604#[derive(Clone, Debug, Eq, PartialEq)]
605pub struct TMessageIdentifier {
606 /// Service call the message is associated with.
607 pub name: String,
608 /// Message type.
609 pub message_type: TMessageType,
610 /// Ordered sequence number identifying the message.
611 pub sequence_number: i32,
612}
613
614impl TMessageIdentifier {
615 /// Create a `TMessageIdentifier` for a Thrift service-call named `name`
616 /// with message type `message_type` and sequence number `sequence_number`.
Allen George0e22c362017-01-30 07:15:00 -0500617 pub fn new<S: Into<String>>(
618 name: S,
619 message_type: TMessageType,
620 sequence_number: i32,
621 ) -> TMessageIdentifier {
Allen George8b96bfb2016-11-02 08:01:08 -0400622 TMessageIdentifier {
623 name: name.into(),
624 message_type: message_type,
625 sequence_number: sequence_number,
626 }
627 }
628}
629
630/// Thrift struct identifier.
631#[derive(Clone, Debug, Eq, PartialEq)]
632pub struct TStructIdentifier {
633 /// Name of the encoded Thrift struct.
634 pub name: String,
635}
636
637impl TStructIdentifier {
638 /// Create a `TStructIdentifier` for a struct named `name`.
639 pub fn new<S: Into<String>>(name: S) -> TStructIdentifier {
640 TStructIdentifier { name: name.into() }
641 }
642}
643
644/// Thrift field identifier.
645#[derive(Clone, Debug, Eq, PartialEq)]
646pub struct TFieldIdentifier {
647 /// Name of the Thrift field.
648 ///
649 /// `None` if it's not sent over the wire.
650 pub name: Option<String>,
651 /// Field type.
652 ///
653 /// This may be a primitive, container, or a struct.
654 pub field_type: TType,
655 /// Thrift field id.
656 ///
657 /// `None` only if `field_type` is `TType::Stop`.
658 pub id: Option<i16>,
659}
660
661impl TFieldIdentifier {
662 /// Create a `TFieldIdentifier` for a field named `name` with type
663 /// `field_type` and field id `id`.
664 ///
665 /// `id` should be `None` if `field_type` is `TType::Stop`.
666 pub fn new<N, S, I>(name: N, field_type: TType, id: I) -> TFieldIdentifier
Allen George0e22c362017-01-30 07:15:00 -0500667 where
668 N: Into<Option<S>>,
669 S: Into<String>,
670 I: Into<Option<i16>>,
Allen George8b96bfb2016-11-02 08:01:08 -0400671 {
672 TFieldIdentifier {
673 name: name.into().map(|n| n.into()),
674 field_type: field_type,
675 id: id.into(),
676 }
677 }
678}
679
680/// Thrift list identifier.
681#[derive(Clone, Debug, Eq, PartialEq)]
682pub struct TListIdentifier {
683 /// Type of the elements in the list.
684 pub element_type: TType,
685 /// Number of elements in the list.
686 pub size: i32,
687}
688
689impl TListIdentifier {
690 /// Create a `TListIdentifier` for a list with `size` elements of type
691 /// `element_type`.
692 pub fn new(element_type: TType, size: i32) -> TListIdentifier {
693 TListIdentifier {
694 element_type: element_type,
695 size: size,
696 }
697 }
698}
699
700/// Thrift set identifier.
701#[derive(Clone, Debug, Eq, PartialEq)]
702pub struct TSetIdentifier {
703 /// Type of the elements in the set.
704 pub element_type: TType,
705 /// Number of elements in the set.
706 pub size: i32,
707}
708
709impl TSetIdentifier {
710 /// Create a `TSetIdentifier` for a set with `size` elements of type
711 /// `element_type`.
712 pub fn new(element_type: TType, size: i32) -> TSetIdentifier {
713 TSetIdentifier {
714 element_type: element_type,
715 size: size,
716 }
717 }
718}
719
720/// Thrift map identifier.
721#[derive(Clone, Debug, Eq, PartialEq)]
722pub struct TMapIdentifier {
723 /// Map key type.
724 pub key_type: Option<TType>,
725 /// Map value type.
726 pub value_type: Option<TType>,
727 /// Number of entries in the map.
728 pub size: i32,
729}
730
731impl TMapIdentifier {
732 /// Create a `TMapIdentifier` for a map with `size` entries of type
733 /// `key_type -> value_type`.
734 pub fn new<K, V>(key_type: K, value_type: V, size: i32) -> TMapIdentifier
Allen George0e22c362017-01-30 07:15:00 -0500735 where
736 K: Into<Option<TType>>,
737 V: Into<Option<TType>>,
Allen George8b96bfb2016-11-02 08:01:08 -0400738 {
739 TMapIdentifier {
740 key_type: key_type.into(),
741 value_type: value_type.into(),
742 size: size,
743 }
744 }
745}
746
747/// Thrift message types.
748#[derive(Clone, Copy, Debug, Eq, PartialEq)]
749pub enum TMessageType {
750 /// Service-call request.
751 Call,
752 /// Service-call response.
753 Reply,
754 /// Unexpected error in the remote service.
755 Exception,
756 /// One-way service-call request (no response is expected).
757 OneWay,
758}
759
760impl Display for TMessageType {
761 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
762 match *self {
763 TMessageType::Call => write!(f, "Call"),
764 TMessageType::Reply => write!(f, "Reply"),
765 TMessageType::Exception => write!(f, "Exception"),
766 TMessageType::OneWay => write!(f, "OneWay"),
767 }
768 }
769}
770
771impl From<TMessageType> for u8 {
772 fn from(message_type: TMessageType) -> Self {
773 match message_type {
774 TMessageType::Call => 0x01,
775 TMessageType::Reply => 0x02,
776 TMessageType::Exception => 0x03,
777 TMessageType::OneWay => 0x04,
778 }
779 }
780}
781
782impl TryFrom<u8> for TMessageType {
783 type Err = ::Error;
784 fn try_from(b: u8) -> ::Result<Self> {
785 match b {
786 0x01 => Ok(TMessageType::Call),
787 0x02 => Ok(TMessageType::Reply),
788 0x03 => Ok(TMessageType::Exception),
789 0x04 => Ok(TMessageType::OneWay),
790 unkn => {
Allen George0e22c362017-01-30 07:15:00 -0500791 Err(
792 ::Error::Protocol(
793 ProtocolError {
794 kind: ProtocolErrorKind::InvalidData,
795 message: format!("cannot convert {} to TMessageType", unkn),
796 },
797 ),
798 )
Allen George8b96bfb2016-11-02 08:01:08 -0400799 }
800 }
801 }
802}
803
804/// Thrift struct-field types.
805#[derive(Clone, Copy, Debug, Eq, PartialEq)]
806pub enum TType {
807 /// Indicates that there are no more serialized fields in this Thrift struct.
808 Stop,
809 /// Void (`()`) field.
810 Void,
811 /// Boolean.
812 Bool,
813 /// Signed 8-bit int.
814 I08,
815 /// Double-precision number.
816 Double,
817 /// Signed 16-bit int.
818 I16,
819 /// Signed 32-bit int.
820 I32,
821 /// Signed 64-bit int.
822 I64,
823 /// UTF-8 string.
824 String,
825 /// UTF-7 string. *Unsupported*.
826 Utf7,
827 /// Thrift struct.
828 Struct,
829 /// Map.
830 Map,
831 /// Set.
832 Set,
833 /// List.
834 List,
835 /// UTF-8 string.
836 Utf8,
837 /// UTF-16 string. *Unsupported*.
838 Utf16,
839}
840
841impl Display for TType {
842 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
843 match *self {
844 TType::Stop => write!(f, "STOP"),
845 TType::Void => write!(f, "void"),
846 TType::Bool => write!(f, "bool"),
847 TType::I08 => write!(f, "i08"),
848 TType::Double => write!(f, "double"),
849 TType::I16 => write!(f, "i16"),
850 TType::I32 => write!(f, "i32"),
851 TType::I64 => write!(f, "i64"),
852 TType::String => write!(f, "string"),
853 TType::Utf7 => write!(f, "UTF7"),
854 TType::Struct => write!(f, "struct"),
855 TType::Map => write!(f, "map"),
856 TType::Set => write!(f, "set"),
857 TType::List => write!(f, "list"),
858 TType::Utf8 => write!(f, "UTF8"),
859 TType::Utf16 => write!(f, "UTF16"),
860 }
861 }
862}
863
864/// Compare the expected message sequence number `expected` with the received
865/// message sequence number `actual`.
866///
867/// Return `()` if `actual == expected`, `Err` otherwise.
868pub fn verify_expected_sequence_number(expected: i32, actual: i32) -> ::Result<()> {
869 if expected == actual {
870 Ok(())
871 } else {
Allen George0e22c362017-01-30 07:15:00 -0500872 Err(
873 ::Error::Application(
874 ::ApplicationError {
875 kind: ::ApplicationErrorKind::BadSequenceId,
876 message: format!("expected {} got {}", expected, actual),
877 },
878 ),
879 )
Allen George8b96bfb2016-11-02 08:01:08 -0400880 }
881}
882
883/// Compare the expected service-call name `expected` with the received
884/// service-call name `actual`.
885///
886/// Return `()` if `actual == expected`, `Err` otherwise.
887pub fn verify_expected_service_call(expected: &str, actual: &str) -> ::Result<()> {
888 if expected == actual {
889 Ok(())
890 } else {
Allen George0e22c362017-01-30 07:15:00 -0500891 Err(
892 ::Error::Application(
893 ::ApplicationError {
894 kind: ::ApplicationErrorKind::WrongMethodName,
895 message: format!("expected {} got {}", expected, actual),
896 },
897 ),
898 )
Allen George8b96bfb2016-11-02 08:01:08 -0400899 }
900}
901
902/// Compare the expected message type `expected` with the received message type
903/// `actual`.
904///
905/// Return `()` if `actual == expected`, `Err` otherwise.
906pub fn verify_expected_message_type(expected: TMessageType, actual: TMessageType) -> ::Result<()> {
907 if expected == actual {
908 Ok(())
909 } else {
Allen George0e22c362017-01-30 07:15:00 -0500910 Err(
911 ::Error::Application(
912 ::ApplicationError {
913 kind: ::ApplicationErrorKind::InvalidMessageType,
914 message: format!("expected {} got {}", expected, actual),
915 },
916 ),
917 )
Allen George8b96bfb2016-11-02 08:01:08 -0400918 }
919}
920
921/// Check if a required Thrift struct field exists.
922///
923/// Return `()` if it does, `Err` otherwise.
924pub fn verify_required_field_exists<T>(field_name: &str, field: &Option<T>) -> ::Result<()> {
925 match *field {
926 Some(_) => Ok(()),
927 None => {
Allen George0e22c362017-01-30 07:15:00 -0500928 Err(
929 ::Error::Protocol(
930 ::ProtocolError {
931 kind: ::ProtocolErrorKind::Unknown,
932 message: format!("missing required field {}", field_name),
933 },
934 ),
935 )
Allen George8b96bfb2016-11-02 08:01:08 -0400936 }
937 }
938}
939
940/// Extract the field id from a Thrift field identifier.
941///
942/// `field_ident` must *not* have `TFieldIdentifier.field_type` of type `TType::Stop`.
943///
944/// Return `TFieldIdentifier.id` if an id exists, `Err` otherwise.
945pub fn field_id(field_ident: &TFieldIdentifier) -> ::Result<i16> {
Allen George0e22c362017-01-30 07:15:00 -0500946 field_ident
947 .id
948 .ok_or_else(
949 || {
950 ::Error::Protocol(
951 ::ProtocolError {
952 kind: ::ProtocolErrorKind::Unknown,
953 message: format!("missing field in in {:?}", field_ident),
954 },
955 )
956 },
957 )
958}
959
960#[cfg(test)]
961mod tests {
962
963 use std::io::Cursor;
964
965 use super::*;
966 use transport::{TReadTransport, TWriteTransport};
967
968 #[test]
969 fn must_create_usable_input_protocol_from_concrete_input_protocol() {
970 let r: Box<TReadTransport> = Box::new(Cursor::new([0, 1, 2]));
971 let mut t = TCompactInputProtocol::new(r);
972 takes_input_protocol(&mut t)
973 }
974
975 #[test]
976 fn must_create_usable_input_protocol_from_boxed_input() {
977 let r: Box<TReadTransport> = Box::new(Cursor::new([0, 1, 2]));
978 let mut t: Box<TInputProtocol> = Box::new(TCompactInputProtocol::new(r));
979 takes_input_protocol(&mut t)
980 }
981
982 #[test]
983 fn must_create_usable_output_protocol_from_concrete_output_protocol() {
984 let w: Box<TWriteTransport> = Box::new(vec![0u8; 10]);
985 let mut t = TCompactOutputProtocol::new(w);
986 takes_output_protocol(&mut t)
987 }
988
989 #[test]
990 fn must_create_usable_output_protocol_from_boxed_output() {
991 let w: Box<TWriteTransport> = Box::new(vec![0u8; 10]);
992 let mut t: Box<TOutputProtocol> = Box::new(TCompactOutputProtocol::new(w));
993 takes_output_protocol(&mut t)
994 }
995
996 fn takes_input_protocol<R>(t: &mut R)
997 where
998 R: TInputProtocol,
999 {
1000 t.read_byte().unwrap();
1001 }
1002
1003 fn takes_output_protocol<W>(t: &mut W)
1004 where
1005 W: TOutputProtocol,
1006 {
1007 t.flush().unwrap();
1008 }
Allen George8b96bfb2016-11-02 08:01:08 -04001009}