Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: mojom/generators/rust/templates/struct.tmpl.go

Issue 2222823002: Rust: Add validation support to code generator (Closed) Base URL: git@github.com:domokit/mojo.git@decoding-test
Patch Set: Double check before land Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package templates 5 package templates
6 6
7 const GenerateStruct = ` 7 const GenerateStruct = `
8 {{- /* . (dot) refers to the Go type |rustgen.StructTemplate| */ -}} 8 {{- /* . (dot) refers to the Go type |rustgen.StructTemplate| */ -}}
9 {{- define "GenerateStruct" -}} 9 {{- define "GenerateStruct" -}}
10 {{- $struct := . -}} 10 {{- $struct := . -}}
(...skipping 23 matching lines...) Expand all
34 {{end -}} 34 {{end -}}
35 } 35 }
36 36
37 impl MojomPointer for {{$struct.Name}} { 37 impl MojomPointer for {{$struct.Name}} {
38 fn header_data(&self) -> DataHeaderValue { DataHeaderValue::Version({{$struc t.Version}}) } 38 fn header_data(&self) -> DataHeaderValue { DataHeaderValue::Version({{$struc t.Version}}) }
39 fn serialized_size(&self, _context: &Context) -> usize { {{$struct.Size}} } 39 fn serialized_size(&self, _context: &Context) -> usize { {{$struct.Size}} }
40 fn encode_value(self, encoder: &mut Encoder, context: Context) { 40 fn encode_value(self, encoder: &mut Encoder, context: Context) {
41 {{range $field := $struct.Fields}} MojomEncodable::encode(self.{{$field.N ame}}, encoder, context.clone()); 41 {{range $field := $struct.Fields}} MojomEncodable::encode(self.{{$field.N ame}}, encoder, context.clone());
42 {{end}} 42 {{end}}
43 } 43 }
44 fn decode_value(decoder: &mut Decoder, context: Context) -> Self { 44 fn decode_value(decoder: &mut Decoder, context: Context) -> Result<Self, Val idationError> {
45 // TODO(mknyszek): Validate bytes and version 45 let version = {
46 let (_bytes, version) = {
47 let mut state = decoder.get_mut(&context); 46 let mut state = decoder.get_mut(&context);
48 let bytes = state.decode::<u32>(); 47 match state.decode_struct_header(&{{$struct.Name}}Versions) {
49 let version = state.decode::<u32>(); 48 Ok(header) => header.data(),
50 (bytes, version) 49 Err(err) => return Err(err),
50 }
51 }; 51 };
52 {{range $field := $struct.Fields}} let {{$field.Name}} = {{if ne $field.M inVersion 0 -}} 52 {{range $field := $struct.Fields}} let {{$field.Name}} = {{if ne $field.M inVersion 0 -}}
53 if version >= {{$field.MinVersion}} { 53 if version >= {{$field.MinVersion}} {
54 <{{$field.Type}}>::decode(decoder, context.clone()) 54 match <{{$field.Type}}>::decode(decoder, context.clone()) {
55 Ok(value) => value,
56 Err(err) => return Err(err),
57 }
55 } else { 58 } else {
56 Default::default() 59 Default::default()
57 }; 60 };
58 {{- else -}} 61 {{- else -}}
59 » <{{$field.Type}}>::decode(decoder, context.clone()); 62 match <{{$field.Type}}>::decode(decoder, context.clone()) {
63 Ok(value) => value,
64 Err(err) => return Err(err),
65 };
60 {{- end}} 66 {{- end}}
61 {{end}} {{$struct.Name}} { 67 {{end}} Ok({{$struct.Name}} {
62 {{range $field := $struct.Fields}} {{$field.Name}}: {{$field.Name}}, 68 {{range $field := $struct.Fields}} {{$field.Name}}: {{$field.Name}},
63 {{end}} } 69 {{end}} })
64 } 70 }
65 } 71 }
66 72
67 impl MojomEncodable for {{$struct.Name}} { 73 impl MojomEncodable for {{$struct.Name}} {
68 impl_encodable_for_pointer!(); 74 impl_encodable_for_pointer!();
69 fn compute_size(&self, context: Context) -> usize { 75 fn compute_size(&self, context: Context) -> usize {
70 encoding::align_default(self.serialized_size(&context)) 76 encoding::align_default(self.serialized_size(&context))
71 {{range $field := $struct.Fields}} + self.{{$field.Name}}.compute_size(co ntext.clone()) 77 {{range $field := $struct.Fields}} + self.{{$field.Name}}.compute_size(co ntext.clone())
72 {{end}} } 78 {{end}} }
73 } 79 }
74 80
75 impl MojomStruct for {{$struct.Name}} {} 81 impl MojomStruct for {{$struct.Name}} {}
76 {{- end}} 82 {{- end}}
77 ` 83 `
OLDNEW
« no previous file with comments | « mojom/generators/rust/templates/source.tmpl.go ('k') | mojom/generators/rust/templates/union.tmpl.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698