Compare commits

...

2 Commits

Author SHA1 Message Date
Steve Manuel
a3341eb238 chore: bump version for extism-convert 2023-11-03 15:47:55 -06:00
Steve Manuel
2d01efb833 feat!: add alt protobuf crate support to convert crate 2023-11-03 15:42:56 -06:00
2 changed files with 31 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "extism-convert"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
authors = ["The Extism Authors", "oss@extism.org"]
license = "BSD-3-Clause"
@@ -13,14 +13,16 @@ description = "Traits to make Rust types usable with Extism"
anyhow = "1.0.75"
base64 = "0.21.3"
prost = { version = "0.12.0", optional = true }
protobuf = { version = "3.3.0", optional = true }
rmp-serde = { version = "1.1.2", optional = true }
serde = "1.0.186"
serde_json = "1.0.105"
[dev-dependencies]
serde = {version = "1.0.186", features = ["derive"]}
serde = { version = "1.0.186", features = ["derive"] }
[features]
default = ["msgpack", "protobuf"]
default = ["msgpack", "prost"]
msgpack = ["rmp-serde"]
protobuf = ["prost"]
prost = ["prost"]
protobuf = ["protobuf"]

View File

@@ -98,10 +98,10 @@ 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")]
pub struct Protobuf<T: prost::Message>(pub T);
#[cfg(feature = "protobuf")]
#[cfg(feature = "prost")]
impl<'a, T: prost::Message> ToBytes<'a> for Protobuf<T> {
type Bytes = Vec<u8>;
@@ -110,9 +110,31 @@ impl<'a, T: prost::Message> ToBytes<'a> for Protobuf<T> {
}
}
#[cfg(feature = "protobuf")]
#[cfg(feature = "prost")]
impl<T: Default + prost::Message> FromBytesOwned for Protobuf<T> {
fn from_bytes_owned(data: &[u8]) -> Result<Self, Error> {
Ok(Protobuf(T::decode(data)?))
}
}
/// Protobuf encoding
///
/// Allows for `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<'a, T: Default + protobuf::Message> FromBytesOwned for Protobuf<T> {
fn from_bytes_owned(data: &[u8]) -> Result<Self, Error> {
Ok(Protobuf(T::parse_from_bytes(data)?))
}
}