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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
use std::collections;
use protobuf;
use protobuf::wire_format;
use crate::descriptor;
use crate::error;
#[derive(Clone, Debug)]
pub enum Value {
Bool(bool),
I32(i32),
I64(i64),
U32(u32),
U64(u64),
F32(f32),
F64(f64),
Bytes(Vec<u8>),
String(String),
Enum(i32),
Message(Message),
}
#[derive(Clone, Debug)]
pub struct Message {
pub fields: collections::BTreeMap<i32, Field>,
pub unknown: protobuf::UnknownFields,
}
#[derive(Clone, Debug)]
pub enum Field {
Singular(Option<Value>),
Repeated(Vec<Value>),
}
impl Message {
#[inline]
pub fn new(message: &descriptor::MessageDescriptor) -> Message {
let mut m = Message {
fields: collections::BTreeMap::new(),
unknown: protobuf::UnknownFields::new(),
};
for field in message.fields() {
m.fields.insert(
field.number(),
if field.is_repeated() {
Field::Repeated(Vec::new())
} else {
Field::Singular(field.default_value().cloned())
},
);
}
m
}
#[inline]
pub fn merge_from(
&mut self,
descriptors: &descriptor::Descriptors,
message: &descriptor::MessageDescriptor,
input: &mut protobuf::CodedInputStream,
) -> error::Result<()> {
while !input.eof()? {
let (number, wire_type) = input.read_tag_unpack()?;
if let Some(field) = message.field_by_number(number as i32) {
let value = self.ensure_field(field);
value.merge_from(descriptors, field, input, wire_type)?;
} else {
use protobuf::rt::read_unknown_or_skip_group as u;
u(number, wire_type, input, &mut self.unknown)?;
}
}
Ok(())
}
#[inline]
fn ensure_field(&mut self, field: &descriptor::FieldDescriptor) -> &mut Field {
self.fields
.entry(field.number())
.or_insert_with(|| Field::new(field))
}
}
impl Field {
#[inline]
pub fn new(field: &descriptor::FieldDescriptor) -> Field {
if field.is_repeated() {
Field::Repeated(Vec::new())
} else {
Field::Singular(None)
}
}
#[inline]
pub fn merge_from(
&mut self,
descriptors: &descriptor::Descriptors,
field: &descriptor::FieldDescriptor,
input: &mut protobuf::CodedInputStream,
wire_type: protobuf::wire_format::WireType,
) -> error::Result<()> {
use crate::descriptor::FieldType::*;
use protobuf::wire_format::WireType::*;
use protobuf::CodedInputStream as I;
macro_rules! ss {
($expected_wire_type:expr, $visit_func:expr, $reader:expr) => {
self.merge_scalar(input, wire_type, $expected_wire_type, $visit_func, $reader)
};
}
macro_rules! ps {
($expected_wire_type:expr, $visit_func:expr, $reader:expr) => {
self.merge_packable_scalar(
input,
wire_type,
$expected_wire_type,
$visit_func,
$reader,
)
};
($expected_wire_type:expr, $size:expr, $visit_func:expr, $reader:expr) => {
self.merge_packable_scalar(
input,
wire_type,
$expected_wire_type,
$visit_func,
$reader,
)
};
}
match field.field_type(descriptors) {
Bool => ps!(WireTypeVarint, Value::Bool, I::read_bool),
Int32 => ps!(WireTypeVarint, Value::I32, I::read_int32),
Int64 => ps!(WireTypeVarint, Value::I64, I::read_int64),
SInt32 => ps!(WireTypeVarint, Value::I32, I::read_sint32),
SInt64 => ps!(WireTypeVarint, Value::I64, I::read_sint64),
UInt32 => ps!(WireTypeVarint, Value::U32, I::read_uint32),
UInt64 => ps!(WireTypeVarint, Value::U64, I::read_uint64),
Fixed32 => ps!(WireTypeFixed32, 4, Value::U32, I::read_fixed32),
Fixed64 => ps!(WireTypeFixed64, 8, Value::U64, I::read_fixed64),
SFixed32 => ps!(WireTypeFixed32, 4, Value::I32, I::read_sfixed32),
SFixed64 => ps!(WireTypeFixed64, 8, Value::I64, I::read_sfixed64),
Float => ps!(WireTypeFixed32, 4, Value::F32, I::read_float),
Double => ps!(WireTypeFixed64, 8, Value::F64, I::read_double),
Bytes => ss!(WireTypeLengthDelimited, Value::Bytes, I::read_bytes),
String => ss!(WireTypeLengthDelimited, Value::String, I::read_string),
Enum(_) => self.merge_enum(input, wire_type),
Message(ref m) => self.merge_message(input, descriptors, m, wire_type),
Group => unimplemented!(),
UnresolvedEnum(e) => Err(error::Error::UnknownEnum { name: e.to_owned() }),
UnresolvedMessage(m) => Err(error::Error::UnknownMessage { name: m.to_owned() }),
}
}
#[inline]
fn merge_scalar<'a, A, V, R>(
&mut self,
input: &mut protobuf::CodedInputStream<'a>,
actual_wire_type: wire_format::WireType,
expected_wire_type: wire_format::WireType,
value_ctor: V,
reader: R,
) -> error::Result<()>
where
V: Fn(A) -> Value,
R: Fn(&mut protobuf::CodedInputStream<'a>) -> protobuf::ProtobufResult<A>,
{
if expected_wire_type == actual_wire_type {
self.put(value_ctor(reader(input)?));
Ok(())
} else {
Err(error::Error::BadWireType {
wire_type: actual_wire_type,
})
}
}
#[inline]
fn merge_packable_scalar<'a, A, V, R>(
&mut self,
input: &mut protobuf::CodedInputStream<'a>,
actual_wire_type: wire_format::WireType,
expected_wire_type: wire_format::WireType,
value_ctor: V,
reader: R,
) -> error::Result<()>
where
V: Fn(A) -> Value,
R: Fn(&mut protobuf::CodedInputStream<'a>) -> protobuf::ProtobufResult<A>,
{
if wire_format::WireType::WireTypeLengthDelimited == actual_wire_type {
let len = input.read_raw_varint64()?;
let old_limit = input.push_limit(len)?;
while !input.eof()? {
self.put(value_ctor(reader(input)?));
}
input.pop_limit(old_limit);
Ok(())
} else {
self.merge_scalar(
input,
actual_wire_type,
expected_wire_type,
value_ctor,
reader,
)
}
}
#[inline]
fn merge_enum(
&mut self,
input: &mut protobuf::CodedInputStream,
actual_wire_type: wire_format::WireType,
) -> error::Result<()> {
if wire_format::WireType::WireTypeVarint == actual_wire_type {
let v = input.read_raw_varint32()? as i32;
self.put(Value::Enum(v));
Ok(())
} else {
Err(error::Error::BadWireType {
wire_type: actual_wire_type,
})
}
}
#[inline]
fn merge_message(
&mut self,
input: &mut protobuf::CodedInputStream,
descriptors: &descriptor::Descriptors,
message: &descriptor::MessageDescriptor,
actual_wire_type: wire_format::WireType,
) -> error::Result<()> {
if wire_format::WireType::WireTypeLengthDelimited == actual_wire_type {
let len = input.read_raw_varint64()?;
let mut msg = match *self {
Field::Singular(ref mut o) => {
if let Some(Value::Message(m)) = o.take() {
m
} else {
Message::new(message)
}
}
_ => Message::new(message),
};
let old_limit = input.push_limit(len)?;
msg.merge_from(descriptors, message, input)?;
input.pop_limit(old_limit);
self.put(Value::Message(msg));
Ok(())
} else {
Err(error::Error::BadWireType {
wire_type: actual_wire_type,
})
}
}
#[inline]
fn put(&mut self, value: Value) {
match *self {
Field::Singular(ref mut s) => *s = Some(value),
Field::Repeated(ref mut r) => r.push(value),
}
}
}