Skip to content

Commit

Permalink
Using serde annotations instead of wrapper types
Browse files Browse the repository at this point in the history
  • Loading branch information
namesty committed Jul 13, 2023
1 parent 189c46c commit 23cdbf1
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 40 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 @@ -7,10 +7,14 @@ use polywrap_plugin::{error::PluginError, module::PluginModule};
use polywrap_msgpack_serde::{
to_vec,
from_slice,
BigIntWrapper,
BigInt,
BigNumber,
JSONString,
bytes
JSON,
bytes,
wrappers::{
polywrap_bigint as bigint,
polywrap_json as json
}
};
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ use polywrap_plugin::error::PluginError;
use polywrap_msgpack_serde::{
to_vec,
from_slice,
BigIntWrapper,
BigInt,
BigNumber,
JSONString,
bytes
JSON,
bytes,
wrappers::{
polywrap_bigint as bigint,
polywrap_json as json
}
};
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
Expand Down Expand Up @@ -46,15 +50,19 @@ pub struct CustomType {
pub i8: i8,
pub i16: i16,
pub i32: i32,
pub bigint: BigIntWrapper,
#[serde(with = "bigint")]
pub bigint: BigInt,
#[serde(with = "bigint")]
#[serde(rename = "optBigint")]
pub opt_bigint: Option<BigIntWrapper>,
pub opt_bigint: Option<BigInt>,
pub bignumber: BigNumber,
#[serde(rename = "optBignumber")]
pub opt_bignumber: Option<BigNumber>,
pub json: JSONString,
#[serde(with = "json")]
pub json: JSON::Value,
#[serde(with = "json")]
#[serde(rename = "optJson")]
pub opt_json: Option<JSONString>,
pub opt_json: Option<JSON::Value>,
#[serde(with = "bytes")]
pub bytes: Vec<u8>,
#[serde(with = "bytes")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ use polywrap_plugin::{error::PluginError, module::PluginModule};
use polywrap_msgpack_serde::{
to_vec,
from_slice,
BigIntWrapper,
BigInt,
BigNumber,
JSONString,
bytes
JSON,
bytes,
wrappers::{
polywrap_bigint as bigint,
polywrap_json as json
}
};
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ use polywrap_plugin::error::PluginError;
use polywrap_msgpack_serde::{
to_vec,
from_slice,
BigIntWrapper,
BigInt,
BigNumber,
JSONString,
bytes
JSON,
bytes,
wrappers::{
polywrap_bigint as bigint,
polywrap_json as json
}
};
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ use polywrap_plugin::{error::PluginError, module::PluginModule};
use polywrap_msgpack_serde::{
to_vec,
from_slice,
BigIntWrapper,
BigInt,
BigNumber,
JSONString,
bytes
JSON,
bytes,
wrappers::{
polywrap_bigint as bigint,
polywrap_json as json
}
};
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ use polywrap_plugin::error::PluginError;
use polywrap_msgpack_serde::{
to_vec,
from_slice,
BigIntWrapper,
BigInt,
BigNumber,
JSONString,
bytes
JSON,
bytes,
wrappers::{
polywrap_bigint as bigint,
polywrap_json as json
}
};
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
Expand All @@ -37,15 +41,19 @@ pub struct CustomType {
pub i8: i8,
pub i16: i16,
pub i32: i32,
pub bigint: BigIntWrapper,
#[serde(with = "bigint")]
pub bigint: BigInt,
#[serde(with = "bigint")]
#[serde(rename = "optBigint")]
pub opt_bigint: Option<BigIntWrapper>,
pub opt_bigint: Option<BigInt>,
pub bignumber: BigNumber,
#[serde(rename = "optBignumber")]
pub opt_bignumber: Option<BigNumber>,
pub json: JSONString,
#[serde(with = "json")]
pub json: JSON::Value,
#[serde(with = "json")]
#[serde(rename = "optJson")]
pub opt_json: Option<JSONString>,
pub opt_json: Option<JSON::Value>,
#[serde(with = "bytes")]
pub bytes: Vec<u8>,
#[serde(with = "bytes")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use handlebars::handlebars_helper;
use serde_json::{Value};
use serde_json::Value;

handlebars_helper!(serde_annotate_if_bytes: |val: Value| {
let name = val.as_str().unwrap();
_serde_annotate_if_bytes(name)
_serde_annotate_if_wrapper(name)
});

pub fn _serde_annotate_if_bytes(val: &str) -> String {
if val == "Bytes" {
return "#[serde(with = \"bytes\")]\n ".to_string();
pub fn _serde_annotate_if_wrapper(val: &str) -> String {
match val {
"Bytes" => "#[serde(with = \"bytes\")]\n ".to_string(),
"JSON" => "#[serde(with = \"json\")]\n ".to_string(),
"BigInt" => "#[serde(with = \"bigint\")]\n ".to_string(),
_ => "".to_string(),
}
"".to_string()
}
4 changes: 2 additions & 2 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" => "BigIntWrapper".to_string(),
"BigInt" => "BigInt".to_string(),
"BigNumber" => "BigNumber".to_string(),
"JSON" => "JSONString".to_string(),
"JSON" => "JSON::Value".to_string(),
_ => {
if res.starts_with("Enum_") {
res = res.replacen("Enum_", "", 1);
Expand Down
10 changes: 7 additions & 3 deletions implementations/plugin-rust/src/templates/module_rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ use polywrap_plugin::{error::PluginError, module::PluginModule};
use polywrap_msgpack_serde::{
to_vec,
from_slice,
BigIntWrapper,
BigInt,
BigNumber,
JSONString,
bytes
JSON,
bytes,
wrappers::{
polywrap_bigint as bigint,
polywrap_json as json
}
};
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
Expand Down
10 changes: 7 additions & 3 deletions implementations/plugin-rust/src/templates/types_rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ use polywrap_plugin::error::PluginError;
use polywrap_msgpack_serde::{
to_vec,
from_slice,
BigIntWrapper,
BigInt,
BigNumber,
JSONString,
bytes
JSON,
bytes,
wrappers::{
polywrap_bigint as bigint,
polywrap_json as json
}
};
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
Expand Down

0 comments on commit 23cdbf1

Please sign in to comment.