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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojom/generators/rust/templates/struct.tmpl.go
diff --git a/mojom/generators/rust/templates/struct.tmpl.go b/mojom/generators/rust/templates/struct.tmpl.go
index 5886840336c1cd1405752a138c4ab250748eb713..797998aba4a9a3d4a8ee140c1c6c7ba7523f53fb 100644
--- a/mojom/generators/rust/templates/struct.tmpl.go
+++ b/mojom/generators/rust/templates/struct.tmpl.go
@@ -41,26 +41,32 @@ impl MojomPointer for {{$struct.Name}} {
{{range $field := $struct.Fields}} MojomEncodable::encode(self.{{$field.Name}}, encoder, context.clone());
{{end}}
}
- fn decode_value(decoder: &mut Decoder, context: Context) -> Self {
- // TODO(mknyszek): Validate bytes and version
- let (_bytes, version) = {
+ fn decode_value(decoder: &mut Decoder, context: Context) -> Result<Self, ValidationError> {
+ let version = {
let mut state = decoder.get_mut(&context);
- let bytes = state.decode::<u32>();
- let version = state.decode::<u32>();
- (bytes, version)
+ match state.decode_struct_header(&{{$struct.Name}}Versions) {
+ Ok(header) => header.data(),
+ Err(err) => return Err(err),
+ }
};
{{range $field := $struct.Fields}} let {{$field.Name}} = {{if ne $field.MinVersion 0 -}}
if version >= {{$field.MinVersion}} {
- <{{$field.Type}}>::decode(decoder, context.clone())
+ match <{{$field.Type}}>::decode(decoder, context.clone()) {
+ Ok(value) => value,
+ Err(err) => return Err(err),
+ }
} else {
Default::default()
};
{{- else -}}
- <{{$field.Type}}>::decode(decoder, context.clone());
+ match <{{$field.Type}}>::decode(decoder, context.clone()) {
+ Ok(value) => value,
+ Err(err) => return Err(err),
+ };
{{- end}}
-{{end}} {{$struct.Name}} {
+{{end}} Ok({{$struct.Name}} {
{{range $field := $struct.Fields}} {{$field.Name}}: {{$field.Name}},
-{{end}} }
+{{end}} })
}
}
« 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