Skip to content

Commit

Permalink
Merge pull request #106 from polywrap/chore/improve-rust-app-exp
Browse files Browse the repository at this point in the history
chore(app-rust): improve rust app dev exp & ignore build folder
  • Loading branch information
dOrgJelli authored Oct 2, 2023
2 parents 133e01b + d6a0c51 commit ab4d867
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 81 deletions.
3 changes: 2 additions & 1 deletion implementations/app-rust/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules
target
workflows/output.json
wrap
debug
debug
build
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ use std::sync::Arc;

pub type BigInt = String;

#[derive(Clone)]
pub struct InvokeOptions {
pub uri: Option<Uri>,
pub client: Option<Arc<dyn Invoker>>,
pub env: Option<Vec<u8>>
}

fn get_default_client() -> Arc<PolywrapClient> {
let mut config = PolywrapClientConfig::new();
config.add(SystemClientConfig::default().into());
config.add(Web3ClientConfig::default().into());
let client = PolywrapClient::new(config.build());
Arc::new(client)
}

// Env START //

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -155,6 +170,23 @@ pub struct TestImportAnotherObject {

// Imported envs START //

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TestImportEnv {
pub object: TestImportAnotherObject,
#[serde(rename = "optObject")]
pub opt_object: Option<TestImportAnotherObject>,
#[serde(rename = "objectArray")]
pub object_array: Vec<TestImportAnotherObject>,
#[serde(rename = "optObjectArray")]
pub opt_object_array: Option<Vec<Option<TestImportAnotherObject>>>,
pub en: TestImportEnum,
#[serde(rename = "optEnum")]
pub opt_enum: Option<TestImportEnum>,
#[serde(rename = "enumArray")]
pub enum_array: Vec<TestImportEnum>,
#[serde(rename = "optEnumArray")]
pub opt_enum_array: Option<Vec<Option<TestImportEnum>>>,
}
// Imported envs END //

// Imported enums START //
Expand All @@ -177,7 +209,7 @@ pub enum TestImportEnumReturn {

// URI: "testimport.uri.eth" //
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TestImportModuleArgsImportedMethod {
pub struct TestImportArgsImportedMethod {
pub str: String,
#[serde(rename = "optStr")]
pub opt_str: Option<String>,
Expand All @@ -204,50 +236,63 @@ pub struct TestImportModuleArgsImportedMethod {

// URI: "testimport.uri.eth" //
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TestImportModuleArgsAnotherMethod {
pub struct TestImportArgsAnotherMethod {
pub arg: Vec<String>,
}

// URI: "testimport.uri.eth" //
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TestImportModuleArgsReturnsArrayOfEnums {
pub struct TestImportArgsReturnsArrayOfEnums {
pub arg: String,
}

#[derive(Clone)]
pub struct TestImportModule {
uri: Uri,
invoker: Arc<dyn Invoker>,
env: Option<Vec<u8>>
pub struct TestImport {
pub uri: Uri,
pub invoker: Arc<dyn Invoker>,
pub env: Option<Vec<u8>>
}

impl TestImportModule {
pub fn new(uri: Option<Uri>, invoker: Option<Arc<dyn Invoker>>, env: Option<Vec<u8>>) -> TestImportModule {
let mut config = PolywrapClientConfig::new();
config.add(SystemClientConfig::default().into());
config.add(Web3ClientConfig::default().into());
let client = PolywrapClient::new(config.build());
impl TestImport {
pub fn new(invoke_options: Option<InvokeOptions>) -> TestImport {
let default_uri = uri!("testimport.uri.eth");
let (_uri, _invoker, _env) = if let Some(invoke_option) = invoke_options {
let _uri = if let Some(uri) = invoke_option.uri {
uri
} else {
default_uri
};

let _invoker = if let Some(invoker) = invoke_option.client {
invoker
} else {
get_default_client()
};

let _uri = uri.unwrap_or(uri!("testimport.uri.eth"));
let _invoker = invoker.unwrap_or(Arc::new(client));
let _env = env;
(_uri, _invoker, invoke_option.env)
} else {
(default_uri, get_default_client() as Arc<dyn Invoker>, None)
};

TestImportModule {
TestImport {
uri: _uri,
invoker: _invoker,
env: _env,
}
}

pub fn imported_method(&self, args: &TestImportModuleArgsImportedMethod, uri: Option<Uri>, invoker: Option<Arc<dyn Invoker>>, env: Option<Vec<u8>>) -> Result<Option<TestImportObject>, Error> {
let _uri = uri.unwrap_or(self.uri.clone());
let _invoker = invoker.unwrap_or(self.invoker.clone());
let _env = match &env {
Some(e) => Some(e.as_slice()),
None => match &self.env {
Some(e) => Some(e.as_slice()),
None => None,
},
pub fn default_uri() -> Uri {
uri!("testimport.uri.eth")
}

pub fn imported_method(&self, args: &TestImportArgsImportedMethod, invoke_options: Option<InvokeOptions>) -> Result<Option<TestImportObject>, Error> {
let (_uri, _invoker, _env) = if let Some(invoke_option) = invoke_options {
let uri = invoke_option.uri.clone().unwrap_or_else(|| self.uri.clone());
let invoker = invoke_option.client.clone().unwrap_or_else(|| self.invoker.clone());
let env = invoke_option.env.clone().or_else(|| self.env.clone());
(uri, invoker, env)
} else {
(self.uri.clone(), self.invoker.clone(), self.env.clone())
};

let serialized_args = to_vec(&args).unwrap();
Expand All @@ -256,22 +301,21 @@ impl TestImportModule {
&_uri,
"importedMethod",
opt_args,
_env,
_env.as_ref().map(|v| v.as_slice()),
None
)?;

from_slice(result.as_slice()).map_err(Error::MsgpackError)
}

pub fn another_method(&self, args: &TestImportModuleArgsAnotherMethod, uri: Option<Uri>, invoker: Option<Arc<dyn Invoker>>, env: Option<Vec<u8>>) -> Result<i32, Error> {
let _uri = uri.unwrap_or(self.uri.clone());
let _invoker = invoker.unwrap_or(self.invoker.clone());
let _env = match &env {
Some(e) => Some(e.as_slice()),
None => match &self.env {
Some(e) => Some(e.as_slice()),
None => None,
},
pub fn another_method(&self, args: &TestImportArgsAnotherMethod, invoke_options: Option<InvokeOptions>) -> Result<i32, Error> {
let (_uri, _invoker, _env) = if let Some(invoke_option) = invoke_options {
let uri = invoke_option.uri.clone().unwrap_or_else(|| self.uri.clone());
let invoker = invoke_option.client.clone().unwrap_or_else(|| self.invoker.clone());
let env = invoke_option.env.clone().or_else(|| self.env.clone());
(uri, invoker, env)
} else {
(self.uri.clone(), self.invoker.clone(), self.env.clone())
};

let serialized_args = to_vec(&args).unwrap();
Expand All @@ -280,22 +324,21 @@ impl TestImportModule {
&_uri,
"anotherMethod",
opt_args,
_env,
_env.as_ref().map(|v| v.as_slice()),
None
)?;

from_slice(result.as_slice()).map_err(Error::MsgpackError)
}

pub fn returns_array_of_enums(&self, args: &TestImportModuleArgsReturnsArrayOfEnums, uri: Option<Uri>, invoker: Option<Arc<dyn Invoker>>, env: Option<Vec<u8>>) -> Result<Vec<Option<TestImportEnumReturn>>, Error> {
let _uri = uri.unwrap_or(self.uri.clone());
let _invoker = invoker.unwrap_or(self.invoker.clone());
let _env = match &env {
Some(e) => Some(e.as_slice()),
None => match &self.env {
Some(e) => Some(e.as_slice()),
None => None,
},
pub fn returns_array_of_enums(&self, args: &TestImportArgsReturnsArrayOfEnums, invoke_options: Option<InvokeOptions>) -> Result<Vec<Option<TestImportEnumReturn>>, Error> {
let (_uri, _invoker, _env) = if let Some(invoke_option) = invoke_options {
let uri = invoke_option.uri.clone().unwrap_or_else(|| self.uri.clone());
let invoker = invoke_option.client.clone().unwrap_or_else(|| self.invoker.clone());
let env = invoke_option.env.clone().or_else(|| self.env.clone());
(uri, invoker, env)
} else {
(self.uri.clone(), self.invoker.clone(), self.env.clone())
};

let serialized_args = to_vec(&args).unwrap();
Expand All @@ -304,7 +347,7 @@ impl TestImportModule {
&_uri,
"returnsArrayOfEnums",
opt_args,
_env,
_env.as_ref().map(|v| v.as_slice()),
None
)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ use std::sync::Arc;

pub type BigInt = String;

#[derive(Clone)]
pub struct InvokeOptions {
pub uri: Option<Uri>,
pub client: Option<Arc<dyn Invoker>>,
pub env: Option<Vec<u8>>
}

fn get_default_client() -> Arc<PolywrapClient> {
let mut config = PolywrapClientConfig::new();
config.add(SystemClientConfig::default().into());
config.add(Web3ClientConfig::default().into());
let client = PolywrapClient::new(config.build());
Arc::new(client)
}

// Env START //

// Env END //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ use std::sync::Arc;

pub type BigInt = String;

#[derive(Clone)]
pub struct InvokeOptions {
pub uri: Option<Uri>,
pub client: Option<Arc<dyn Invoker>>,
pub env: Option<Vec<u8>>
}

fn get_default_client() -> Arc<PolywrapClient> {
let mut config = PolywrapClientConfig::new();
config.add(SystemClientConfig::default().into());
config.add(Web3ClientConfig::default().into());
let client = PolywrapClient::new(config.build());
Arc::new(client)
}

// Env START //

// Env END //
Expand Down
5 changes: 5 additions & 0 deletions implementations/app-rust/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod is_keyword;
mod is_not_first;
mod is_not_last;
mod pretty;
mod remove_module_suffix;
mod serde_rename_if_case_mismatch;
mod to_graphql_type;
mod to_lower;
Expand Down Expand Up @@ -46,6 +47,10 @@ pub fn register(handlebars: &mut Handlebars) -> () {
"pretty",
Box::new(pretty::pretty)
);
handlebars.register_helper(
"remove_module_suffix",
Box::new(remove_module_suffix::remove_module_suffix)
);
handlebars.register_helper(
"serde_rename_if_case_mismatch",
Box::new(serde_rename_if_case_mismatch::serde_rename_if_case_mismatch)
Expand Down
15 changes: 15 additions & 0 deletions implementations/app-rust/src/helpers/remove_module_suffix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use handlebars::handlebars_helper;
use serde_json::Value;

handlebars_helper!(remove_module_suffix: |val: Value| {
let type_val = val.as_str().unwrap();
_remove_module_suffix(type_val)
});

pub fn _remove_module_suffix(type_val: &str) -> String {
if type_val.ends_with("Module") {
let new_type_val = type_val.strip_suffix("Module").unwrap();
return format!("{}", new_type_val);
}
return format!("{}", type_val);
}
Loading

0 comments on commit ab4d867

Please sign in to comment.