Skip to content

Commit

Permalink
feat-issue-1177: use function to replace const str
Browse files Browse the repository at this point in the history
  • Loading branch information
fospring committed May 30, 2024
1 parent e1086f0 commit 8951d06
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 42 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ members = [
"near-sdk-macros",
"near-contract-standards",
"near-sys",
"near-attribute-str",
]
exclude = ["examples/"]

Expand Down
8 changes: 0 additions & 8 deletions near-attribute-str/Cargo.toml

This file was deleted.

1 change: 0 additions & 1 deletion near-sdk-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
24 changes: 9 additions & 15 deletions near-sdk-macros/src/core_impl/info_extractor/attr_sig_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand All @@ -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);
}
}
_ => {
Expand Down
1 change: 0 additions & 1 deletion near-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
3 changes: 2 additions & 1 deletion near-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
46 changes: 31 additions & 15 deletions near-attribute-str/src/lib.rs → near-sdk/src/near.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
///
Expand All @@ -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.
///
Expand All @@ -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.
///
Expand All @@ -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<T, E> (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<Promise, &'static str> {
/// 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() {}

0 comments on commit 8951d06

Please sign in to comment.