diff --git a/Cargo.toml b/Cargo.toml index 0ac56340c..af1f3b28b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ 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 new file mode 100644 index 000000000..37e8f11fb --- /dev/null +++ b/near-attribute-str/Cargo.toml @@ -0,0 +1,8 @@ +[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-attribute-str/src/lib.rs b/near-attribute-str/src/lib.rs new file mode 100644 index 000000000..811a2ba2d --- /dev/null +++ b/near-attribute-str/src/lib.rs @@ -0,0 +1,76 @@ +/// Initialization Methods. +/// +/// 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 +/// +/// ## Basic example +/// +/// ```rust +///#[near] +///impl Counter { +/// #[init] +/// pub fn new(value: u64) -> Self { +/// log!("Custom counter initialization!"); +/// Self { value } +/// } +///} +/// ``` +#[allow(non_upper_case_globals)] +pub const init: &str = "init"; + +/// Payable Methods +/// +/// Methods can be annotated with #[payable] to allow tokens to be transferred with the method invocation. For more information, see payable methods. +/// +/// To declare a function as payable, use the #[payable] annotation as follows: +/// # Examples +/// +/// ## Basic example +/// +/// ```rust +///#[payable] +///pub fn my_method(&mut self) { +/// ... +///} +/// ``` +#[allow(non_upper_case_globals)] +pub const payable: &str = "payable"; + +/// Private Methods +/// +/// 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. +/// +/// This annotation can be applied to any method through the following: +/// # Examples +/// +/// ## Basic example +/// +/// ```rust +/// #[private] +/// pub fn my_method(&mut self) { +/// ... +///} +/// ``` +#[allow(non_upper_case_globals)] +pub const private: &str = "private"; + +/// Result serialization. +/// +/// Only one of `borsh` or `json` can be specified. +/// +/// # Examples +/// +/// ## Basic example +/// +/// ```rust +///#[result_serializer(borsh)] +///pub fn add_borsh(&self, #[serializer(borsh)] a: Pair, #[serializer(borsh)] b: Pair) -> Pair { +/// sum_pair(&a, &b) +///} +/// ``` +#[allow(non_upper_case_globals)] +pub const result_serializer: &str = "result_serializer"; + +/// Handle result +#[allow(non_upper_case_globals)] +pub const handle_result: &str = "handle_result"; diff --git a/near-sdk-macros/Cargo.toml b/near-sdk-macros/Cargo.toml index f09bcec78..d097d7862 100644 --- a/near-sdk-macros/Cargo.toml +++ b/near-sdk-macros/Cargo.toml @@ -24,6 +24,7 @@ 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 772565295..15292dc75 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,6 +3,7 @@ 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; @@ -102,20 +103,24 @@ impl AttrSigInfo { for attr in original_attrs.iter() { let attr_str = attr.path().to_token_stream().to_string(); match attr_str.as_str() { - "init" => { + #[allow(non_upper_case_globals)] + 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)?; } - "payable" => { + #[allow(non_upper_case_globals)] + payable => { visitor.visit_payable_attr(attr)?; } - "private" => { + #[allow(non_upper_case_globals)] + private => { visitor.visit_private_attr(attr)?; } - "result_serializer" => { + #[allow(non_upper_case_globals)] + result_serializer => { if args.borsh.is_some() && args.json.is_some() { return Err(Error::new( attr.span(), @@ -135,13 +140,14 @@ impl AttrSigInfo { } visitor.visit_result_serializer_attr(attr, &serializer)?; } - "handle_result" => { + #[allow(non_upper_case_globals)] + handle_result => { if let Some(value) = args.aliased { - let handle_result = HandleResultAttr { check: value }; - visitor.visit_handle_result_attr(&handle_result); + let handle_result_attr = HandleResultAttr { check: value }; + visitor.visit_handle_result_attr(&handle_result_attr); } else { - let handle_result = HandleResultAttr { check: false }; - visitor.visit_handle_result_attr(&handle_result); + let handle_result_attr = HandleResultAttr { check: false }; + visitor.visit_handle_result_attr(&handle_result_attr); } } _ => { diff --git a/near-sdk/Cargo.toml b/near-sdk/Cargo.toml index 7488fe7bd..1f854f87e 100644 --- a/near-sdk/Cargo.toml +++ b/near-sdk/Cargo.toml @@ -33,6 +33,7 @@ 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 45df0dbcd..3b81939b6 100644 --- a/near-sdk/src/lib.rs +++ b/near-sdk/src/lib.rs @@ -63,6 +63,7 @@ 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;