Skip to content

Commit

Permalink
Updated rust plugin bindings to use polywrap_msgpack_serde
Browse files Browse the repository at this point in the history
  • Loading branch information
namesty committed Jul 12, 2023
1 parent 82f842a commit 189c46c
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 51 deletions.
Binary file modified implementations/plugin-rust/build/wrap.wasm
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
use std::sync::Arc;
use polywrap_core::invoke::Invoker;
use polywrap_plugin::{error::PluginError, module::PluginModule};
use polywrap_msgpack_serde::{
to_vec,
from_slice,
BigIntWrapper,
BigNumber,
JSONString,
bytes
};
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
use super::types::*;

Expand All @@ -19,15 +28,15 @@ pub struct ArgsModuleMethod {
pub enum_array: Vec<CustomEnum>,
#[serde(rename = "optEnumArray")]
pub opt_enum_array: Option<Vec<Option<CustomEnum>>>,
pub map: Map<String, i32>,
pub map: BTreeMap<String, i32>,
#[serde(rename = "mapOfArr")]
pub map_of_arr: Map<String, Vec<i32>>,
pub map_of_arr: BTreeMap<String, Vec<i32>>,
#[serde(rename = "mapOfMap")]
pub map_of_map: Map<String, Map<String, i32>>,
pub map_of_map: BTreeMap<String, BTreeMap<String, i32>>,
#[serde(rename = "mapOfObj")]
pub map_of_obj: Map<String, AnotherType>,
pub map_of_obj: BTreeMap<String, AnotherType>,
#[serde(rename = "mapOfArrOfObj")]
pub map_of_arr_of_obj: Map<String, Vec<AnotherType>>,
pub map_of_arr_of_obj: BTreeMap<String, Vec<AnotherType>>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
// NOTE: This is an auto-generated file.
// All modifications will be overwritten.
use polywrap_core::{invoke::Invoker, uri::Uri};
use polywrap_msgpack::{decode, serialize};
use polywrap_plugin::{error::PluginError, BigInt, BigNumber, Map, JSON};
use polywrap_plugin::error::PluginError;
use polywrap_msgpack_serde::{
to_vec,
from_slice,
BigIntWrapper,
BigNumber,
JSONString,
bytes
};
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
use std::sync::Arc;

Expand All @@ -17,7 +25,7 @@ pub struct Env {
#[serde(rename = "optProp")]
pub opt_prop: Option<String>,
#[serde(rename = "optMap")]
pub opt_map: Option<Map<String, Option<i32>>>,
pub opt_map: Option<BTreeMap<String, Option<i32>>>,
}
// Env END //

Expand All @@ -38,18 +46,18 @@ pub struct CustomType {
pub i8: i8,
pub i16: i16,
pub i32: i32,
pub bigint: BigInt,
pub bigint: BigIntWrapper,
#[serde(rename = "optBigint")]
pub opt_bigint: Option<BigInt>,
pub opt_bigint: Option<BigIntWrapper>,
pub bignumber: BigNumber,
#[serde(rename = "optBignumber")]
pub opt_bignumber: Option<BigNumber>,
pub json: JSON::Value,
pub json: JSONString,
#[serde(rename = "optJson")]
pub opt_json: Option<JSON::Value>,
#[serde(with = "serde_bytes")]
pub opt_json: Option<JSONString>,
#[serde(with = "bytes")]
pub bytes: Vec<u8>,
#[serde(with = "serde_bytes")]
#[serde(with = "bytes")]
#[serde(rename = "optBytes")]
pub opt_bytes: Option<Vec<u8>>,
pub boolean: bool,
Expand Down Expand Up @@ -84,15 +92,15 @@ pub struct CustomType {
pub enum_array: Vec<CustomEnum>,
#[serde(rename = "optEnumArray")]
pub opt_enum_array: Option<Vec<Option<CustomEnum>>>,
pub map: Map<String, i32>,
pub map: BTreeMap<String, i32>,
#[serde(rename = "mapOfArr")]
pub map_of_arr: Map<String, Vec<i32>>,
pub map_of_arr: BTreeMap<String, Vec<i32>>,
#[serde(rename = "mapOfObj")]
pub map_of_obj: Map<String, AnotherType>,
pub map_of_obj: BTreeMap<String, AnotherType>,
#[serde(rename = "mapOfArrOfObj")]
pub map_of_arr_of_obj: Map<String, Vec<AnotherType>>,
pub map_of_arr_of_obj: BTreeMap<String, Vec<AnotherType>>,
#[serde(rename = "mapCustomValue")]
pub map_custom_value: Map<String, Option<CustomMapValue>>,
pub map_custom_value: BTreeMap<String, Option<CustomMapValue>>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct AnotherType {
Expand Down Expand Up @@ -228,7 +236,7 @@ impl<'a> TestImportModule<'a> {

pub fn imported_method(&self, args: &TestImportModuleArgsImportedMethod) -> Result<Option<TestImportObject>, PluginError> {
let uri = self.uri;
let serialized_args = serialize(args.clone()).unwrap();
let serialized_args = to_vec(args.clone()).unwrap();
let result = invoker.invoke_raw(
uri,
"importedMethod",
Expand All @@ -243,12 +251,12 @@ impl<'a> TestImportModule<'a> {
exception: e.to_string(),
})?;

Ok(Some(decode(result.as_slice())?))
Ok(Some(from_slice(result.as_slice())?))
}

pub fn another_method(&self, args: &TestImportModuleArgsAnotherMethod) -> Result<i32, PluginError> {
let uri = self.uri;
let serialized_args = serialize(args.clone()).unwrap();
let serialized_args = to_vec(args.clone()).unwrap();
let result = invoker.invoke_raw(
uri,
"anotherMethod",
Expand All @@ -263,12 +271,12 @@ impl<'a> TestImportModule<'a> {
exception: e.to_string(),
})?;

Ok(decode(result.as_slice())?)
Ok(from_slice(result.as_slice())?)
}

pub fn returns_array_of_enums(&self, args: &TestImportModuleArgsReturnsArrayOfEnums) -> Result<Vec<Option<TestImportEnumReturn>>, PluginError> {
let uri = self.uri;
let serialized_args = serialize(args.clone()).unwrap();
let serialized_args = to_vec(args.clone()).unwrap();
let result = invoker.invoke_raw(
uri,
"returnsArrayOfEnums",
Expand All @@ -283,7 +291,7 @@ impl<'a> TestImportModule<'a> {
exception: e.to_string(),
})?;

Ok(decode(result.as_slice())?)
Ok(from_slice(result.as_slice())?)
}
}
// Imported Modules END //
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
use std::sync::Arc;
use polywrap_core::invoke::Invoker;
use polywrap_plugin::{error::PluginError, module::PluginModule};
use polywrap_msgpack_serde::{
to_vec,
from_slice,
BigIntWrapper,
BigNumber,
JSONString,
bytes
};
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
use super::types::*;

Expand All @@ -16,7 +25,7 @@ pub struct ArgsFunction1 {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ArgsFunction2 {
pub arg1: Option<i32>,
#[serde(with = "serde_bytes")]
#[serde(with = "bytes")]
pub arg2: Option<Vec<u8>>,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
// NOTE: This is an auto-generated file.
// All modifications will be overwritten.
use polywrap_core::{invoke::Invoker, uri::Uri};
use polywrap_msgpack::{decode, serialize};
use polywrap_plugin::{error::PluginError, BigInt, BigNumber, Map, JSON};
use polywrap_plugin::error::PluginError;
use polywrap_msgpack_serde::{
to_vec,
from_slice,
BigIntWrapper,
BigNumber,
JSONString,
bytes
};
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};

// Env START //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
use std::sync::Arc;
use polywrap_core::invoke::Invoker;
use polywrap_plugin::{error::PluginError, module::PluginModule};
use polywrap_msgpack_serde::{
to_vec,
from_slice,
BigIntWrapper,
BigNumber,
JSONString,
bytes
};
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
use super::types::*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
// NOTE: This is an auto-generated file.
// All modifications will be overwritten.
use polywrap_core::{invoke::Invoker, uri::Uri};
use polywrap_msgpack::{decode, serialize};
use polywrap_plugin::{error::PluginError, BigInt, BigNumber, Map, JSON};
use polywrap_plugin::error::PluginError;
use polywrap_msgpack_serde::{
to_vec,
from_slice,
BigIntWrapper,
BigNumber,
JSONString,
bytes
};
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};

// Env START //
Expand All @@ -29,18 +37,18 @@ pub struct CustomType {
pub i8: i8,
pub i16: i16,
pub i32: i32,
pub bigint: BigInt,
pub bigint: BigIntWrapper,
#[serde(rename = "optBigint")]
pub opt_bigint: Option<BigInt>,
pub opt_bigint: Option<BigIntWrapper>,
pub bignumber: BigNumber,
#[serde(rename = "optBignumber")]
pub opt_bignumber: Option<BigNumber>,
pub json: JSON::Value,
pub json: JSONString,
#[serde(rename = "optJson")]
pub opt_json: Option<JSON::Value>,
#[serde(with = "serde_bytes")]
pub opt_json: Option<JSONString>,
#[serde(with = "bytes")]
pub bytes: Vec<u8>,
#[serde(with = "serde_bytes")]
#[serde(with = "bytes")]
#[serde(rename = "optBytes")]
pub opt_bytes: Option<Vec<u8>>,
pub boolean: bool,
Expand Down Expand Up @@ -68,15 +76,15 @@ pub struct CustomType {
pub object_array: Vec<AnotherType>,
#[serde(rename = "optObjectArray")]
pub opt_object_array: Option<Vec<Option<AnotherType>>>,
pub map: Map<String, i32>,
pub map: BTreeMap<String, i32>,
#[serde(rename = "mapOfArr")]
pub map_of_arr: Map<String, Vec<i32>>,
pub map_of_arr: BTreeMap<String, Vec<i32>>,
#[serde(rename = "mapOfObj")]
pub map_of_obj: Map<String, AnotherType>,
pub map_of_obj: BTreeMap<String, AnotherType>,
#[serde(rename = "mapOfArrOfObj")]
pub map_of_arr_of_obj: Map<String, Vec<AnotherType>>,
pub map_of_arr_of_obj: BTreeMap<String, Vec<AnotherType>>,
#[serde(rename = "mapCustomValue")]
pub map_custom_value: Map<String, Option<CustomMapValue>>,
pub map_custom_value: BTreeMap<String, Option<CustomMapValue>>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct AnotherType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ handlebars_helper!(serde_annotate_if_bytes: |val: Value| {

pub fn _serde_annotate_if_bytes(val: &str) -> String {
if val == "Bytes" {
return "#[serde(with = \"serde_bytes\")]\n ".to_string();
return "#[serde(with = \"bytes\")]\n ".to_string();
}
"".to_string()
}
6 changes: 3 additions & 3 deletions implementations/plugin-rust/src/helpers/to_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ pub fn _to_rust(value: &str) -> String {
"String" => "String".to_string(),
"Boolean" => "bool".to_string(),
"Bytes" => "Vec<u8>".to_string(),
"BigInt" => "BigInt".to_string(),
"BigInt" => "BigIntWrapper".to_string(),
"BigNumber" => "BigNumber".to_string(),
"JSON" => "JSON::Value".to_string(),
"JSON" => "JSONString".to_string(),
_ => {
if res.starts_with("Enum_") {
res = res.replacen("Enum_", "", 1);
Expand All @@ -64,7 +64,7 @@ pub fn _to_rust_map(value: &str, optional: bool) -> Result<String, String> {
let (key_type, val_type) = map_types(value)?;
let rs_key_type = _to_rust(&key_type);
let rs_val_type = _to_rust(&val_type);
let rs_map = format!("Map<{}, {}>", &rs_key_type, &rs_val_type);
let rs_map = format!("BTreeMap<{}, {}>", &rs_key_type, &rs_val_type);
Ok(_apply_optional(&rs_map, optional))
}

Expand Down
9 changes: 9 additions & 0 deletions implementations/plugin-rust/src/templates/module_rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ lazy_static! {
use std::sync::Arc;
use polywrap_core::invoke::Invoker;
use polywrap_plugin::{error::PluginError, module::PluginModule};
use polywrap_msgpack_serde::{
to_vec,
from_slice,
BigIntWrapper,
BigNumber,
JSONString,
bytes
};
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
use super::types::*;
Expand Down
20 changes: 14 additions & 6 deletions implementations/plugin-rust/src/templates/types_rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ lazy_static! {
// NOTE: This is an auto-generated file.
// All modifications will be overwritten.
use polywrap_core::{invoke::Invoker, uri::Uri};
use polywrap_msgpack::{decode, serialize};
use polywrap_plugin::{error::PluginError, BigInt, BigNumber, Map, JSON};
use polywrap_plugin::error::PluginError;
use polywrap_msgpack_serde::{
to_vec,
from_slice,
BigIntWrapper,
BigNumber,
JSONString,
bytes
};
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
{{#each importedModuleTypes}}
use std::sync::Arc;
Expand Down Expand Up @@ -116,7 +124,7 @@ impl<'a> {{detect_keyword (to_upper type)}}<'a> {
{{#each methods}}
pub fn {{to_lower name}}(&self, args: &{{to_upper ../type}}Args{{to_upper name}}) -> Result<{{#with return}}{{to_rust (to_graphql_type this)}}{{/with}}, PluginError> {
let uri = self.uri;
let serialized_args = serialize(args.clone()).unwrap();
let serialized_args = to_vec(args.clone()).unwrap();
let result = invoker.invoke_raw(
uri,
"{{name}}",
Expand All @@ -131,7 +139,7 @@ impl<'a> {{detect_keyword (to_upper type)}}<'a> {
exception: e.to_string(),
})?;
Ok({{#with return}}{{#if required}}{{else}}Some({{/if}}{{/with}}decode(result.as_slice())?{{#with return}}{{#if required}}{{else}}){{/if}}{{/with}})
Ok({{#with return}}{{#if required}}{{else}}Some({{/if}}{{/with}}from_slice(result.as_slice())?{{#with return}}{{#if required}}{{else}}){{/if}}{{/with}})
}
{{#if (is_not_last @index ../methods)}}
Expand All @@ -152,7 +160,7 @@ impl {{detect_keyword (to_upper type)}} {
{{#each methods}}
let uri = {{to_upper ../type}}::URI;
pub fn {{detect_keyword (to_lower name)}}(args: &{{to_upper ../type}}Args{{to_upper name}}, invoker: Arc<dyn Invoker>) -> Result<{{#with return}}{{to_rust (to_graphql_type this)}}{{/with}}, PluginError> {
let serialized_args = serialize(args.clone()).unwrap();
let serialized_args = to_vec(args.clone()).unwrap();
let opt_args = Some(serialized_args.as_slice());
let uri = Uri::try_from(uri).unwrap();
let result = invoker.invoke_raw(
Expand All @@ -169,7 +177,7 @@ impl {{detect_keyword (to_upper type)}} {
exception: e.to_string(),
})?;
Ok({{#with return}}{{#if required}}{{else}}Some({{/if}}{{/with}}decode(result.as_slice())?{{#with return}}{{#if required}}{{else}}){{/if}}{{/with}})
Ok({{#with return}}{{#if required}}{{else}}Some({{/if}}{{/with}}from_slice(result.as_slice())?{{#with return}}{{#if required}}{{else}}){{/if}}{{/with}})
}
{{#if (is_not_last @index ../methods)}}
Expand Down

0 comments on commit 189c46c

Please sign in to comment.