Skip to content

Commit

Permalink
Provide blanket implementation for protobuf (#557)
Browse files Browse the repository at this point in the history
Allow any protobuf message to be encoded and decoded as bytes + handles,
where handles are always empty.

Also validate that incoming handles are indeed empty when decoding, and
return an error if that's not the case.
  • Loading branch information
tiziano88 authored Feb 5, 2020
1 parent f3eb28b commit 94ef0a5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
32 changes: 1 addition & 31 deletions sdk/rust/oak/src/grpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use crate::proto::code::Code;
use crate::{proto, OakError, OakStatus, ReadHandle};
use log::{error, info, warn};
use proto::grpc_encap::{GrpcRequest, GrpcResponse};
use protobuf::{Message, ProtobufEnum};
use protobuf::ProtobufEnum;

mod invocation;
pub use invocation::Invocation;
Expand All @@ -36,43 +36,13 @@ pub fn build_status(code: Code, msg: &str) -> proto::status::Status {
status
}

impl crate::io::Encodable for GrpcRequest {
fn encode(&self) -> std::result::Result<crate::io::Message, crate::OakError> {
let bytes = self.write_to_bytes()?;
let handles = Vec::new();
Ok(crate::io::Message { bytes, handles })
}
}

impl crate::io::Decodable for GrpcRequest {
fn decode(message: &crate::io::Message) -> std::result::Result<Self, crate::OakError> {
let value = protobuf::parse_from_bytes(&message.bytes)?;
Ok(value)
}
}

/// Channel-holding object that encapsulates response messages into
/// `GrpcResponse` wrapper messages and writes serialized versions to a send
/// channel.
pub struct ChannelResponseWriter {
sender: crate::io::Sender<GrpcResponse>,
}

impl crate::io::Encodable for GrpcResponse {
fn encode(&self) -> std::result::Result<crate::io::Message, crate::OakError> {
let bytes = self.write_to_bytes()?;
let handles = Vec::new();
Ok(crate::io::Message { bytes, handles })
}
}

impl crate::io::Decodable for GrpcResponse {
fn decode(message: &crate::io::Message) -> std::result::Result<Self, crate::OakError> {
let value = protobuf::parse_from_bytes(&message.bytes)?;
Ok(value)
}
}

/// Indicate whether a write method should leave the current gRPC method
/// invocation open or close it.
#[derive(PartialEq, Clone, Debug)]
Expand Down
12 changes: 12 additions & 0 deletions sdk/rust/oak/src/io/decodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ use crate::OakError;
pub trait Decodable: Sized {
fn decode(message: &Message) -> Result<Self, OakError>;
}

impl<T: protobuf::Message> Decodable for T {
fn decode(message: &Message) -> Result<Self, OakError> {
if !message.handles.is_empty() {
return Err(
protobuf::ProtobufError::WireError(protobuf::error::WireError::Other).into(),
);
}
let value = protobuf::parse_from_bytes(&message.bytes)?;
Ok(value)
}
}
8 changes: 8 additions & 0 deletions sdk/rust/oak/src/io/encodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ use crate::OakError;
pub trait Encodable {
fn encode(&self) -> Result<Message, OakError>;
}

impl<T: protobuf::Message> Encodable for T {
fn encode(&self) -> Result<Message, OakError> {
let bytes = self.write_to_bytes()?;
let handles = Vec::new();
Ok(crate::io::Message { bytes, handles })
}
}

0 comments on commit 94ef0a5

Please sign in to comment.