1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use async_trait::async_trait;
use crate::thrift::{Error, ProtocolError, ProtocolErrorKind, Result};
use super::*;
#[async_trait]
pub trait TInputStreamProtocol: Send {
async fn read_message_begin(&mut self) -> Result<TMessageIdentifier>;
async fn read_message_end(&mut self) -> Result<()>;
async fn read_struct_begin(&mut self) -> Result<Option<TStructIdentifier>>;
async fn read_struct_end(&mut self) -> Result<()>;
async fn read_field_begin(&mut self) -> Result<TFieldIdentifier>;
async fn read_field_end(&mut self) -> Result<()>;
async fn read_bool(&mut self) -> Result<bool>;
async fn read_bytes(&mut self) -> Result<Vec<u8>>;
async fn read_i8(&mut self) -> Result<i8>;
async fn read_i16(&mut self) -> Result<i16>;
async fn read_i32(&mut self) -> Result<i32>;
async fn read_i64(&mut self) -> Result<i64>;
async fn read_double(&mut self) -> Result<f64>;
async fn read_string(&mut self) -> Result<String>;
async fn read_list_begin(&mut self) -> Result<TListIdentifier>;
async fn read_list_end(&mut self) -> Result<()>;
async fn read_set_begin(&mut self) -> Result<TSetIdentifier>;
async fn read_set_end(&mut self) -> Result<()>;
async fn read_map_begin(&mut self) -> Result<TMapIdentifier>;
async fn read_map_end(&mut self) -> Result<()>;
async fn skip(&mut self, field_type: TType) -> Result<()> {
self.skip_till_depth(field_type, MAXIMUM_SKIP_DEPTH).await
}
async fn skip_till_depth(&mut self, field_type: TType, depth: i8) -> Result<()> {
if depth == 0 {
return Err(Error::Protocol(ProtocolError {
kind: ProtocolErrorKind::DepthLimit,
message: format!("cannot parse past {:?}", field_type),
}));
}
match field_type {
TType::Bool => self.read_bool().await.map(|_| ()),
TType::I08 => self.read_i8().await.map(|_| ()),
TType::I16 => self.read_i16().await.map(|_| ()),
TType::I32 => self.read_i32().await.map(|_| ()),
TType::I64 => self.read_i64().await.map(|_| ()),
TType::Double => self.read_double().await.map(|_| ()),
TType::String => self.read_string().await.map(|_| ()),
TType::Struct => {
self.read_struct_begin().await?;
loop {
let field_ident = self.read_field_begin().await?;
if field_ident.field_type == TType::Stop {
break;
}
self.skip_till_depth(field_ident.field_type, depth - 1)
.await?;
}
self.read_struct_end().await
}
TType::List => {
let list_ident = self.read_list_begin().await?;
for _ in 0..list_ident.size {
self.skip_till_depth(list_ident.element_type, depth - 1)
.await?;
}
self.read_list_end().await
}
TType::Set => {
let set_ident = self.read_set_begin().await?;
for _ in 0..set_ident.size {
self.skip_till_depth(set_ident.element_type, depth - 1)
.await?;
}
self.read_set_end().await
}
TType::Map => {
let map_ident = self.read_map_begin().await?;
for _ in 0..map_ident.size {
let key_type = map_ident
.key_type
.expect("non-zero sized map should contain key type");
let val_type = map_ident
.value_type
.expect("non-zero sized map should contain value type");
self.skip_till_depth(key_type, depth - 1).await?;
self.skip_till_depth(val_type, depth - 1).await?;
}
self.read_map_end().await
}
u => Err(Error::Protocol(ProtocolError {
kind: ProtocolErrorKind::Unknown,
message: format!("cannot skip field type {:?}", &u),
})),
}
}
async fn read_byte(&mut self) -> Result<u8>;
}
#[async_trait]
pub trait TOutputStreamProtocol: Send {
async fn write_message_begin(&mut self, identifier: &TMessageIdentifier) -> Result<usize>;
async fn write_message_end(&mut self) -> Result<usize>;
async fn write_struct_begin(&mut self, identifier: &TStructIdentifier) -> Result<usize>;
fn write_struct_end(&mut self) -> Result<usize>;
async fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> Result<usize>;
fn write_field_end(&mut self) -> Result<usize>;
async fn write_field_stop(&mut self) -> Result<usize>;
async fn write_bool(&mut self, b: bool) -> Result<usize>;
async fn write_bytes(&mut self, b: &[u8]) -> Result<usize>;
async fn write_i8(&mut self, i: i8) -> Result<usize>;
async fn write_i16(&mut self, i: i16) -> Result<usize>;
async fn write_i32(&mut self, i: i32) -> Result<usize>;
async fn write_i64(&mut self, i: i64) -> Result<usize>;
async fn write_double(&mut self, d: f64) -> Result<usize>;
async fn write_string(&mut self, s: &str) -> Result<usize>;
async fn write_list_begin(&mut self, identifier: &TListIdentifier) -> Result<usize>;
async fn write_list_end(&mut self) -> Result<usize>;
async fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> Result<usize>;
async fn write_set_end(&mut self) -> Result<usize>;
async fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> Result<usize>;
async fn write_map_end(&mut self) -> Result<usize>;
async fn flush(&mut self) -> Result<()>;
async fn write_byte(&mut self, b: u8) -> Result<usize>;
}