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

Unified Diff: mojom/generators/rust/templates/union.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/struct.tmpl.go ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojom/generators/rust/templates/union.tmpl.go
diff --git a/mojom/generators/rust/templates/union.tmpl.go b/mojom/generators/rust/templates/union.tmpl.go
index d395346daa24cd81903e9b1376dcc3f07059862f..0e957be54b64cb9bd26813e7f62d5a70a71c8919 100644
--- a/mojom/generators/rust/templates/union.tmpl.go
+++ b/mojom/generators/rust/templates/union.tmpl.go
@@ -31,18 +31,24 @@ impl MojomUnion for {{$union.Name}} {
{{end}} {{$union.Name}}::_Unknown(val) => MojomEncodable::encode(val, encoder, context.clone()),
}
}
- fn decode_value(decoder: &mut Decoder, context: Context) -> Self {
- // TODO(mknyszek): Validate bytes and tag
- let (_bytes, tag) = {
+ fn decode_value(decoder: &mut Decoder, context: Context) -> Result<Self, ValidationError> {
+ let tag = {
let mut state = decoder.get_mut(&context);
let bytes = state.decode::<u32>();
- let tag = state.decode::<u32>();
- (bytes, tag)
+ if (bytes as usize) != UNION_SIZE {
+ return Err(ValidationError::UnexpectedNullUnion);
+ }
+ state.decode::<u32>()
};
- match tag {
-{{range $field := $union.Fields}} {{$union.TagsEnum.Name}}_{{$field.Name}} => {{$union.Name}}::{{$field.Name}}(<{{$field.Type}}>::decode(decoder, context.clone())),
-{{end}} _ => {{$union.Name}}::_Unknown(u64::decode(decoder, context.clone())),
- }
+ Ok(match tag {
+{{range $field := $union.Fields}} {{$union.TagsEnum.Name}}_{{$field.Name}} => {{$union.Name}}::{{$field.Name}}({
+ match <{{$field.Type}}>::decode(decoder, context.clone()) {
+ Ok(value) => value,
+ Err(err) => return Err(err),
+ }
+ }),
+{{end}} _ => {{$union.Name}}::_Unknown(u64::decode(decoder, context.clone()).unwrap()),
+ })
}
}
« no previous file with comments | « mojom/generators/rust/templates/struct.tmpl.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698