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