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

Unified Diff: mojom/generators/rust/templates/interface.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/rustgen/type_translation.go ('k') | mojom/generators/rust/templates/source.tmpl.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojom/generators/rust/templates/interface.tmpl.go
diff --git a/mojom/generators/rust/templates/interface.tmpl.go b/mojom/generators/rust/templates/interface.tmpl.go
index 2f5c4adf39cf848a094342ceaf67159f93face0f..e8af6aca8982d08a2070e8fe0241a31a1a54225c 100644
--- a/mojom/generators/rust/templates/interface.tmpl.go
+++ b/mojom/generators/rust/templates/interface.tmpl.go
@@ -100,11 +100,23 @@ pub enum {{$req_option}} {
}
impl MojomMessageOption for {{$req_option}} {
- fn decode_payload(ordinal: u32, buffer: &[u8], handles: Vec<UntypedHandle>) -> Self {
- match ordinal {
-{{range $message := $interface.Messages}} {{$message.Name}}::ORDINAL => {{$req_option}}::{{$message.Name}}({{$message.RequestStruct.Name}}::deserialize(buffer, handles)),
-{{end}} _ => panic!("Unknown message found: {}", ordinal),
- }
+ fn decode_payload(header: MessageHeader, buffer: &[u8], handles: Vec<UntypedHandle>) -> Result<Self, ValidationError> {
+ match header.name {
+{{range $message := $interface.Messages}} {{$message.Name}}::ORDINAL => {
+ if header.flags != {{- if eq $message.ResponseStruct.Name "" -}}
+ message::MESSAGE_HEADER_NO_FLAG
+ {{- else -}}
+ message::MESSAGE_HEADER_EXPECT_RESPONSE
+ {{- end -}} {
+ return Err(ValidationError::MessageHeaderInvalidFlags);
+ }
+ match {{$message.RequestStruct.Name}}::deserialize(buffer, handles) {
+ Ok(value) => Ok({{$req_option}}::{{$message.Name}}(value)),
+ Err(err) => return Err(err),
+ }
+ },
+{{end}} _ => Err(ValidationError::MessageHeaderUnknownMethod),
+ }
}
}
@@ -116,13 +128,21 @@ pub enum {{$rsp_option}} {
}
impl MojomMessageOption for {{$rsp_option}} {
- fn decode_payload(ordinal: u32, buffer: &[u8], handles: Vec<UntypedHandle>) -> Self {
- match ordinal {
+ fn decode_payload(header: MessageHeader, buffer: &[u8], handles: Vec<UntypedHandle>) -> Result<Self, ValidationError> {
+ if header.flags != message::MESSAGE_HEADER_IS_RESPONSE {
+ return Err(ValidationError::MessageHeaderInvalidFlags);
+ }
+ match header.name {
{{range $message := $interface.Messages}}
-{{- if ne $message.ResponseStruct.Name ""}} {{$message.Name}}::ORDINAL => {{$rsp_option}}::{{$message.Name}}({{$message.ResponseStruct.Name}}::deserialize(buffer, handles)),
+{{- if ne $message.ResponseStruct.Name ""}} {{$message.Name}}::ORDINAL => {
+ match {{$message.ResponseStruct.Name}}::deserialize(buffer, handles) {
+ Ok(value) => Ok({{$rsp_option}}::{{$message.Name}}(value)),
+ Err(err) => return Err(err),
+ }
+ },
{{end -}}
-{{end}} _ => panic!("Unknown message found, or message has no response: {}", ordinal),
- }
+{{end}} _ => Err(ValidationError::MessageHeaderUnknownMethod),
+ }
}
}
« no previous file with comments | « mojom/generators/rust/rustgen/type_translation.go ('k') | mojom/generators/rust/templates/source.tmpl.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698