Skip to content

Commit

Permalink
feat-issue-1177: add attribute string for indexing attribute string i…
Browse files Browse the repository at this point in the history
…n near-sdk
  • Loading branch information
fospring committed May 29, 2024
1 parent 5a9acae commit e1086f0
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"near-sdk-macros",
"near-contract-standards",
"near-sys",
"near-attribute-str",
]
exclude = ["examples/"]

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

0 comments on commit e1086f0

Please sign in to comment.