Skip to content

Commit

Permalink
refactor: disentangle bindgen extractor logic (#1025)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniyar Itegulov <ditegulov@gmail.com>
  • Loading branch information
agostbiro and itegulov authored Jun 22, 2023
1 parent c2352df commit 76ee2da
Show file tree
Hide file tree
Showing 20 changed files with 538 additions and 110 deletions.
2 changes: 2 additions & 0 deletions examples/adder/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/callback-results/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/cross-contract-calls/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/factory-contract/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/fungible-token/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/lockable-fungible-token/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/mission-control/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/non-fungible-token/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/status-message-collections/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/status-message/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/test-contract/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/versioned/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions near-sdk-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ proc-macro = true
[dependencies]
proc-macro2 = "1.0"
syn = { version = "1", features = ["full", "fold", "extra-traits", "visit"] }
strum = "0.24"
strum_macros = "0.24"
quote = "1.0"
Inflector = { version = "0.11.4", default-features = false, features = [] }

Expand Down
56 changes: 10 additions & 46 deletions near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,12 +497,9 @@ mod tests {
#[init]
pub fn method(k: &mut u64) { }
};
let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap();
let actual = method_info.method_wrapper();
let expected = quote!(
compile_error! { "Init methods must return the contract state" }
);
assert_eq!(expected.to_string(), actual.to_string());
let actual = ImplItemMethodInfo::new(&mut method, false, impl_type).map(|_| ()).unwrap_err();
let expected = "Init function must return the contract state.";
assert_eq!(expected, actual.to_string());
}

#[test]
Expand Down Expand Up @@ -786,23 +783,6 @@ mod tests {
assert_eq!(expected.to_string(), actual.to_string());
}

#[test]
fn init_result_without_handle_result() {
let impl_type: Type = syn::parse_str("Hello").unwrap();
let mut method: ImplItemMethod = parse_quote! {
#[init]
pub fn new() -> Result<Self, &'static str> { }
};
let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap();
let actual = method_info.method_wrapper();
let expected = quote!(
compile_error! {
"Serializing Result<T, E> has been deprecated. Consider marking your method with #[handle_result] if the second generic represents a panicable error or replacing Result with another two type sum enum otherwise. If you really want to keep the legacy behavior, mark the method with #[handle_result] and make it return Result<Result<T, E>, near_sdk::Abort>."
}
);
assert_eq!(expected.to_string(), actual.to_string());
}

#[test]
fn handle_result_init_ignore_state() {
let impl_type: Type = syn::parse_str("Hello").unwrap();
Expand Down Expand Up @@ -832,33 +812,17 @@ mod tests {
}

#[test]
fn handle_result_incorrect_return_type() {
fn handle_no_self() {
let impl_type: Type = syn::parse_str("Hello").unwrap();
let mut method: ImplItemMethod = parse_quote! {
#[handle_result]
pub fn method(&self) -> &'static str { }
};
let mut method: ImplItemMethod = syn::parse_str("pub fn method() { }").unwrap();
let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap();
let actual = method_info.method_wrapper();
let expected = quote!(
compile_error! {
"Method marked with #[handle_result] should return Result<T, E> (where E implements FunctionError)."
}
);
assert_eq!(expected.to_string(), actual.to_string());
}

#[test]
fn handle_result_without_marker() {
let impl_type: Type = syn::parse_str("Hello").unwrap();
let mut method: ImplItemMethod = parse_quote! {
pub fn method(&self) -> Result<u64, &'static str> { }
};
let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap();
let actual = method_info.method_wrapper();
let expected = quote!(
compile_error! {
"Serializing Result<T, E> has been deprecated. Consider marking your method with #[handle_result] if the second generic represents a panicable error or replacing Result with another two type sum enum otherwise. If you really want to keep the legacy behavior, mark the method with #[handle_result] and make it return Result<Result<T, E>, near_sdk::Abort>."
#[cfg(target_arch = "wasm32")]
#[no_mangle]
pub extern "C" fn method() {
near_sdk::env::setup_panic_hook();
Hello::method();
}
);
assert_eq!(expected.to_string(), actual.to_string());
Expand Down
Loading

0 comments on commit 76ee2da

Please sign in to comment.