Skip to content

Commit

Permalink
proxy gen - replace char removed
Browse files Browse the repository at this point in the history
  • Loading branch information
BiancaIalangi committed Apr 2, 2024
1 parent 0cf9262 commit fe664c3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 40 deletions.
42 changes: 10 additions & 32 deletions framework/meta/src/cmd/contract/generate_proxy/proxy_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ where
if self
.meta_config
.original_contract_abi
.build_info
.contract_crate
.name
.get_crate_name_for_code()
!= extract_struct_crate(type_description.names.rust.as_str())
{
continue;
Expand Down Expand Up @@ -482,20 +480,24 @@ where
self.writeln(" },");
}

pub fn clean_paths(&mut self, proxy_crate: &str, rust_type: &str) -> String {
pub fn clean_paths(&mut self, rust_type: &str) -> String {
let delimiters = "<>,()[] ";
let words: Vec<&str> = rust_type
.split(|c| delimiters.contains(c))
.filter(|s| !s.is_empty())
.collect();

let crate_name = self
.meta_config
.original_contract_abi
.get_crate_name_for_code();
let mut words_replacer: Vec<String> = Vec::new();
for word in &words {
let type_rust_name = word.split("::").last().unwrap().to_string();
if proxy_crate == extract_struct_crate(word)
|| TYPES_FROM_FRAMEWORK.contains(&type_rust_name.as_str())
let type_rust_name = word.split("::").last().unwrap();
if crate_name == extract_struct_crate(word)
|| TYPES_FROM_FRAMEWORK.contains(&type_rust_name)
{
words_replacer.push(type_rust_name);
words_replacer.push(type_rust_name.to_string());
} else {
words_replacer.push(word.to_string());
}
Expand Down Expand Up @@ -545,11 +547,6 @@ where

fn adjust_type_name_with_env_api(&mut self, original_rust_name: &str) -> String {
self.clean_paths(
self.meta_config
.original_contract_abi
.build_info
.contract_crate
.name,
&original_rust_name
.replace("multiversx_sc::api::uncallable::UncallableApi", "Env::Api")
.replace("$API", "Env::Api"),
Expand All @@ -558,11 +555,6 @@ where

fn adjust_type_name_with_api(&mut self, original_rust_name: &str) -> String {
self.clean_paths(
self.meta_config
.original_contract_abi
.build_info
.contract_crate
.name,
&original_rust_name
.replace("multiversx_sc::api::uncallable::UncallableApi", "Api")
.replace("$API", "Api"),
Expand Down Expand Up @@ -599,15 +591,8 @@ pub mod tests {
meta_config: &meta_config,
file: None,
};
let name = proxy_generator
.meta_config
.original_contract_abi
.build_info
.contract_crate
.name;

let cleaned_path_unsanitized = proxy_generator.clean_paths(
name,
"(other_crate::contract_crate::TestStruct, Option<contract_crate::other_crate::Box<AbiTestType>>)",
);
let expected_result_unsanitized =
Expand Down Expand Up @@ -636,15 +621,8 @@ pub mod tests {
meta_config: &meta_config,
file: None,
};
let name = proxy_generator
.meta_config
.original_contract_abi
.build_info
.contract_crate
.name;

let cleaned_path_sanitized = proxy_generator.clean_paths(
name,
"(contract_crate::other_crate::TestStruct, Option<contract_crate::Box<AbiTestType>>)",
);
let expected_result_sanitized = "(TestStruct, Option<Box<AbiTestType>>)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ pub(super) fn proxy_methods_type_name(contract_trait_name: &str) -> String {
}

pub(super) fn extract_struct_crate(struct_path: &str) -> String {
let struct_crate_name = struct_path
.replace('_', "-")
.replace("multiversx_sc::api::uncallable::UncallableApi", "Api")
.to_string();
let crate_name = struct_crate_name
.split("::")
.next()
.unwrap_or_else(|| &struct_crate_name);
let crate_name = struct_path.split("::").next().unwrap_or(struct_path);
crate_name.to_string()
}

0 comments on commit fe664c3

Please sign in to comment.