Skip to content

Commit

Permalink
feat: add rust-protobuf support to extism-convert (extism#653)
Browse files Browse the repository at this point in the history
(code from @zshipko, thank you!)

---------

Co-authored-by: Zach Shipko <[email protected]>
  • Loading branch information
nilslice and zshipko authored Jan 5, 2024
1 parent 431bc4d commit 211d553
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
4 changes: 2 additions & 2 deletions convert/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ anyhow = "1.0.75"
base64 = "~0.21"
bytemuck = {version = "1.14.0", optional = true }
prost = { version = "0.12.0", optional = true }
protobuf = { version = "3.2.0", optional = true }
rmp-serde = { version = "1.1.2", optional = true }
serde = "1.0.186"
serde_json = "1.0.105"
Expand All @@ -22,7 +23,6 @@ serde_json = "1.0.105"
serde = { version = "1.0.186", features = ["derive"] }

[features]
default = ["msgpack", "protobuf", "raw"]
default = ["msgpack", "prost", "raw"]
msgpack = ["rmp-serde"]
protobuf = ["prost"]
raw = ["bytemuck"]
38 changes: 30 additions & 8 deletions convert/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,30 +112,52 @@ impl FromBytesOwned for Base64<String> {
/// Protobuf encoding
///
/// Allows for `prost` Protobuf messages to be used as arguments to Extism plugin calls
#[cfg(feature = "protobuf")]
#[cfg(feature = "prost")]
#[derive(Debug)]
pub struct Protobuf<T: prost::Message>(pub T);
pub struct Prost<T: prost::Message>(pub T);

#[cfg(feature = "protobuf")]
impl<T: prost::Message> From<T> for Protobuf<T> {
#[cfg(feature = "prost")]
impl<T: prost::Message> From<T> for Prost<T> {
fn from(data: T) -> Self {
Self(data)
}
}

#[cfg(feature = "protobuf")]
impl<'a, T: prost::Message> ToBytes<'a> for Protobuf<T> {
#[cfg(feature = "prost")]
impl<'a, T: prost::Message> ToBytes<'a> for Prost<T> {
type Bytes = Vec<u8>;

fn to_bytes(&self) -> Result<Self::Bytes, Error> {
Ok(self.0.encode_to_vec())
}
}

#[cfg(feature = "prost")]
impl<T: Default + prost::Message> FromBytesOwned for Prost<T> {
fn from_bytes_owned(data: &[u8]) -> Result<Self, Error> {
Ok(Prost(T::decode(data)?))
}
}

/// Protobuf encoding
///
/// Allows for `rust-protobuf` Protobuf messages to be used as arguments to Extism plugin calls
#[cfg(feature = "protobuf")]
pub struct Protobuf<T: protobuf::Message>(pub T);

#[cfg(feature = "protobuf")]
impl<'a, T: protobuf::Message> ToBytes<'a> for Protobuf<T> {
type Bytes = Vec<u8>;

fn to_bytes(&self) -> Result<Self::Bytes, Error> {
Ok(self.0.write_to_bytes()?)
}
}

#[cfg(feature = "protobuf")]
impl<T: Default + prost::Message> FromBytesOwned for Protobuf<T> {
impl<T: Default + protobuf::Message> FromBytesOwned for Protobuf<T> {
fn from_bytes_owned(data: &[u8]) -> Result<Self, Error> {
Ok(Protobuf(T::decode(data)?))
Ok(Protobuf(T::parse_from_bytes(data)?))
}
}

Expand Down
3 changes: 3 additions & 0 deletions convert/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub use encoding::{Base64, Json};
#[cfg(feature = "msgpack")]
pub use encoding::Msgpack;

#[cfg(feature = "prost")]
pub use encoding::Prost;

#[cfg(feature = "protobuf")]
pub use encoding::Protobuf;

Expand Down

0 comments on commit 211d553

Please sign in to comment.