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
204
205
206
207
208
209
use crate::errors::ProtocolError;
use crate::Result;
use bytes::Bytes;
use ordered_float::OrderedFloat;
use std::{
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
convert::TryFrom,
hash::Hash,
};
#[derive(PartialEq, Copy, Clone, Debug)]
#[repr(u8)]
pub enum TType {
Stop = 0,
Void = 1,
Bool = 2,
Byte = 3,
Double = 4,
I16 = 6,
I32 = 8,
I64 = 10,
String = 11,
Struct = 12,
Map = 13,
Set = 14,
List = 15,
UTF8 = 16,
UTF16 = 17,
Stream = 18,
Float = 19,
}
impl TryFrom<i8> for TType {
type Error = anyhow::Error;
fn try_from(val: i8) -> Result<Self> {
let ret = match val {
0 => TType::Stop,
1 => TType::Void,
2 => TType::Bool,
3 => TType::Byte,
4 => TType::Double,
6 => TType::I16,
8 => TType::I32,
10 => TType::I64,
11 => TType::String,
12 => TType::Struct,
13 => TType::Map,
14 => TType::Set,
15 => TType::List,
16 => TType::UTF8,
17 => TType::UTF16,
18 => TType::Stream,
19 => TType::Float,
_ => bail_err!(ProtocolError::InvalidTypeTag),
};
Ok(ret)
}
}
impl From<TType> for String {
fn from(t: TType) -> String {
let tmp: &str = t.into();
tmp.to_owned()
}
}
impl From<TType> for &'static str {
fn from(t: TType) -> &'static str {
match t {
TType::Stop => "STOP",
TType::Void => "VOID",
TType::Bool => "BOOL",
TType::Byte => "BYTE",
TType::Double => "DOUBLE",
TType::I16 => "I16",
TType::I32 => "I32",
TType::I64 => "I64",
TType::String => "STRING",
TType::Struct => "STRUCT",
TType::Map => "MAP",
TType::Set => "SET",
TType::List => "LIST",
TType::UTF8 => "UTF8",
TType::UTF16 => "UTF16",
TType::Stream => "STREAM",
TType::Float => "FLOAT",
}
}
}
pub trait GetTType {
const TTYPE: TType;
}
impl GetTType for () {
const TTYPE: TType = TType::Void;
}
impl GetTType for bool {
const TTYPE: TType = TType::Bool;
}
impl GetTType for i8 {
const TTYPE: TType = TType::Byte;
}
impl GetTType for i16 {
const TTYPE: TType = TType::I16;
}
impl GetTType for i32 {
const TTYPE: TType = TType::I32;
}
impl GetTType for i64 {
const TTYPE: TType = TType::I64;
}
impl GetTType for f64 {
const TTYPE: TType = TType::Double;
}
impl GetTType for f32 {
const TTYPE: TType = TType::Float;
}
impl GetTType for OrderedFloat<f64> {
const TTYPE: TType = TType::Double;
}
impl GetTType for OrderedFloat<f32> {
const TTYPE: TType = TType::Float;
}
impl GetTType for String {
const TTYPE: TType = TType::String;
}
impl GetTType for Bytes {
const TTYPE: TType = TType::String;
}
impl GetTType for Vec<u8> {
const TTYPE: TType = TType::String;
}
impl<T> GetTType for BTreeSet<T>
where
T: GetTType + Ord,
{
const TTYPE: TType = TType::Set;
}
impl<T, S> GetTType for HashSet<T, S>
where
T: GetTType + Hash + Eq,
S: std::hash::BuildHasher,
{
const TTYPE: TType = TType::Set;
}
impl<K, V> GetTType for BTreeMap<K, V>
where
K: GetTType + Ord,
V: GetTType,
{
const TTYPE: TType = TType::Map;
}
impl<K, V, S> GetTType for HashMap<K, V, S>
where
K: GetTType + Hash + Eq,
V: GetTType,
S: std::hash::BuildHasher,
{
const TTYPE: TType = TType::Map;
}
impl<T> GetTType for Vec<T>
where
T: GetTType,
{
const TTYPE: TType = TType::List;
}