18
18
enum WireType {
19
19
/// The Varint WireType indicates the value is a single VARINT.
20
20
Varint ,
21
- /// The I64 WireType indicates that the value is precisely 8 bytes in
22
- /// little-endian order containing a 64-bit signed integer or double type.
21
+ // The I64 WireType indicates that the value is precisely 8 bytes in
22
+ // little-endian order containing a 64-bit signed integer or double type.
23
23
//I64, -- not needed for this exercise
24
24
/// The Len WireType indicates that the value is a length represented as a
25
25
/// VARINT followed by exactly that number of bytes.
@@ -195,6 +195,34 @@ impl<'a> ProtoMessage<'a> for PhoneNumber<'a> {
195
195
196
196
// ANCHOR: main
197
197
fn main ( ) {
198
+ let person_id: Person = parse_message ( & [ 0x10 , 0x2a ] ) ;
199
+ assert_eq ! ( person_id, Person { name: "" , id: 42 , phone: vec![ ] } ) ;
200
+
201
+ let person_name: Person = parse_message ( & [
202
+ 0x0a , 0x0e , 0x62 , 0x65 , 0x61 , 0x75 , 0x74 , 0x69 , 0x66 , 0x75 , 0x6c , 0x20 ,
203
+ 0x6e , 0x61 , 0x6d , 0x65 ,
204
+ ] ) ;
205
+ assert_eq ! ( person_name, Person { name: "beautiful name" , id: 0 , phone: vec![ ] } ) ;
206
+
207
+ let person_name_id: Person =
208
+ parse_message ( & [ 0x0a , 0x04 , 0x45 , 0x76 , 0x61 , 0x6e , 0x10 , 0x16 ] ) ;
209
+ assert_eq ! ( person_name_id, Person { name: "Evan" , id: 22 , phone: vec![ ] } ) ;
210
+
211
+ let phone: Person = parse_message ( & [
212
+ 0x0a , 0x00 , 0x10 , 0x00 , 0x1a , 0x16 , 0x0a , 0x0e , 0x2b , 0x31 , 0x32 , 0x33 ,
213
+ 0x34 , 0x2d , 0x37 , 0x37 , 0x37 , 0x2d , 0x39 , 0x30 , 0x39 , 0x30 , 0x12 , 0x04 ,
214
+ 0x68 , 0x6f , 0x6d , 0x65 ,
215
+ ] ) ;
216
+ assert_eq ! (
217
+ phone,
218
+ Person {
219
+ name: "" ,
220
+ id: 0 ,
221
+ phone: vec![ PhoneNumber { number: "+1234-777-9090" , type_: "home" } , ] ,
222
+ }
223
+ ) ;
224
+
225
+ // Put that all together into a single parse.
198
226
let person: Person = parse_message ( & [
199
227
0x0a , 0x07 , 0x6d , 0x61 , 0x78 , 0x77 , 0x65 , 0x6c , 0x6c , 0x10 , 0x2a , 0x1a ,
200
228
0x16 , 0x0a , 0x0e , 0x2b , 0x31 , 0x32 , 0x30 , 0x32 , 0x2d , 0x35 , 0x35 , 0x35 ,
@@ -203,53 +231,16 @@ fn main() {
203
231
0x2d , 0x35 , 0x33 , 0x30 , 0x38 , 0x12 , 0x06 , 0x6d , 0x6f , 0x62 , 0x69 , 0x6c ,
204
232
0x65 ,
205
233
] ) ;
206
- println ! ( "{:#?}" , person) ;
234
+ assert_eq ! (
235
+ person,
236
+ Person {
237
+ name: "maxwell" ,
238
+ id: 42 ,
239
+ phone: vec![
240
+ PhoneNumber { number: "+1202-555-1212" , type_: "home" } ,
241
+ PhoneNumber { number: "+1800-867-5308" , type_: "mobile" } ,
242
+ ]
243
+ }
244
+ ) ;
207
245
}
208
246
// ANCHOR_END: main
209
-
210
- #[ cfg( test) ]
211
- mod tests {
212
- use super :: * ;
213
-
214
- #[ test]
215
- fn test_id ( ) {
216
- let person_id: Person = parse_message ( & [ 0x10 , 0x2a ] ) ;
217
- assert_eq ! ( person_id, Person { name: "" , id: 42 , phone: vec![ ] } ) ;
218
- }
219
-
220
- #[ test]
221
- fn test_name ( ) {
222
- let person_name: Person = parse_message ( & [
223
- 0x0a , 0x0e , 0x62 , 0x65 , 0x61 , 0x75 , 0x74 , 0x69 , 0x66 , 0x75 , 0x6c , 0x20 ,
224
- 0x6e , 0x61 , 0x6d , 0x65 ,
225
- ] ) ;
226
- assert_eq ! (
227
- person_name,
228
- Person { name: "beautiful name" , id: 0 , phone: vec![ ] }
229
- ) ;
230
- }
231
-
232
- #[ test]
233
- fn test_just_person ( ) {
234
- let person_name_id: Person =
235
- parse_message ( & [ 0x0a , 0x04 , 0x45 , 0x76 , 0x61 , 0x6e , 0x10 , 0x16 ] ) ;
236
- assert_eq ! ( person_name_id, Person { name: "Evan" , id: 22 , phone: vec![ ] } ) ;
237
- }
238
-
239
- #[ test]
240
- fn test_phone ( ) {
241
- let phone: Person = parse_message ( & [
242
- 0x0a , 0x00 , 0x10 , 0x00 , 0x1a , 0x16 , 0x0a , 0x0e , 0x2b , 0x31 , 0x32 , 0x33 ,
243
- 0x34 , 0x2d , 0x37 , 0x37 , 0x37 , 0x2d , 0x39 , 0x30 , 0x39 , 0x30 , 0x12 , 0x04 ,
244
- 0x68 , 0x6f , 0x6d , 0x65 ,
245
- ] ) ;
246
- assert_eq ! (
247
- phone,
248
- Person {
249
- name: "" ,
250
- id: 0 ,
251
- phone: vec![ PhoneNumber { number: "+1234-777-9090" , type_: "home" } , ] ,
252
- }
253
- ) ;
254
- }
255
- }
0 commit comments