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
use std::error::Error;
use std::borrow::Borrow;
use std::ops::RangeBounds;
use std::collections::BTreeMap;
use std::cmp;
use buf::{ByteBuf, ByteBufMut};
pub mod types;
pub mod buf;
pub type StrBytes = string::String<bytes::Bytes>;
#[derive(Debug)]
pub struct DecodeError;
#[derive(Debug)]
pub struct EncodeError;
impl<E: Error> From<E> for DecodeError {
fn from(other: E) -> Self {
error!("{}", other);
DecodeError
}
}
pub(crate) trait NewType<Inner>: From<Inner> + Into<Inner> + Borrow<Inner> {}
impl<T> NewType<T> for T {}
pub(crate) trait Encoder<Value> {
fn encode<B: ByteBufMut>(&self, buf: &mut B, value: Value) -> Result<(), EncodeError>;
fn compute_size(&self, value: Value) -> Result<usize, EncodeError>;
fn fixed_size(&self) -> Option<usize> {
None
}
}
pub(crate) trait Decoder<Value> {
fn decode<B: ByteBuf>(&self, buf: &mut B) -> Result<Value, DecodeError>;
}
#[derive(Debug, Copy, Clone)]
pub struct VersionRange {
pub min: i16,
pub max: i16,
}
impl VersionRange {
pub fn is_empty(&self) -> bool {
self.min > self.max
}
pub fn intersect(&self, other: &VersionRange) -> VersionRange {
VersionRange {
min: cmp::max(self.min, other.min),
max: cmp::min(self.max, other.max),
}
}
}
pub trait Message: Sized {
const VERSIONS: VersionRange;
}
pub trait Encodable: Sized {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<(), EncodeError>;
fn compute_size(&self, version: i16) -> Result<usize, EncodeError>;
}
pub trait Decodable: Sized {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self, DecodeError>;
}
pub(crate) trait MapEncodable: Sized {
type Key;
fn encode<B: ByteBufMut>(&self, key: &Self::Key, buf: &mut B, version: i16) -> Result<(), EncodeError>;
fn compute_size(&self, key: &Self::Key, version: i16) -> Result<usize, EncodeError>;
}
pub(crate) trait MapDecodable: Sized {
type Key;
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<(Self::Key, Self), DecodeError>;
}
pub trait HeaderVersion {
fn header_version(version: i16) -> i16;
}
pub trait Request: Message + Encodable + Decodable + HeaderVersion {
const KEY: i16;
type Response: Message + Encodable + Decodable + HeaderVersion;
}
pub(crate) fn write_unknown_tagged_fields<B: ByteBufMut, R: RangeBounds<i32>>(
buf: &mut B,
range: R,
unknown_tagged_fields: &BTreeMap<i32, Vec<u8>>,
) -> Result<(), EncodeError> {
for (&k, v) in unknown_tagged_fields.range(range) {
if v.len() > std::u32::MAX as usize {
error!("Tagged field is too long to encode ({} bytes)", v.len());
return Err(EncodeError);
}
types::UnsignedVarInt.encode(buf, k as u32)?;
types::UnsignedVarInt.encode(buf, v.len() as u32)?;
buf.put_slice(v.as_slice());
}
Ok(())
}
pub(crate) fn compute_unknown_tagged_fields_size(
unknown_tagged_fields: &BTreeMap<i32, Vec<u8>>,
) -> Result<usize, EncodeError> {
let mut total_size = 0;
for (&k, v) in unknown_tagged_fields {
if v.len() > std::u32::MAX as usize {
error!("Tagged field is too long to encode ({} bytes)", v.len());
return Err(EncodeError);
}
total_size += types::UnsignedVarInt.compute_size(k as u32)?;
total_size += types::UnsignedVarInt.compute_size(v.len() as u32)?;
total_size += v.len();
}
Ok(total_size)
}