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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use crate::wire::*;
/// A "on the wire" protobuf value reference that can be furhter
/// interpreted into a "proto type" based on it's definition.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub enum WireValueRef<'a> {
/// Variable sized integer used for `int32`,
/// `int64`, `uint32`, `uint64`, `sint32`, `sint64`,
/// `bool` and `enum`.
VarInt(WireVarInt),
/// Fixed size 64bit value used for `fixed64`,
/// `sfixed64` and `double`.
I64(WireI64),
/// LEN wire type that can be interpred as `string`, `bytes`,
/// "embedded messages" and "packed repeated fields" Prototype
/// types.
Len(WireLenRef<'a>),
SGroup,
EGroup,
/// Fixed size 32bit value used for `fixed32`,
/// `sfixed32` and `float`.
I32(WireI32),
}
impl<'a> WireValueRef<'a> {
/// Return the type of wire value (e.g. [`WireValueRef::I64`] will return
/// [`WireType::I64`]).
pub fn write_type(&self) -> WireType {
match &self {
WireValueRef::VarInt(_) => WireType::VarInt,
WireValueRef::I64(_) => WireType::I64,
WireValueRef::Len(_) => WireType::Len,
WireValueRef::SGroup => WireType::SGroup,
WireValueRef::EGroup => WireType::EGroup,
WireValueRef::I32(_) => WireType::I32,
}
}
/// Try to interpret the value as a Protobuf `bool` (varint).
///
/// If the value is not a [`WireValueRef::VarInt`] or if the
/// [`WireVarInt`] contains a value that is bigger then
/// 1 `None` is returned instead.
#[inline]
pub fn try_as_bool(&self) -> Result<bool, WireValueIntoError> {
use WireValueIntoError::*;
if let WireValueRef::VarInt(v) = &self {
v.try_as_bool().ok_or(VarIntValueTooBigFor32Bit(v.raw()))
} else {
Err(UnexpectedType {
expected: WireType::VarInt,
actual: self.write_type(),
})
}
}
/// Try to interpret the value as a Protobuf `uint32` (varint).
///
/// If the value is not a [`WireValueRef::VarInt`] or if the
/// [`WireVarInt`] contains a value that is bigger then
/// 32 bits `None` is returned instead.
#[inline]
pub fn try_as_uint32(&self) -> Result<u32, WireValueIntoError> {
use WireValueIntoError::*;
if let WireValueRef::VarInt(v) = &self {
v.try_as_uint32().ok_or(VarIntValueTooBigFor32Bit(v.raw()))
} else {
Err(UnexpectedType {
expected: WireType::VarInt,
actual: self.write_type(),
})
}
}
/// Try to interpret the value as a Protobuf `int32` (varint,
/// two's complement encoding).
///
/// If the value is not a [`WireValueRef::VarInt`] or if the
/// [`WireVarInt`] contains a value that is bigger then
/// 32 bits `None` is returned instead.
#[inline]
pub fn try_as_int32(&self) -> Result<i32, WireValueIntoError> {
use WireValueIntoError::*;
if let WireValueRef::VarInt(v) = &self {
v.try_as_int32().ok_or(VarIntValueTooBigFor32Bit(v.raw()))
} else {
Err(UnexpectedType {
expected: WireType::VarInt,
actual: self.write_type(),
})
}
}
/// Try to interpret the value as a Protobuf `sint32` (varint,
/// ZigZag encoding).
///
/// If the value is not a [`WireValueRef::VarInt`] or if the
/// [`WireVarInt`] contains a value that is bigger then
/// 32 bits `None` is returned instead.
#[inline]
pub fn try_as_sint32(&self) -> Result<i32, WireValueIntoError> {
use WireValueIntoError::*;
if let WireValueRef::VarInt(v) = &self {
v.try_as_sint32().ok_or(VarIntValueTooBigFor32Bit(v.raw()))
} else {
Err(UnexpectedType {
expected: WireType::VarInt,
actual: self.write_type(),
})
}
}
/// Try to interpret the value as a Protobuf `uint64` (varint).
///
/// If the value is not a [`WireValueRef::VarInt`] or if the
/// [`WireVarInt`] contains a value that is bigger then
/// 32 bits `None` is returned instead.
#[inline]
pub fn try_as_uint64(&self) -> Result<u64, WireValueIntoError> {
use WireValueIntoError::*;
if let WireValueRef::VarInt(v) = &self {
Ok(v.as_uint64())
} else {
Err(UnexpectedType {
expected: WireType::VarInt,
actual: self.write_type(),
})
}
}
/// Try to interpret the value as a Protobuf `int64` (varint,
/// two's complement encoding).
///
/// If the value is not a [`WireValueRef::VarInt`] or if the
/// [`WireVarInt`] contains a value that is bigger then
/// 64 bits `None` is returned instead.
#[inline]
pub fn try_as_int64(&self) -> Result<i64, WireValueIntoError> {
use WireValueIntoError::*;
if let WireValueRef::VarInt(v) = &self {
Ok(v.as_int64())
} else {
Err(UnexpectedType {
expected: WireType::VarInt,
actual: self.write_type(),
})
}
}
/// Try to interpret the value as a Protobuf `sint64` (varint,
/// ZigZag encoding).
///
/// If the value is not a [`WireValueRef::VarInt`] or if the
/// [`WireVarInt`] contains a value that is bigger then
/// 64 bits `None` is returned instead.
#[inline]
pub fn try_as_sint64(&self) -> Result<i64, WireValueIntoError> {
use WireValueIntoError::*;
if let WireValueRef::VarInt(v) = &self {
Ok(v.as_sint64())
} else {
Err(UnexpectedType {
expected: WireType::VarInt,
actual: self.write_type(),
})
}
}
/// Try to interpret the value as a Protobuf `fixed32`.
///
/// If the value is not a [`WireValueRef::I32`] `None` is
/// returned instead.
#[inline]
pub fn as_fixed32(&self) -> Result<u32, WireValueIntoError> {
use WireValueIntoError::*;
if let WireValueRef::I32(v) = &self {
Ok(v.as_fixed32())
} else {
Err(UnexpectedType {
expected: WireType::VarInt,
actual: self.write_type(),
})
}
}
/// Try to interpret the value as a Protobuf `sfixed32`.
///
/// If the value is not a [`WireValueRef::I32`] `None` is
/// returned instead.
#[inline]
pub fn as_sfixed32(&self) -> Result<i32, WireValueIntoError> {
use WireValueIntoError::*;
if let WireValueRef::I32(v) = &self {
Ok(v.as_sfixed32())
} else {
Err(UnexpectedType {
expected: WireType::VarInt,
actual: self.write_type(),
})
}
}
/// Try to interpret the value as a Protobuf `float`.
///
/// If the value is not a [`WireValueRef::I32`] `None` is
/// returned instead.
#[inline]
pub fn as_float(&self) -> Result<f32, WireValueIntoError> {
use WireValueIntoError::*;
if let WireValueRef::I32(v) = &self {
Ok(v.as_float())
} else {
Err(UnexpectedType {
expected: WireType::VarInt,
actual: self.write_type(),
})
}
}
/// Try to interpret the value as a Protobuf `fixed64`.
///
/// If the value is not a [`WireValueRef::I64`] `None` is
/// returned instead.
#[inline]
pub fn as_fixed64(&self) -> Result<u64, WireValueIntoError> {
use WireValueIntoError::*;
if let WireValueRef::I64(v) = &self {
Ok(v.as_fixed64())
} else {
Err(UnexpectedType {
expected: WireType::VarInt,
actual: self.write_type(),
})
}
}
/// Try to interpret the value as a Protobuf `sfixed64`.
///
/// If the value is not a [`WireValueRef::I64`] `None` is
/// returned instead.
#[inline]
pub fn as_sfixed64(&self) -> Result<i64, WireValueIntoError> {
use WireValueIntoError::*;
if let WireValueRef::I64(v) = &self {
Ok(v.as_sfixed64())
} else {
Err(UnexpectedType {
expected: WireType::VarInt,
actual: self.write_type(),
})
}
}
/// Try to interpret the value as a Protobuf `double`.
///
/// If the value is not a [`WireValueRef::I64`] `None` is
/// returned instead.
#[inline]
pub fn as_double(&self) -> Result<f64, WireValueIntoError> {
use WireValueIntoError::*;
if let WireValueRef::I64(v) = &self {
Ok(v.as_double())
} else {
Err(UnexpectedType {
expected: WireType::VarInt,
actual: self.write_type(),
})
}
}
}
/// Error when it is not possible to convert a [`WireValueRef`]
/// to a specific Protobuf type.
#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub enum WireValueIntoError {
/// Error if a [`WireValueRef`] is not [`WireValueRef::VarInt`] even though
/// it was expected to be one.
UnexpectedType {
expected: WireType,
actual: WireType,
},
/// Error if the value is too big for a 32 bit value.
VarIntValueTooBigFor32Bit(u64),
}