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