From 8951d0655d035b2fea3c19079661387a08877fc0 Mon Sep 17 00:00:00 2001 From: Spring Chiu Date: Fri, 31 May 2024 00:50:54 +0800 Subject: [PATCH] feat-issue-1177: use function to replace const str --- Cargo.toml | 1 - near-attribute-str/Cargo.toml | 8 ---- near-sdk-macros/Cargo.toml | 1 - .../core_impl/info_extractor/attr_sig_info.rs | 24 ++++------ near-sdk/Cargo.toml | 1 - near-sdk/src/lib.rs | 3 +- .../src/lib.rs => near-sdk/src/near.rs | 46 +++++++++++++------ 7 files changed, 42 insertions(+), 42 deletions(-) delete mode 100644 near-attribute-str/Cargo.toml rename near-attribute-str/src/lib.rs => near-sdk/src/near.rs (58%) diff --git a/Cargo.toml b/Cargo.toml index af1f3b28b..0ac56340c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,6 @@ members = [ "near-sdk-macros", "near-contract-standards", "near-sys", - "near-attribute-str", ] exclude = ["examples/"] diff --git a/near-attribute-str/Cargo.toml b/near-attribute-str/Cargo.toml deleted file mode 100644 index 37e8f11fb..000000000 --- a/near-attribute-str/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "near-attribute-str" -edition = "2021" -version.workspace = true - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] diff --git a/near-sdk-macros/Cargo.toml b/near-sdk-macros/Cargo.toml index d097d7862..f09bcec78 100644 --- a/near-sdk-macros/Cargo.toml +++ b/near-sdk-macros/Cargo.toml @@ -24,7 +24,6 @@ Inflector = { version = "0.11.4", default-features = false, features = [] } darling = { version = "0.20.3", default-features = false } serde = { version = "1", default-features = false, features = ["serde_derive"] } serde_json = "1" -near-attribute-str = { path = "../near-attribute-str" } [dev-dependencies] insta = { version = "1.31.0", features = ["yaml"] } 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 15292dc75..772565295 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 @@ -3,7 +3,6 @@ use super::{ ArgInfo, BindgenArgType, HandleResultAttr, InitAttr, MethodKind, SerializerAttr, SerializerType, }; use crate::core_impl::{utils, Returns}; -use near_attribute_str::{handle_result, init, payable, private, result_serializer}; use proc_macro2::{Span, TokenStream as TokenStream2}; use quote::ToTokens; use syn::spanned::Spanned; @@ -103,24 +102,20 @@ impl AttrSigInfo { for attr in original_attrs.iter() { let attr_str = attr.path().to_token_stream().to_string(); match attr_str.as_str() { - #[allow(non_upper_case_globals)] - init => { + "init" => { 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)?; } - #[allow(non_upper_case_globals)] - payable => { + "payable" => { visitor.visit_payable_attr(attr)?; } - #[allow(non_upper_case_globals)] - private => { + "private" => { visitor.visit_private_attr(attr)?; } - #[allow(non_upper_case_globals)] - result_serializer => { + "result_serializer" => { if args.borsh.is_some() && args.json.is_some() { return Err(Error::new( attr.span(), @@ -140,14 +135,13 @@ impl AttrSigInfo { } visitor.visit_result_serializer_attr(attr, &serializer)?; } - #[allow(non_upper_case_globals)] - handle_result => { + "handle_result" => { if let Some(value) = args.aliased { - let handle_result_attr = HandleResultAttr { check: value }; - visitor.visit_handle_result_attr(&handle_result_attr); + let handle_result = HandleResultAttr { check: value }; + visitor.visit_handle_result_attr(&handle_result); } else { - let handle_result_attr = HandleResultAttr { check: false }; - visitor.visit_handle_result_attr(&handle_result_attr); + let handle_result = HandleResultAttr { check: false }; + visitor.visit_handle_result_attr(&handle_result); } } _ => { diff --git a/near-sdk/Cargo.toml b/near-sdk/Cargo.toml index 1f854f87e..7488fe7bd 100644 --- a/near-sdk/Cargo.toml +++ b/near-sdk/Cargo.toml @@ -33,7 +33,6 @@ once_cell = { version = "1.17", default-features = false } near-account-id = { version="1.0.0", features = ["serde", "borsh"] } near-gas = { version = "0.2.3", features = ["serde", "borsh"] } near-token = { version = "0.2.0", features = ["serde", "borsh"] } -near-attribute-str = { path = "../near-attribute-str" } [target.'cfg(target_arch = "wasm32")'.dependencies] wee_alloc = { version = "0.4.5", default-features = false, optional = true } diff --git a/near-sdk/src/lib.rs b/near-sdk/src/lib.rs index 3b81939b6..52db4cc8e 100644 --- a/near-sdk/src/lib.rs +++ b/near-sdk/src/lib.rs @@ -50,6 +50,8 @@ pub mod utils; pub use crate::utils::storage_key_impl::IntoStorageKey; pub use crate::utils::*; +pub mod near; + #[cfg(all(feature = "unit-testing", not(target_arch = "wasm32")))] pub mod test_utils; @@ -63,7 +65,6 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; pub use base64; pub use borsh; pub use bs58; -pub use near_attribute_str::*; #[cfg(feature = "abi")] pub use schemars; pub use serde; diff --git a/near-attribute-str/src/lib.rs b/near-sdk/src/near.rs similarity index 58% rename from near-attribute-str/src/lib.rs rename to near-sdk/src/near.rs index 811a2ba2d..e2c98b57d 100644 --- a/near-attribute-str/src/lib.rs +++ b/near-sdk/src/near.rs @@ -1,4 +1,4 @@ -/// Initialization Methods. +/// Initialization Methods inner #[near] annotation. /// /// By default, the Default::default() implementation of a contract will be used to initialize a contract. There can be a custom initialization function which takes parameters or performs custom logic with the following #[init] annotation: /// # Examples @@ -15,10 +15,9 @@ /// } ///} /// ``` -#[allow(non_upper_case_globals)] -pub const init: &str = "init"; +pub fn init() {} -/// Payable Methods +/// Payable Methods inner #[near] annotation. /// /// Methods can be annotated with #[payable] to allow tokens to be transferred with the method invocation. For more information, see payable methods. /// @@ -33,10 +32,9 @@ pub const init: &str = "init"; /// ... ///} /// ``` -#[allow(non_upper_case_globals)] -pub const payable: &str = "payable"; +pub fn payable() {} -/// Private Methods +/// Private Methods inner #[near] annotation. /// /// Some methods need to be exposed to allow the contract to call a method on itself through a promise, but want to disallow any other contract to call it. For this, use the #[private] annotation to panic when this method is called externally. See [private methods](https://docs.near.org/sdk/rust/contract-interface/private-methods) for more information. /// @@ -51,10 +49,9 @@ pub const payable: &str = "payable"; /// ... ///} /// ``` -#[allow(non_upper_case_globals)] -pub const private: &str = "private"; +pub fn private() {} -/// Result serialization. +/// Result serialization inner #[near] annotation.. /// /// Only one of `borsh` or `json` can be specified. /// @@ -68,9 +65,28 @@ pub const private: &str = "private"; /// sum_pair(&a, &b) ///} /// ``` -#[allow(non_upper_case_globals)] -pub const result_serializer: &str = "result_serializer"; +pub fn result_serializer() {} -/// Handle result -#[allow(non_upper_case_globals)] -pub const handle_result: &str = "handle_result"; +/// Support Result types regardless of how they're referred to inner #[near] annotation. +/// +/// Have #[handle_result] to Support Result types regardless of how they're referred to +/// Function marked with #[handle_result] should return Result (where E implements FunctionError). If you're trying to use a type alias for `Result`, try `#[handle_result(aliased)] +/// +/// # Examples +/// +/// ## Basic example +/// +/// ```rust +/// #[handle_result] +/// pub fn get_result( +/// &self, +/// account_id: AccountId, +/// #[callback_result] set_status_result: Result<(), PromiseError>, +/// ) -> Result { +/// match set_status_result { +/// Ok(_) => Ok(ext_status_message::ext(account_id).get_status(env::signer_account_id())), +/// Err(_) => Err("Failed to set status"), +/// } +/// } +/// ``` +pub fn handle_result() {}