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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use async_trait::async_trait;

use crate::thrift::{Error, ProtocolError, ProtocolErrorKind, Result};

use super::*;

#[async_trait]
pub trait TInputStreamProtocol: Send {
    /// Read the beginning of a Thrift message.
    async fn read_message_begin(&mut self) -> Result<TMessageIdentifier>;
    /// Read the end of a Thrift message.
    async fn read_message_end(&mut self) -> Result<()>;
    /// Read the beginning of a Thrift struct.
    async fn read_struct_begin(&mut self) -> Result<Option<TStructIdentifier>>;
    /// Read the end of a Thrift struct.
    async fn read_struct_end(&mut self) -> Result<()>;
    /// Read the beginning of a Thrift struct field.
    async fn read_field_begin(&mut self) -> Result<TFieldIdentifier>;
    /// Read the end of a Thrift struct field.
    async fn read_field_end(&mut self) -> Result<()>;
    /// Read a bool.
    async fn read_bool(&mut self) -> Result<bool>;
    /// Read a fixed-length byte array.
    async fn read_bytes(&mut self) -> Result<Vec<u8>>;
    /// Read a word.
    async fn read_i8(&mut self) -> Result<i8>;
    /// Read a 16-bit signed integer.
    async fn read_i16(&mut self) -> Result<i16>;
    /// Read a 32-bit signed integer.
    async fn read_i32(&mut self) -> Result<i32>;
    /// Read a 64-bit signed integer.
    async fn read_i64(&mut self) -> Result<i64>;
    /// Read a 64-bit float.
    async fn read_double(&mut self) -> Result<f64>;
    /// Read a fixed-length string (not null terminated).
    async fn read_string(&mut self) -> Result<String>;
    /// Read the beginning of a list.
    async fn read_list_begin(&mut self) -> Result<TListIdentifier>;
    /// Read the end of a list.
    async fn read_list_end(&mut self) -> Result<()>;
    /// Read the beginning of a set.
    async fn read_set_begin(&mut self) -> Result<TSetIdentifier>;
    /// Read the end of a set.
    async fn read_set_end(&mut self) -> Result<()>;
    /// Read the beginning of a map.
    async fn read_map_begin(&mut self) -> Result<TMapIdentifier>;
    /// Read the end of a map.
    async fn read_map_end(&mut self) -> Result<()>;

    /// Skip a field with type `field_type` recursively until the default
    /// maximum skip depth is reached.
    async fn skip(&mut self, field_type: TType) -> Result<()> {
        self.skip_till_depth(field_type, MAXIMUM_SKIP_DEPTH).await
    }

    /// Skip a field with type `field_type` recursively up to `depth` levels.
    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),
            })),
        }
    }

    // utility (DO NOT USE IN GENERATED CODE!!!!)
    //

    /// Read an unsigned byte.
    ///
    /// This method should **never** be used in generated code.
    async fn read_byte(&mut self) -> Result<u8>;
}

#[async_trait]
pub trait TOutputStreamProtocol: Send {
    /// Write the beginning of a Thrift message.
    async fn write_message_begin(&mut self, identifier: &TMessageIdentifier) -> Result<usize>;
    /// Write the end of a Thrift message.
    async fn write_message_end(&mut self) -> Result<usize>;
    /// Write the beginning of a Thrift struct.
    async fn write_struct_begin(&mut self, identifier: &TStructIdentifier) -> Result<usize>;
    /// Write the end of a Thrift struct.
    fn write_struct_end(&mut self) -> Result<usize>;
    /// Write the beginning of a Thrift field.
    async fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> Result<usize>;
    /// Write the end of a Thrift field.
    fn write_field_end(&mut self) -> Result<usize>;
    /// Write a STOP field indicating that all the fields in a struct have been
    /// written.
    async fn write_field_stop(&mut self) -> Result<usize>;
    /// Write a bool.
    async fn write_bool(&mut self, b: bool) -> Result<usize>;
    /// Write a fixed-length byte array.
    async fn write_bytes(&mut self, b: &[u8]) -> Result<usize>;
    /// Write an 8-bit signed integer.
    async fn write_i8(&mut self, i: i8) -> Result<usize>;
    /// Write a 16-bit signed integer.
    async fn write_i16(&mut self, i: i16) -> Result<usize>;
    /// Write a 32-bit signed integer.
    async fn write_i32(&mut self, i: i32) -> Result<usize>;
    /// Write a 64-bit signed integer.
    async fn write_i64(&mut self, i: i64) -> Result<usize>;
    /// Write a 64-bit float.
    async fn write_double(&mut self, d: f64) -> Result<usize>;
    /// Write a fixed-length string.
    async fn write_string(&mut self, s: &str) -> Result<usize>;
    /// Write the beginning of a list.
    async fn write_list_begin(&mut self, identifier: &TListIdentifier) -> Result<usize>;
    /// Write the end of a list.
    async fn write_list_end(&mut self) -> Result<usize>;
    /// Write the beginning of a set.
    async fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> Result<usize>;
    /// Write the end of a set.
    async fn write_set_end(&mut self) -> Result<usize>;
    /// Write the beginning of a map.
    async fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> Result<usize>;
    /// Write the end of a map.
    async fn write_map_end(&mut self) -> Result<usize>;
    /// Flush buffered bytes to the underlying transport.
    async fn flush(&mut self) -> Result<()>;

    // utility (DO NOT USE IN GENERATED CODE!!!!)
    //

    /// Write an unsigned byte.
    ///
    /// This method should **never** be used in generated code.
    async fn write_byte(&mut self, b: u8) -> Result<usize>; // FIXME: REMOVE
}