From 185fd6b3e3454e9a7376cf32e09d358094845c56 Mon Sep 17 00:00:00 2001 From: iho Date: Wed, 4 Oct 2023 09:16:27 +0300 Subject: [PATCH] refactor: upgrade syn crate from version 1 to 2 (#1088) --- examples/adder/Cargo.lock | 15 +-- examples/callback-results/Cargo.lock | 37 +++--- examples/cross-contract-calls/Cargo.lock | 15 +-- examples/factory-contract/Cargo.lock | 15 +-- examples/fungible-token/Cargo.lock | 15 +-- examples/lockable-fungible-token/Cargo.lock | 15 +-- examples/mission-control/Cargo.lock | 15 +-- examples/non-fungible-token/Cargo.lock | 15 +-- examples/status-message/Cargo.lock | 15 +-- examples/test-contract/Cargo.lock | 15 +-- examples/versioned/Cargo.lock | 15 +-- near-sdk-macros/Cargo.toml | 5 +- .../src/core_impl/abi/abi_generator.rs | 16 +-- .../src/core_impl/code_generator/ext.rs | 10 +- .../code_generator/item_impl_info.rs | 60 +++++----- near-sdk-macros/src/core_impl/event/mod.rs | 106 +++++++++--------- .../src/core_impl/info_extractor/arg_info.rs | 36 +++++- .../core_impl/info_extractor/attr_sig_info.rs | 39 ++++++- .../info_extractor/impl_item_method_info.rs | 7 +- .../src/core_impl/info_extractor/init_attr.rs | 22 ---- .../info_extractor/item_impl_info.rs | 2 +- .../info_extractor/item_trait_info.rs | 7 +- .../info_extractor/serializer_attr.rs | 19 ---- .../info_extractor/trait_item_method_info.rs | 8 +- .../src/core_impl/info_extractor/visitor.rs | 2 +- .../core_impl/metadata/metadata_visitor.rs | 2 +- near-sdk-macros/src/lib.rs | 94 ++++++---------- .../schema_derive_invalids.rs | 2 +- .../schema_derive_invalids.stderr | 39 ++++--- near-sdk/tests/event_tests.rs | 2 +- 30 files changed, 340 insertions(+), 325 deletions(-) diff --git a/examples/adder/Cargo.lock b/examples/adder/Cargo.lock index 44255dda1..59ec92137 100644 --- a/examples/adder/Cargo.lock +++ b/examples/adder/Cargo.lock @@ -823,9 +823,9 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0558d22a7b463ed0241e993f76f09f30b126687447751a8638587b864e4b3944" +checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" dependencies = [ "darling_core", "darling_macro", @@ -833,9 +833,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8bfa2e259f8ee1ce5e97824a3c55ec4404a0d772ca7fa96bf19f0752a046eb" +checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" dependencies = [ "fnv", "ident_case", @@ -847,9 +847,9 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29a358ff9f12ec09c3e61fef9b5a9902623a695a46a917b07f269bff1445611a" +checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core", "quote", @@ -2117,11 +2117,12 @@ name = "near-sdk-macros" version = "4.1.1" dependencies = [ "Inflector", + "darling", "proc-macro2", "quote", "strum", "strum_macros", - "syn 1.0.109", + "syn 2.0.18", ] [[package]] diff --git a/examples/callback-results/Cargo.lock b/examples/callback-results/Cargo.lock index 7b75902c4..493f0f1ca 100644 --- a/examples/callback-results/Cargo.lock +++ b/examples/callback-results/Cargo.lock @@ -788,9 +788,9 @@ dependencies = [ [[package]] name = "curl-sys" -version = "0.4.62+curl-8.1.0" +version = "0.4.66+curl-8.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "274ef7ef7c1113c7611af49ce248a700afa1171045a1aaa40137030773f993b8" +checksum = "70c44a72e830f0e40ad90dda8a6ab6ed6314d39776599a58a2e5e37fbc6db5b9" dependencies = [ "cc", "libc", @@ -798,7 +798,7 @@ dependencies = [ "openssl-sys", "pkg-config", "vcpkg", - "winapi", + "windows-sys 0.48.0", ] [[package]] @@ -816,9 +816,9 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0558d22a7b463ed0241e993f76f09f30b126687447751a8638587b864e4b3944" +checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" dependencies = [ "darling_core", "darling_macro", @@ -826,9 +826,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8bfa2e259f8ee1ce5e97824a3c55ec4404a0d772ca7fa96bf19f0752a046eb" +checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" dependencies = [ "fnv", "ident_case", @@ -840,9 +840,9 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29a358ff9f12ec09c3e61fef9b5a9902623a695a46a917b07f269bff1445611a" +checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core", "quote", @@ -1253,9 +1253,9 @@ dependencies = [ [[package]] name = "gimli" -version = "0.27.2" +version = "0.27.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad0a93d233ebf96623465aad4046a8d3aa4da22d4f4beba5388838c8a434bbb4" +checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e" [[package]] name = "h2" @@ -1599,9 +1599,9 @@ checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" [[package]] name = "libz-sys" -version = "1.1.9" +version = "1.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56ee889ecc9568871456d42f603d6a0ce59ff328d291063a45cbdf0036baf6db" +checksum = "d97137b25e321a73eef1418d1d5d2eda4d77e12813f8e6dead84bc52c5870a7b" dependencies = [ "cc", "libc", @@ -2101,11 +2101,12 @@ name = "near-sdk-macros" version = "4.1.1" dependencies = [ "Inflector", + "darling", "proc-macro2", "quote", "strum", "strum_macros", - "syn 1.0.109", + "syn 2.0.18", ] [[package]] @@ -2233,9 +2234,9 @@ dependencies = [ [[package]] name = "object" -version = "0.30.3" +version = "0.30.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea86265d3d3dcb6a27fc51bd29a4bf387fae9d2986b823079d4986af253eb439" +checksum = "03b4680b86d9cfafba8fc491dc9b6df26b68cf40e9e6cd73909194759a63c385" dependencies = [ "memchr", ] @@ -3712,9 +3713,9 @@ checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" [[package]] name = "uuid" -version = "1.3.3" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "345444e32442451b267fc254ae85a209c64be56d2890e601a0c37ff0c3c5ecd2" +checksum = "79daa5ed5740825c40b389c5e50312b9c86df53fccd33f281df655642b43869d" dependencies = [ "getrandom 0.2.9", ] diff --git a/examples/cross-contract-calls/Cargo.lock b/examples/cross-contract-calls/Cargo.lock index aaea09875..f4ae953b1 100644 --- a/examples/cross-contract-calls/Cargo.lock +++ b/examples/cross-contract-calls/Cargo.lock @@ -840,9 +840,9 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0558d22a7b463ed0241e993f76f09f30b126687447751a8638587b864e4b3944" +checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" dependencies = [ "darling_core", "darling_macro", @@ -850,9 +850,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8bfa2e259f8ee1ce5e97824a3c55ec4404a0d772ca7fa96bf19f0752a046eb" +checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" dependencies = [ "fnv", "ident_case", @@ -864,9 +864,9 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29a358ff9f12ec09c3e61fef9b5a9902623a695a46a917b07f269bff1445611a" +checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core", "quote", @@ -2125,11 +2125,12 @@ name = "near-sdk-macros" version = "4.1.1" dependencies = [ "Inflector", + "darling", "proc-macro2", "quote", "strum", "strum_macros", - "syn 1.0.109", + "syn 2.0.18", ] [[package]] diff --git a/examples/factory-contract/Cargo.lock b/examples/factory-contract/Cargo.lock index 13ecda519..fc2494533 100644 --- a/examples/factory-contract/Cargo.lock +++ b/examples/factory-contract/Cargo.lock @@ -811,9 +811,9 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0558d22a7b463ed0241e993f76f09f30b126687447751a8638587b864e4b3944" +checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" dependencies = [ "darling_core", "darling_macro", @@ -821,9 +821,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8bfa2e259f8ee1ce5e97824a3c55ec4404a0d772ca7fa96bf19f0752a046eb" +checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" dependencies = [ "fnv", "ident_case", @@ -835,9 +835,9 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29a358ff9f12ec09c3e61fef9b5a9902623a695a46a917b07f269bff1445611a" +checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core", "quote", @@ -2125,11 +2125,12 @@ name = "near-sdk-macros" version = "4.1.1" dependencies = [ "Inflector", + "darling", "proc-macro2", "quote", "strum", "strum_macros", - "syn 1.0.109", + "syn 2.0.18", ] [[package]] diff --git a/examples/fungible-token/Cargo.lock b/examples/fungible-token/Cargo.lock index 650666dfe..390e925ec 100644 --- a/examples/fungible-token/Cargo.lock +++ b/examples/fungible-token/Cargo.lock @@ -811,9 +811,9 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0558d22a7b463ed0241e993f76f09f30b126687447751a8638587b864e4b3944" +checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" dependencies = [ "darling_core", "darling_macro", @@ -821,9 +821,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8bfa2e259f8ee1ce5e97824a3c55ec4404a0d772ca7fa96bf19f0752a046eb" +checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" dependencies = [ "fnv", "ident_case", @@ -835,9 +835,9 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29a358ff9f12ec09c3e61fef9b5a9902623a695a46a917b07f269bff1445611a" +checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core", "quote", @@ -2140,11 +2140,12 @@ name = "near-sdk-macros" version = "4.1.1" dependencies = [ "Inflector", + "darling", "proc-macro2", "quote", "strum", "strum_macros", - "syn 1.0.109", + "syn 2.0.18", ] [[package]] diff --git a/examples/lockable-fungible-token/Cargo.lock b/examples/lockable-fungible-token/Cargo.lock index e09b98425..534063a35 100644 --- a/examples/lockable-fungible-token/Cargo.lock +++ b/examples/lockable-fungible-token/Cargo.lock @@ -811,9 +811,9 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0558d22a7b463ed0241e993f76f09f30b126687447751a8638587b864e4b3944" +checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" dependencies = [ "darling_core", "darling_macro", @@ -821,9 +821,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8bfa2e259f8ee1ce5e97824a3c55ec4404a0d772ca7fa96bf19f0752a046eb" +checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" dependencies = [ "fnv", "ident_case", @@ -835,9 +835,9 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29a358ff9f12ec09c3e61fef9b5a9902623a695a46a917b07f269bff1445611a" +checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core", "quote", @@ -2109,11 +2109,12 @@ name = "near-sdk-macros" version = "4.1.1" dependencies = [ "Inflector", + "darling", "proc-macro2", "quote", "strum", "strum_macros", - "syn 1.0.109", + "syn 2.0.18", ] [[package]] diff --git a/examples/mission-control/Cargo.lock b/examples/mission-control/Cargo.lock index 696beaf1a..cc875e3a0 100644 --- a/examples/mission-control/Cargo.lock +++ b/examples/mission-control/Cargo.lock @@ -565,9 +565,9 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0558d22a7b463ed0241e993f76f09f30b126687447751a8638587b864e4b3944" +checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" dependencies = [ "darling_core", "darling_macro", @@ -575,9 +575,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8bfa2e259f8ee1ce5e97824a3c55ec4404a0d772ca7fa96bf19f0752a046eb" +checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" dependencies = [ "fnv", "ident_case", @@ -589,9 +589,9 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29a358ff9f12ec09c3e61fef9b5a9902623a695a46a917b07f269bff1445611a" +checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core", "quote", @@ -1441,11 +1441,12 @@ name = "near-sdk-macros" version = "4.1.1" dependencies = [ "Inflector", + "darling", "proc-macro2", "quote", "strum", "strum_macros", - "syn 1.0.109", + "syn 2.0.18", ] [[package]] diff --git a/examples/non-fungible-token/Cargo.lock b/examples/non-fungible-token/Cargo.lock index 07135f2b7..aa339493b 100644 --- a/examples/non-fungible-token/Cargo.lock +++ b/examples/non-fungible-token/Cargo.lock @@ -819,9 +819,9 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0558d22a7b463ed0241e993f76f09f30b126687447751a8638587b864e4b3944" +checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" dependencies = [ "darling_core", "darling_macro", @@ -829,9 +829,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8bfa2e259f8ee1ce5e97824a3c55ec4404a0d772ca7fa96bf19f0752a046eb" +checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" dependencies = [ "fnv", "ident_case", @@ -843,9 +843,9 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29a358ff9f12ec09c3e61fef9b5a9902623a695a46a917b07f269bff1445611a" +checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core", "quote", @@ -2114,11 +2114,12 @@ name = "near-sdk-macros" version = "4.1.1" dependencies = [ "Inflector", + "darling", "proc-macro2", "quote", "strum", "strum_macros", - "syn 1.0.109", + "syn 2.0.18", ] [[package]] diff --git a/examples/status-message/Cargo.lock b/examples/status-message/Cargo.lock index 27570fa8e..6605a897b 100644 --- a/examples/status-message/Cargo.lock +++ b/examples/status-message/Cargo.lock @@ -565,9 +565,9 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0558d22a7b463ed0241e993f76f09f30b126687447751a8638587b864e4b3944" +checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" dependencies = [ "darling_core", "darling_macro", @@ -575,9 +575,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8bfa2e259f8ee1ce5e97824a3c55ec4404a0d772ca7fa96bf19f0752a046eb" +checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" dependencies = [ "fnv", "ident_case", @@ -589,9 +589,9 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29a358ff9f12ec09c3e61fef9b5a9902623a695a46a917b07f269bff1445611a" +checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core", "quote", @@ -1434,11 +1434,12 @@ name = "near-sdk-macros" version = "4.1.1" dependencies = [ "Inflector", + "darling", "proc-macro2", "quote", "strum", "strum_macros", - "syn 1.0.109", + "syn 2.0.18", ] [[package]] diff --git a/examples/test-contract/Cargo.lock b/examples/test-contract/Cargo.lock index 6251713cf..53d290fdb 100644 --- a/examples/test-contract/Cargo.lock +++ b/examples/test-contract/Cargo.lock @@ -565,9 +565,9 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0558d22a7b463ed0241e993f76f09f30b126687447751a8638587b864e4b3944" +checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" dependencies = [ "darling_core", "darling_macro", @@ -575,9 +575,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8bfa2e259f8ee1ce5e97824a3c55ec4404a0d772ca7fa96bf19f0752a046eb" +checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" dependencies = [ "fnv", "ident_case", @@ -589,9 +589,9 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29a358ff9f12ec09c3e61fef9b5a9902623a695a46a917b07f269bff1445611a" +checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core", "quote", @@ -1434,11 +1434,12 @@ name = "near-sdk-macros" version = "4.1.1" dependencies = [ "Inflector", + "darling", "proc-macro2", "quote", "strum", "strum_macros", - "syn 1.0.109", + "syn 2.0.18", ] [[package]] diff --git a/examples/versioned/Cargo.lock b/examples/versioned/Cargo.lock index efedcb97e..43fe1a1e2 100644 --- a/examples/versioned/Cargo.lock +++ b/examples/versioned/Cargo.lock @@ -565,9 +565,9 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0558d22a7b463ed0241e993f76f09f30b126687447751a8638587b864e4b3944" +checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" dependencies = [ "darling_core", "darling_macro", @@ -575,9 +575,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8bfa2e259f8ee1ce5e97824a3c55ec4404a0d772ca7fa96bf19f0752a046eb" +checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" dependencies = [ "fnv", "ident_case", @@ -589,9 +589,9 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29a358ff9f12ec09c3e61fef9b5a9902623a695a46a917b07f269bff1445611a" +checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core", "quote", @@ -1434,11 +1434,12 @@ name = "near-sdk-macros" version = "4.1.1" dependencies = [ "Inflector", + "darling", "proc-macro2", "quote", "strum", "strum_macros", - "syn 1.0.109", + "syn 2.0.18", ] [[package]] diff --git a/near-sdk-macros/Cargo.toml b/near-sdk-macros/Cargo.toml index 9b7e7bc11..3edfba0fc 100644 --- a/near-sdk-macros/Cargo.toml +++ b/near-sdk-macros/Cargo.toml @@ -15,12 +15,13 @@ Main macro of the library for writing NEAR smart contracts. proc-macro = true [dependencies] -proc-macro2 = "1.0" -syn = { version = "1", features = ["full", "fold", "extra-traits", "visit"] } +proc-macro2 = "1" +syn = { version = "2", 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 = [] } +darling = { version = "0.20.3" } [features] abi = [] diff --git a/near-sdk-macros/src/core_impl/abi/abi_generator.rs b/near-sdk-macros/src/core_impl/abi/abi_generator.rs index cbe659a63..9eac0634c 100644 --- a/near-sdk-macros/src/core_impl/abi/abi_generator.rs +++ b/near-sdk-macros/src/core_impl/abi/abi_generator.rs @@ -1,6 +1,6 @@ use proc_macro2::{Span, TokenStream as TokenStream2}; use quote::{format_ident, quote}; -use syn::{Attribute, Lit::Str, Meta::NameValue, MetaNameValue, Type}; +use syn::{Attribute, Expr, Lit::Str, Meta::NameValue, MetaNameValue, Type}; use crate::core_impl::{ utils, BindgenArgType, ImplItemMethodInfo, ItemImplInfo, MethodKind, ReturnKind, SerializerType, @@ -271,15 +271,15 @@ pub fn parse_rustdoc(attrs: &[Attribute]) -> Option { let doc = attrs .iter() .filter_map(|attr| { - if attr.path.is_ident("doc") { - if let NameValue(MetaNameValue { lit: Str(s), .. }) = attr.parse_meta().ok()? { - Some(s.value()) - } else { - None + if attr.path().is_ident("doc") { + if let NameValue(MetaNameValue { value: Expr::Lit(value), .. }) = attr.meta.clone() + { + if let Str(doc) = value.lit { + return Some(doc.value()); + } } - } else { - None } + None }) .collect::>() .join("\n"); diff --git a/near-sdk-macros/src/core_impl/code_generator/ext.rs b/near-sdk-macros/src/core_impl/code_generator/ext.rs index d77c8123b..592824703 100644 --- a/near-sdk-macros/src/core_impl/code_generator/ext.rs +++ b/near-sdk-macros/src/core_impl/code_generator/ext.rs @@ -87,7 +87,7 @@ fn is_fn_attribute_to_forward(attribute: &Attribute) -> bool { for to_forward in FN_ATTRIBUTES_TO_FORWARD.iter() { let to_forward_ident = Ident::new(to_forward, Span::mixed_site()); let to_forward_path: Path = parse_quote! { #to_forward_ident }; - if to_forward_path == attribute.path { + if &to_forward_path == attribute.meta.path() { return true; } } @@ -148,7 +148,7 @@ mod tests { use super::*; use quote::quote; - use syn::{parse_quote, ImplItemMethod, ItemStruct, Type}; + use syn::{parse_quote, ImplItemFn, ItemStruct, Type}; #[test] fn ext_gen() { @@ -235,7 +235,7 @@ mod tests { #[test] fn ext_fn_non_bindgen_attrs() { let impl_type: Type = parse_quote! { Hello }; - let mut method: ImplItemMethod = parse_quote! { + let mut method: ImplItemFn = parse_quote! { #[cfg(target_os = "linux")] #[inline] #[warn(unused)] @@ -264,7 +264,7 @@ mod tests { #[test] fn ext_basic_json() { let impl_type: Type = parse_quote! { Hello }; - let mut method: ImplItemMethod = parse_quote! { + let mut method: ImplItemFn = parse_quote! { pub fn method(&self, k: &String) { } }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); @@ -295,7 +295,7 @@ mod tests { #[test] fn ext_basic_borsh() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = parse_quote! { + let mut method: syn::ImplItemFn = parse_quote! { pub fn borsh_test(&mut self, #[serializer(borsh)] a: String) {} }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); diff --git a/near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs b/near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs index de66c53af..6855ff46b 100644 --- a/near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +++ b/near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs @@ -28,7 +28,7 @@ impl ItemImplInfo { #[rustfmt::skip] #[cfg(test)] mod tests { - use syn::{Type, ImplItemMethod, parse_quote}; + use syn::{Type, ImplItemFn, parse_quote}; use quote::quote; use crate::core_impl::info_extractor::ImplItemMethodInfo; @@ -36,7 +36,7 @@ mod tests { #[test] fn trait_implt() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = syn::parse_str("fn method(&self) { }").unwrap(); + let mut method: ImplItemFn = syn::parse_str("fn method(&self) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, true, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); let expected = quote!( @@ -54,7 +54,7 @@ mod tests { #[test] fn no_args_no_return_no_mut() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = syn::parse_str("pub fn method(&self) { }").unwrap(); + let mut method: ImplItemFn = syn::parse_str("pub fn method(&self) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); let expected = quote!( @@ -72,7 +72,7 @@ mod tests { #[test] fn owned_no_args_no_return_no_mut() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = syn::parse_str("pub fn method(self) { }").unwrap(); + let mut method: ImplItemFn = syn::parse_str("pub fn method(self) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); let expected = quote!( @@ -91,7 +91,7 @@ mod tests { #[test] fn mut_owned_no_args_no_return() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = syn::parse_str("pub fn method(mut self) { }").unwrap(); + let mut method: ImplItemFn = syn::parse_str("pub fn method(mut self) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); let expected = quote!( @@ -109,7 +109,7 @@ mod tests { #[test] fn no_args_no_return_mut() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = syn::parse_str("pub fn method(&mut self) { }").unwrap(); + let mut method: ImplItemFn = syn::parse_str("pub fn method(&mut self) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); let expected = quote!( @@ -131,7 +131,7 @@ mod tests { #[test] fn arg_no_return_no_mut() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = syn::parse_str("pub fn method(&self, k: u64) { }").unwrap(); + let mut method: ImplItemFn = syn::parse_str("pub fn method(&self, k: u64) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); let expected = quote!( @@ -158,7 +158,7 @@ mod tests { #[test] fn args_no_return_mut() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = + let mut method: ImplItemFn = syn::parse_str("pub fn method(&mut self, k: u64, m: Bar) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); @@ -191,7 +191,7 @@ mod tests { #[test] fn args_return_mut() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = + let mut method: ImplItemFn = syn::parse_str("pub fn method(&mut self, k: u64, m: Bar) -> Option { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); @@ -227,7 +227,7 @@ mod tests { #[test] fn args_return_ref() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = + let mut method: ImplItemFn = syn::parse_str("pub fn method(&self) -> &Option { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); @@ -249,7 +249,7 @@ mod tests { #[test] fn arg_ref() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = syn::parse_str("pub fn method(&self, k: &u64) { }").unwrap(); + let mut method: ImplItemFn = syn::parse_str("pub fn method(&self, k: &u64) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); let expected = quote!( @@ -276,7 +276,7 @@ mod tests { #[test] fn arg_mut_ref() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = + let mut method: ImplItemFn = syn::parse_str("pub fn method(&self, k: &mut u64) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); @@ -304,7 +304,7 @@ mod tests { #[test] fn callback_args() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = parse_quote! { + let mut method: ImplItemFn = parse_quote! { #[private] pub fn method(&self, #[callback_unwrap] x: &mut u64, y: ::std::string::String, #[callback_unwrap] z: ::std::vec::Vec) { } }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); @@ -348,7 +348,7 @@ mod tests { #[test] fn callback_args_only() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = parse_quote! { + let mut method: ImplItemFn = parse_quote! { #[private] pub fn method(&self, #[callback_unwrap] x: &mut u64, #[callback_unwrap] y: ::std::string::String) { } }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); @@ -384,7 +384,7 @@ mod tests { #[test] fn callback_args_results() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = parse_quote! { + let mut method: ImplItemFn = parse_quote! { #[private] pub fn method(&self, #[callback_result] x: &mut Result, #[callback_result] y: Result<::std::string::String, PromiseError>) { } }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); @@ -416,7 +416,7 @@ mod tests { #[test] fn callback_args_vec() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = parse_quote! { + let mut method: ImplItemFn = parse_quote! { #[private] pub fn method(&self, #[callback_vec] x: Vec, y: String) { } }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); @@ -455,7 +455,7 @@ mod tests { #[test] fn simple_init() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = parse_quote! { + let mut method: ImplItemFn = parse_quote! { #[init] pub fn method(k: &mut u64) -> Self { } }; @@ -491,7 +491,7 @@ mod tests { #[test] fn init_no_return() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = parse_quote! { + let mut method: ImplItemFn = parse_quote! { #[init] pub fn method(k: &mut u64) { } }; @@ -503,7 +503,7 @@ mod tests { #[test] fn init_ignore_state() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = parse_quote! { + let mut method: ImplItemFn = parse_quote! { #[init(ignore_state)] pub fn method(k: &mut u64) -> Self { } }; @@ -536,7 +536,7 @@ mod tests { #[test] fn init_payable() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = parse_quote! { + let mut method: ImplItemFn = parse_quote! { #[init] #[payable] pub fn method(k: &mut u64) -> Self { } @@ -570,7 +570,7 @@ mod tests { #[test] fn args_return_mut_borsh() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = parse_quote! { + let mut method: ImplItemFn = parse_quote! { #[result_serializer(borsh)] pub fn method(&mut self, #[serializer(borsh)] k: u64, #[serializer(borsh)]m: Bar) -> Option { } }; @@ -607,7 +607,7 @@ mod tests { #[test] fn callback_args_mixed_serialization() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = parse_quote! { + let mut method: ImplItemFn = parse_quote! { #[private] pub fn method(&self, #[callback_unwrap] #[serializer(borsh)] x: &mut u64, #[serializer(borsh)] y: ::std::string::String, #[callback_unwrap] #[serializer(json)] z: ::std::vec::Vec) { } }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); @@ -650,7 +650,7 @@ mod tests { #[test] fn no_args_no_return_mut_payable() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = syn::parse_str("#[payable] pub fn method(&mut self) { }").unwrap(); + let mut method: ImplItemFn = syn::parse_str("#[payable] pub fn method(&mut self) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); let expected = quote!( @@ -669,7 +669,7 @@ mod tests { #[test] fn private_method() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = syn::parse_str("#[private] pub fn private_method(&mut self) { }").unwrap(); + let mut method: ImplItemFn = syn::parse_str("#[private] pub fn private_method(&mut self) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); let expected = quote!( @@ -694,7 +694,7 @@ mod tests { #[test] fn handle_result_json() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = parse_quote! { + let mut method: ImplItemFn = parse_quote! { #[handle_result] pub fn method(&self) -> Result:: { } }; @@ -723,7 +723,7 @@ mod tests { #[test] fn handle_result_mut() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = parse_quote! { + let mut method: ImplItemFn = parse_quote! { #[handle_result] pub fn method(&mut self) -> Result { } }; @@ -756,7 +756,7 @@ mod tests { #[test] fn handle_result_borsh() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = parse_quote! { + let mut method: ImplItemFn = parse_quote! { #[handle_result] #[result_serializer(borsh)] pub fn method(&self) -> Result { } @@ -786,7 +786,7 @@ mod tests { #[test] fn handle_result_init() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = parse_quote! { + let mut method: ImplItemFn = parse_quote! { #[init] #[handle_result] pub fn new() -> Result { } @@ -819,7 +819,7 @@ mod tests { #[test] fn handle_result_init_ignore_state() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = parse_quote! { + let mut method: ImplItemFn = parse_quote! { #[init(ignore_state)] #[handle_result] pub fn new() -> Result { } @@ -849,7 +849,7 @@ mod tests { #[test] fn handle_no_self() { let impl_type: Type = syn::parse_str("Hello").unwrap(); - let mut method: ImplItemMethod = syn::parse_str("pub fn method() { }").unwrap(); + let mut method: ImplItemFn = 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!( diff --git a/near-sdk-macros/src/core_impl/event/mod.rs b/near-sdk-macros/src/core_impl/event/mod.rs index 75b1aa54e..e35c49aa7 100644 --- a/near-sdk-macros/src/core_impl/event/mod.rs +++ b/near-sdk-macros/src/core_impl/event/mod.rs @@ -1,45 +1,72 @@ use proc_macro::TokenStream; use proc_macro2::Span; + use quote::quote; + +use darling::ast::NestedMeta; +use darling::Error; +use darling::FromMeta; use syn::{parse_quote, ItemEnum, LitStr}; +#[derive(Default, FromMeta, Clone, Debug)] +pub struct MacroConfig { + pub event_json: Option, +} +#[derive(Default, FromMeta, Clone, Debug)] +pub struct EventsConfig { + standard: Option, +} + /// this function is used to inject serialization macros and the `near_sdk::EventMetadata` macro. /// In addition, this function extracts the event's `standard` value and injects it as a constant to be used by /// the `near_sdk::EventMetadata` derive macro pub(crate) fn near_events(attr: TokenStream, item: TokenStream) -> TokenStream { // get standard from attr args - let standard = get_standard_arg(&syn::parse_macro_input!(attr as syn::AttributeArgs)); - if standard.is_none() { - return TokenStream::from( - syn::Error::new( - Span::call_site(), - "Near events must have a `standard` value as an argument for `event_json` in the `near_bindgen` arguments. The value must be a string literal, e.g. \"nep999\", \"mintbase-marketplace\".", - ) - .to_compile_error(), - ); - } - if let Ok(mut input) = syn::parse::(item) { - let name = &input.ident; - let standard_name = format!("{}_event_standard", name); - let standard_ident = syn::Ident::new(&standard_name, Span::call_site()); - // NearEvent Macro handles implementation - input.attrs.push( - parse_quote! (#[derive(::near_sdk::serde::Serialize, ::near_sdk::EventMetadata)]), - ); - input.attrs.push(parse_quote! (#[serde(crate="::near_sdk::serde")])); - input.attrs.push(parse_quote! (#[serde(tag = "event", content = "data")])); - input.attrs.push(parse_quote! (#[serde(rename_all = "snake_case")])); + let attr_args = match NestedMeta::parse_meta_list(attr.into()) { + Ok(v) => v, + Err(e) => { + return TokenStream::from(Error::from(e).write_errors()); + } + }; + + let args = match MacroConfig::from_list(&attr_args) { + Ok(v) => v, + Err(e) => { + return TokenStream::from(e.write_errors()); + } + }; + if let Some(standard) = args.event_json.and_then(|event_json| event_json.standard) { + if let Ok(mut input) = syn::parse::(item) { + let name = &input.ident; + let standard_name = format!("{}_event_standard", name); + let standard_ident = syn::Ident::new(&standard_name, Span::call_site()); + // NearEvent Macro handles implementation + input.attrs.push( + parse_quote! (#[derive(::near_sdk::serde::Serialize, ::near_sdk::EventMetadata)]), + ); + input.attrs.push(parse_quote! (#[serde(crate="::near_sdk::serde")])); + input.attrs.push(parse_quote! (#[serde(tag = "event", content = "data")])); + input.attrs.push(parse_quote! (#[serde(rename_all = "snake_case")])); - TokenStream::from(quote! { - const #standard_ident: &'static str = #standard; - #input - }) + TokenStream::from(quote! { + const #standard_ident: &'static str = #standard; + #input + }) + } else { + TokenStream::from( + syn::Error::new( + Span::call_site(), + "`#[near_bindgen(event_json(standard = \"nepXXX\"))]` can only be used as an attribute on enums.", + ) + .to_compile_error(), + ) + } } else { TokenStream::from( syn::Error::new( Span::call_site(), - "`#[near_bindgen(event_json(standard = \"nepXXX\"))]` can only be used as an attribute on enums.", + "Near events must have a `standard` value as an argument for `event_json` in the `near_bindgen` arguments. The value must be a string literal, e.g. \"nep999\", \"mintbase-marketplace\".", ) .to_compile_error(), ) @@ -50,34 +77,9 @@ pub(crate) fn near_events(attr: TokenStream, item: TokenStream) -> TokenStream { /// used by `near_sdk::EventMetadata` pub(crate) fn get_event_version(var: &syn::Variant) -> Option { for attr in var.attrs.iter() { - if attr.path.is_ident("event_version") { + if attr.path().is_ident("event_version") { return attr.parse_args::().ok(); } } None } - -/// this function returns the `standard` value from `#[near_bindgen(event_json(standard = "nepXXX"))]` -fn get_standard_arg(args: &[syn::NestedMeta]) -> Option { - let mut standard: Option = None; - for arg in args.iter() { - if let syn::NestedMeta::Meta(syn::Meta::List(syn::MetaList { path, nested, .. })) = arg { - if path.is_ident("event_json") { - for event_arg in nested.iter() { - if let syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue { - path, - lit: syn::Lit::Str(value), - .. - })) = event_arg - { - if path.is_ident("standard") { - standard = Some(value.to_owned()); - break; - } - } - } - } - } - } - standard -} diff --git a/near-sdk-macros/src/core_impl/info_extractor/arg_info.rs b/near-sdk-macros/src/core_impl/info_extractor/arg_info.rs index 23674434b..71a81e14b 100644 --- a/near-sdk-macros/src/core_impl/info_extractor/arg_info.rs +++ b/near-sdk-macros/src/core_impl/info_extractor/arg_info.rs @@ -1,4 +1,4 @@ -use crate::core_impl::info_extractor::{SerializerAttr, SerializerType}; +use crate::core_impl::info_extractor::SerializerType; use crate::core_impl::utils; use proc_macro2::{Span, TokenStream}; use quote::ToTokens; @@ -40,6 +40,16 @@ pub struct ArgInfo { /// The original `PatType` of the argument. pub original: PatType, } +use darling::FromAttributes; +#[derive(darling::FromAttributes, Clone, Debug)] +#[darling( + attributes(init, payable, private, result_serializer, serializer, handle_result), + forward_attrs(serializer) +)] +struct AttributeConfig { + borsh: Option, + json: Option, +} impl ArgInfo { /// Extract near-sdk specific argument info. @@ -68,8 +78,9 @@ impl ArgInfo { let mut bindgen_ty = BindgenArgType::Regular; // In the absence of serialization attributes this is a JSON serialization. let mut serializer_ty = SerializerType::JSON; + let args = AttributeConfig::from_attributes(&original.attrs)?; for attr in &original.attrs { - let attr_str = attr.path.to_token_stream().to_string(); + let attr_str = attr.path().to_token_stream().to_string(); match attr_str.as_str() { "callback" | "callback_unwrap" => { bindgen_ty = BindgenArgType::CallbackArg; @@ -81,8 +92,23 @@ impl ArgInfo { bindgen_ty = BindgenArgType::CallbackArgVec; } "serializer" => { - let serializer: SerializerAttr = syn::parse2(attr.tokens.clone())?; - serializer_ty = serializer.serializer_type; + if args.borsh.is_some() && args.json.is_some() { + return Err(Error::new( + attr.span(), + "Only one of `borsh` or `json` can be specified.", + )); + }; + + if let Some(borsh) = args.borsh { + if borsh { + serializer_ty = SerializerType::Borsh; + } + } + if let Some(json) = args.json { + if json { + serializer_ty = SerializerType::JSON; + } + } } _ => { non_bindgen_attrs.push((*attr).clone()); @@ -91,7 +117,7 @@ impl ArgInfo { } original.attrs.retain(|attr| { - let attr_str = attr.path.to_token_stream().to_string(); + let attr_str = attr.path().to_token_stream().to_string(); attr_str != "callback" && attr_str != "callback_vec" && attr_str != "serializer" diff --git a/near-sdk-macros/src/core_impl/info_extractor/attr_sig_info.rs b/near-sdk-macros/src/core_impl/info_extractor/attr_sig_info.rs index 12c259d48..9ea5762b4 100644 --- a/near-sdk-macros/src/core_impl/info_extractor/attr_sig_info.rs +++ b/near-sdk-macros/src/core_impl/info_extractor/attr_sig_info.rs @@ -24,6 +24,18 @@ pub struct AttrSigInfo { pub original_sig: Signature, } +use darling::FromAttributes; +#[derive(darling::FromAttributes, Clone, Debug)] +#[darling( + attributes(init, payable, private, result_serializer, serializer, handle_result), + forward_attrs(serializer) +)] +struct AttributeConfig { + borsh: Option, + json: Option, + ignore_state: Option, +} + impl AttrSigInfo { /// Apart from replacing `Self` types with their concretions, returns spans of all `Self` tokens found. fn sanitize_self( @@ -54,7 +66,6 @@ impl AttrSigInfo { source_type: &TokenStream2, ) -> syn::Result { let mut self_occurrences = Self::sanitize_self(original_sig, source_type)?; - let mut errors = vec![]; for generic in &original_sig.generics.params { match generic { @@ -83,12 +94,16 @@ impl AttrSigInfo { let ident = original_sig.ident.clone(); let mut non_bindgen_attrs = vec![]; + let args = AttributeConfig::from_attributes(original_attrs)?; // Visit attributes for attr in original_attrs.iter() { - let attr_str = attr.path.to_token_stream().to_string(); + let attr_str = attr.path().to_token_stream().to_string(); match attr_str.as_str() { "init" => { - let init_attr: InitAttr = syn::parse2(attr.tokens.clone())?; + let mut init_attr = InitAttr { ignore_state: false }; + if let Some(state) = args.ignore_state { + init_attr.ignore_state = state; + } visitor.visit_init_attr(attr, &init_attr)?; } "payable" => { @@ -98,7 +113,23 @@ impl AttrSigInfo { visitor.visit_private_attr(attr)?; } "result_serializer" => { - let serializer: SerializerAttr = syn::parse2(attr.tokens.clone())?; + if args.borsh.is_some() && args.json.is_some() { + return Err(Error::new( + attr.span(), + "Only one of `borsh` or `json` can be specified.", + )); + }; + let mut serializer = SerializerAttr { serializer_type: SerializerType::JSON }; + if let Some(borsh) = args.borsh { + if borsh { + serializer.serializer_type = SerializerType::Borsh; + } + } + if let Some(json) = args.json { + if json { + serializer.serializer_type = SerializerType::JSON; + } + } visitor.visit_result_serializer_attr(attr, &serializer)?; } "handle_result" => { diff --git a/near-sdk-macros/src/core_impl/info_extractor/impl_item_method_info.rs b/near-sdk-macros/src/core_impl/info_extractor/impl_item_method_info.rs index aed41846b..4c567a06e 100644 --- a/near-sdk-macros/src/core_impl/info_extractor/impl_item_method_info.rs +++ b/near-sdk-macros/src/core_impl/info_extractor/impl_item_method_info.rs @@ -1,7 +1,7 @@ use crate::core_impl::info_extractor::AttrSigInfo; use crate::core_impl::utils; use quote::ToTokens; -use syn::{ImplItemMethod, Type, Visibility}; +use syn::{ImplItemFn as ImplItemMethod, Type, Visibility}; /// Information extracted from `ImplItemMethod`. pub struct ImplItemMethodInfo { @@ -21,7 +21,8 @@ impl ImplItemMethodInfo { let ImplItemMethod { attrs, sig, .. } = original; utils::sig_is_supported(sig)?; if is_trait_impl || matches!(original.vis, Visibility::Public(_)) { - let attr_signature_info = AttrSigInfo::new(attrs, sig, &struct_type.to_token_stream())?; + let source_type = &struct_type.to_token_stream(); + let attr_signature_info = AttrSigInfo::new(attrs, sig, source_type)?; Ok(Some(Self { attr_signature_info, struct_type })) } else { Ok(None) @@ -33,7 +34,7 @@ impl ImplItemMethodInfo { #[rustfmt::skip] #[cfg(test)] mod tests { - use syn::{parse_quote, Type, ImplItemMethod, ReturnType}; + use syn::{parse_quote, Type, ImplItemFn as ImplItemMethod , ReturnType}; use crate::core_impl::{ImplItemMethodInfo}; #[test] diff --git a/near-sdk-macros/src/core_impl/info_extractor/init_attr.rs b/near-sdk-macros/src/core_impl/info_extractor/init_attr.rs index 9a59040d1..ebd34bf75 100644 --- a/near-sdk-macros/src/core_impl/info_extractor/init_attr.rs +++ b/near-sdk-macros/src/core_impl/info_extractor/init_attr.rs @@ -1,25 +1,3 @@ -use proc_macro2::Ident; -use syn::parse::{Parse, ParseStream}; -use syn::token::Paren; -use syn::Error; - pub struct InitAttr { pub ignore_state: bool, } - -impl Parse for InitAttr { - fn parse(input: ParseStream) -> syn::Result { - let ignore_state = if input.peek(Paren) { - let content; - let _paren_token = syn::parenthesized!(content in input); - let ident: Ident = content.parse()?; - match ident.to_string().as_str() { - "ignore_state" => true, - _ => return Err(Error::new(input.span(), "Unsupported init attribute.")), - } - } else { - false - }; - Ok(Self { ignore_state }) - } -} diff --git a/near-sdk-macros/src/core_impl/info_extractor/item_impl_info.rs b/near-sdk-macros/src/core_impl/info_extractor/item_impl_info.rs index 785fdf062..3e62e3514 100644 --- a/near-sdk-macros/src/core_impl/info_extractor/item_impl_info.rs +++ b/near-sdk-macros/src/core_impl/info_extractor/item_impl_info.rs @@ -23,7 +23,7 @@ impl ItemImplInfo { let mut methods = vec![]; for subitem in &mut original.items { - if let ImplItem::Method(m) = subitem { + if let ImplItem::Fn(m) = subitem { if let Some(method_info) = ImplItemMethodInfo::new(m, is_trait_impl, ty.clone())? { methods.push(method_info); } diff --git a/near-sdk-macros/src/core_impl/info_extractor/item_trait_info.rs b/near-sdk-macros/src/core_impl/info_extractor/item_trait_info.rs index 32a2dc0f2..ef4971e54 100644 --- a/near-sdk-macros/src/core_impl/info_extractor/item_trait_info.rs +++ b/near-sdk-macros/src/core_impl/info_extractor/item_trait_info.rs @@ -30,9 +30,10 @@ impl ItemTraitInfo { "Traits for external contracts do not support associated trait types yet.", )) } - TraitItem::Method(method) => { - methods - .push(TraitItemMethodInfo::new(method, &original.ident.to_token_stream())?); + TraitItem::Fn(method) => { + let item = TraitItemMethodInfo::new(method, &original.ident.to_token_stream())?; + methods.push(item); + if method.default.is_some() { return Err(Error::new( method.span(), diff --git a/near-sdk-macros/src/core_impl/info_extractor/serializer_attr.rs b/near-sdk-macros/src/core_impl/info_extractor/serializer_attr.rs index f9084ea1e..936120811 100644 --- a/near-sdk-macros/src/core_impl/info_extractor/serializer_attr.rs +++ b/near-sdk-macros/src/core_impl/info_extractor/serializer_attr.rs @@ -1,24 +1,5 @@ use super::SerializerType; -use proc_macro2::Ident; -use syn::parse::{Parse, ParseStream}; -use syn::{parenthesized, Error}; pub struct SerializerAttr { - #[allow(dead_code)] - paren_token: syn::token::Paren, pub serializer_type: SerializerType, } - -impl Parse for SerializerAttr { - fn parse(input: ParseStream) -> syn::Result { - let content; - let paren_token = parenthesized!(content in input); - let ident: Ident = content.parse()?; - let serializer_type = match ident.to_string().as_str() { - "borsh" => SerializerType::Borsh, - "json" => SerializerType::JSON, - _ => return Err(Error::new(input.span(), "Unsupported serializer type.")), - }; - Ok(Self { paren_token, serializer_type }) - } -} diff --git a/near-sdk-macros/src/core_impl/info_extractor/trait_item_method_info.rs b/near-sdk-macros/src/core_impl/info_extractor/trait_item_method_info.rs index bf26494b3..f5bcdf57c 100644 --- a/near-sdk-macros/src/core_impl/info_extractor/trait_item_method_info.rs +++ b/near-sdk-macros/src/core_impl/info_extractor/trait_item_method_info.rs @@ -2,20 +2,20 @@ use super::AttrSigInfo; use crate::core_impl::utils; use proc_macro2::TokenStream as TokenStream2; use syn::spanned::Spanned; -use syn::{Error, LitStr, TraitItemMethod}; +use syn::{Error, LitStr, TraitItemFn}; /// Information extracted from trait method. pub struct TraitItemMethodInfo { /// Attributes and signature information. pub attr_sig_info: AttrSigInfo, /// The original AST of the trait item method. - pub original: TraitItemMethod, + pub original: TraitItemFn, /// String representation of method name, e.g. `"my_method"`. pub ident_byte_str: LitStr, } impl TraitItemMethodInfo { - pub fn new(original: &mut TraitItemMethod, trait_name: &TokenStream2) -> syn::Result { + pub fn new(original: &mut TraitItemFn, trait_name: &TokenStream2) -> syn::Result { if original.default.is_some() { return Err(Error::new( original.span(), @@ -25,7 +25,7 @@ impl TraitItemMethodInfo { )); } - let TraitItemMethod { attrs, sig, .. } = original; + let TraitItemFn { attrs, sig, .. } = original; utils::sig_is_supported(sig)?; let attr_sig_info = AttrSigInfo::new(attrs, sig, trait_name)?; diff --git a/near-sdk-macros/src/core_impl/info_extractor/visitor.rs b/near-sdk-macros/src/core_impl/info_extractor/visitor.rs index c32f4fbf3..1252512f2 100644 --- a/near-sdk-macros/src/core_impl/info_extractor/visitor.rs +++ b/near-sdk-macros/src/core_impl/info_extractor/visitor.rs @@ -177,7 +177,7 @@ impl Visitor { } fn is_init(attributes: &[Attribute]) -> bool { - attributes.iter().any(|a| a.path.to_token_stream().to_string() == "init") + attributes.iter().any(|a| a.path().to_token_stream().to_string() == "init") } fn is_view(sig: &Signature) -> bool { diff --git a/near-sdk-macros/src/core_impl/metadata/metadata_visitor.rs b/near-sdk-macros/src/core_impl/metadata/metadata_visitor.rs index 0721ab7af..424490f78 100644 --- a/near-sdk-macros/src/core_impl/metadata/metadata_visitor.rs +++ b/near-sdk-macros/src/core_impl/metadata/metadata_visitor.rs @@ -22,7 +22,7 @@ impl<'ast> Visit<'ast> for MetadataVisitor { let has_near_sdk_attr = i .attrs .iter() - .any(|attr| attr.path.to_token_stream().to_string().as_str() == "near_bindgen"); + .any(|attr| attr.path().to_token_stream().to_string().as_str() == "near_bindgen"); if has_near_sdk_attr { match ItemImplInfo::new(&mut i.clone()) { Ok(info) => self.impl_item_infos.push(info), diff --git a/near-sdk-macros/src/lib.rs b/near-sdk-macros/src/lib.rs index 37081425b..7f0e04163 100644 --- a/near-sdk-macros/src/lib.rs +++ b/near-sdk-macros/src/lib.rs @@ -275,68 +275,51 @@ pub fn metadata(item: TokenStream) -> TokenStream { ) } } +#[cfg(feature = "abi")] +use darling::FromDeriveInput; +#[derive(darling::FromDeriveInput, Debug)] +#[darling(attributes(abi), forward_attrs(serde, borsh_skip, schemars, validate))] +#[cfg(feature = "abi")] +struct DeriveNearSchema { + attrs: Vec, + json: Option, + borsh: Option, +} #[cfg(feature = "abi")] #[proc_macro_derive(NearSchema, attributes(abi, serde, borsh_skip, schemars, validate))] pub fn derive_near_schema(input: TokenStream) -> TokenStream { - let mut input = syn::parse_macro_input!(input as syn::DeriveInput); - - let (mut json_schema, mut borsh_schema) = (false, false); - let mut type_attrs = vec![]; - let mut errors = vec![]; - for attr in input.attrs { - match attr.parse_meta() { - // #[abi] - Ok(syn::Meta::Path(meta)) if meta.is_ident("abi") => { - errors.push(syn::Error::new_spanned( - meta.into_token_stream(), - "attribute requires at least one argument", - )); - } - // #[abi(json, borsh)] - Ok(syn::Meta::List(meta)) if meta.path.is_ident("abi") => { - // #[abi()] - if meta.nested.is_empty() { - errors.push(syn::Error::new_spanned( - meta.into_token_stream(), - "attribute requires at least one argument", - )); - continue; - } - for meta in meta.nested { - match meta { - // #[abi(.. json ..)] - syn::NestedMeta::Meta(m) if m.path().is_ident("json") => json_schema = true, - // #[abi(.. borsh ..)] - syn::NestedMeta::Meta(m) if m.path().is_ident("borsh") => { - borsh_schema = true - } - _ => { - errors.push(syn::Error::new_spanned( - meta.into_token_stream(), - "invalid argument, expected: `json` or `borsh`", - )); - } - } - } - } - // #[serde(..)], #[schemars(..)] - Ok(syn::Meta::List(meta)) - if meta.path.is_ident("serde") || meta.path.is_ident("schemars") => - { - type_attrs.push(attr) - } - _ => continue, + let derive_input = syn::parse_macro_input!(input as syn::DeriveInput); + let args = match DeriveNearSchema::from_derive_input(&derive_input) { + Ok(v) => v, + Err(e) => { + return TokenStream::from(e.write_errors()); } + }; + + if args.borsh.is_none() + && args.json.is_none() + && derive_input.attrs.iter().any(|attr| attr.path().is_ident("abi")) + { + return TokenStream::from( + syn::Error::new_spanned( + derive_input.to_token_stream(), + "At least one of `json` or `borsh` inside of `#[abi(...)]` must be specified", + ) + .to_compile_error(), + ); } - input.attrs = type_attrs; + // #[abi(json, borsh)] + let (json_schema, borsh_schema) = (args.json.unwrap_or(false), args.borsh.unwrap_or(false)); + let mut input = derive_input; + input.attrs = args.attrs; let strip_unknown_attr = |attrs: &mut Vec| { attrs.retain(|attr| { ["serde", "schemars", "validate", "borsh_skip"] .iter() - .any(|&path| attr.path.is_ident(path)) + .any(|&path| attr.path().is_ident(path)) }); }; @@ -365,10 +348,6 @@ pub fn derive_near_schema(input: TokenStream) -> TokenStream { } } - if let Some(combined_errors) = errors.into_iter().reduce(|mut l, r| (l.combine(r), l).1) { - return TokenStream::from(combined_errors.to_compile_error()); - } - let json_schema = json_schema || !borsh_schema; let derive = match (json_schema, borsh_schema) { @@ -577,9 +556,10 @@ pub fn derive_event_attributes(item: TokenStream) -> TokenStream { // add `'near_event` lifetime for user defined events let mut generics = input.generics.clone(); let event_lifetime = syn::Lifetime::new("'near_event", Span::call_site()); - generics - .params - .insert(0, syn::GenericParam::Lifetime(syn::LifetimeDef::new(event_lifetime.clone()))); + generics.params.insert( + 0, + syn::GenericParam::Lifetime(syn::LifetimeParam::new(event_lifetime.clone())), + ); let (custom_impl_generics, ..) = generics.split_for_impl(); TokenStream::from(quote! { diff --git a/near-sdk/compilation_tests/schema_derive_invalids.rs b/near-sdk/compilation_tests/schema_derive_invalids.rs index 1c7e24f81..5d99b3225 100644 --- a/near-sdk/compilation_tests/schema_derive_invalids.rs +++ b/near-sdk/compilation_tests/schema_derive_invalids.rs @@ -43,7 +43,7 @@ union UnsupporteedWithoutArgs { } #[derive(NearSchema)] -#[abi(json, schemars)] +#[abi(json)] union UnsupporteedWithArgs { a: u8, b: u16, diff --git a/near-sdk/compilation_tests/schema_derive_invalids.stderr b/near-sdk/compilation_tests/schema_derive_invalids.stderr index b179da531..1064a910a 100644 --- a/near-sdk/compilation_tests/schema_derive_invalids.stderr +++ b/near-sdk/compilation_tests/schema_derive_invalids.stderr @@ -1,44 +1,46 @@ -error: attribute requires at least one argument - --> compilation_tests/schema_derive_invalids.rs:9:3 - | -9 | #[abi] - | ^^^ +error: At least one of `json` or `borsh` inside of `#[abi(...)]` must be specified + --> compilation_tests/schema_derive_invalids.rs:9:1 + | +9 | / #[abi] +10 | | struct Nada; + | |____________^ -error: attribute requires at least one argument - --> compilation_tests/schema_derive_invalids.rs:13:3 +error: At least one of `json` or `borsh` inside of `#[abi(...)]` must be specified + --> compilation_tests/schema_derive_invalids.rs:13:1 | -13 | #[abi()] - | ^^^^^ +13 | / #[abi()] +14 | | struct Empty; + | |_____________^ -error: invalid argument, expected: `json` or `borsh` +error: Unknown field: `serde` --> compilation_tests/schema_derive_invalids.rs:17:7 | 17 | #[abi(serde)] | ^^^^^ -error: invalid argument, expected: `json` or `borsh` +error: Unknown field: `serde` --> compilation_tests/schema_derive_invalids.rs:21:13 | 21 | #[abi(json, serde)] | ^^^^^ -error: invalid argument, expected: `json` or `borsh` +error: Unknown field: `serde` --> compilation_tests/schema_derive_invalids.rs:25:13 | 25 | #[abi(json, serde, schemars)] | ^^^^^ -error: invalid argument, expected: `json` or `borsh` +error: Unknown field: `schemars` --> compilation_tests/schema_derive_invalids.rs:25:20 | 25 | #[abi(json, serde, schemars)] | ^^^^^^^^ -error: invalid argument, expected: `json` or `borsh` +error: Unknown field: `serde` --> compilation_tests/schema_derive_invalids.rs:29:13 | 29 | #[abi(json, serde = "?")] - | ^^^^^^^^^^^ + | ^^^^^ error: `NearSchema` does not support derive for unions --> compilation_tests/schema_derive_invalids.rs:33:1 @@ -49,10 +51,11 @@ error: `NearSchema` does not support derive for unions 36 | | } | |_^ -error: `NearSchema` does not support derive for unions - --> compilation_tests/schema_derive_invalids.rs:40:1 +error: At least one of `json` or `borsh` inside of `#[abi(...)]` must be specified + --> compilation_tests/schema_derive_invalids.rs:39:1 | -40 | / union UnsupporteedWithoutArgs { +39 | / #[abi()] +40 | | union UnsupporteedWithoutArgs { 41 | | a: u8, 42 | | b: u16, 43 | | } diff --git a/near-sdk/tests/event_tests.rs b/near-sdk/tests/event_tests.rs index a33d6bfc2..5f1983e7c 100644 --- a/near-sdk/tests/event_tests.rs +++ b/near-sdk/tests/event_tests.rs @@ -1,7 +1,7 @@ use near_sdk::test_utils::get_logs; use near_sdk::{near_bindgen, AccountId}; -#[near_bindgen(event_json(standard = "test_standard", random = "random"), other_random)] +#[near_bindgen(event_json(standard = "test_standard"))] pub enum TestEvents<'a, 'b, T> where T: near_sdk::serde::Serialize,