Skip to content

Commit

Permalink
Add inside_nearsdk for near macro, use near inside near_sdk instead o…
Browse files Browse the repository at this point in the history
…f boilerplate
  • Loading branch information
PolyProgrammist committed Mar 15, 2024
1 parent 198d731 commit 43e3f49
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 35 deletions.
35 changes: 26 additions & 9 deletions near-sdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct NearMacroArgs {
serializers: Option<IdentsVector>,
contract_state: Option<bool>,
contract_metadata: Option<core_impl::ContractMetadata>,
inside_nearsdk: Option<bool>,
}

/// This attribute macro is used to reduce enhance near_bindgen macro.
Expand Down Expand Up @@ -139,28 +140,42 @@ pub fn near(attr: TokenStream, item: TokenStream) -> TokenStream {
}
}

let near_sdk_crate = if near_macro_args.inside_nearsdk.unwrap_or(false) {
quote! {crate}
} else {
quote! {::near_sdk}
};
let string_borsh_crate = quote! {#near_sdk_crate::borsh}.to_string();
let string_serde_crate = quote! {#near_sdk_crate::serde}.to_string();

let inside_nearsdk_attr = if near_macro_args.inside_nearsdk.unwrap_or(false) {
quote! {#[inside_nearsdk]}
} else {
quote! {}
};

let borsh = if has_borsh {
quote! {
#[derive(near_sdk::borsh::BorshSerialize, near_sdk::borsh::BorshDeserialize)]
#[borsh(crate = "near_sdk::borsh")]
#[derive(#near_sdk_crate::borsh::BorshSerialize, #near_sdk_crate::borsh::BorshDeserialize)]
#[borsh(crate = #string_borsh_crate)]
}
} else {
quote! {}
};
let json = if has_json {
quote! {
#[derive(near_sdk::serde::Serialize, near_sdk::serde::Deserialize)]
#[serde(crate = "near_sdk::serde")]
#[derive(#near_sdk_crate::serde::Serialize, #near_sdk_crate::serde::Deserialize)]
#[serde(crate = #string_serde_crate)]
}
} else {
quote! {}
};

let near_bindgen_annotation = if near_macro_args.contract_state.unwrap_or(false) {
if let Some(metadata) = near_macro_args.contract_metadata {
quote! {#[near_sdk::near_bindgen(#metadata)]}
quote! {#[#near_sdk_crate::near_bindgen(#metadata)]}
} else {
quote! {#[near_sdk::near_bindgen]}
quote! {#[#near_sdk_crate::near_bindgen]}
}
} else {
quote! {}
Expand All @@ -179,7 +194,8 @@ pub fn near(attr: TokenStream, item: TokenStream) -> TokenStream {
if let Ok(input) = syn::parse::<ItemStruct>(item.clone()) {
expanded = quote! {
#near_bindgen_annotation
#[derive(near_sdk::NearSchema)]
#[derive(#near_sdk_crate::NearSchema)]
#inside_nearsdk_attr
#borsh
#json
#abis
Expand All @@ -188,15 +204,16 @@ pub fn near(attr: TokenStream, item: TokenStream) -> TokenStream {
} else if let Ok(input) = syn::parse::<ItemEnum>(item.clone()) {
expanded = quote! {
#near_bindgen_annotation
#[derive(near_sdk::NearSchema)]
#[derive(#near_sdk_crate::NearSchema)]
#inside_nearsdk_attr
#borsh
#json
#abis
#input
};
} else if let Ok(input) = syn::parse::<ItemImpl>(item) {
expanded = quote! {
#[near_sdk::near_bindgen]
#[#near_sdk_crate::near_bindgen]
#input
};
} else {
Expand Down
6 changes: 2 additions & 4 deletions near-sdk/src/collections/lazy_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ use borsh::{to_vec, BorshDeserialize, BorshSerialize};

use crate::env;
use crate::IntoStorageKey;
use near_sdk_macros::NearSchema;
use near_sdk_macros::near;

const ERR_VALUE_SERIALIZATION: &str = "Cannot serialize value with Borsh";
const ERR_VALUE_DESERIALIZATION: &str = "Cannot deserialize value with Borsh";

/// An persistent lazy option, that stores a value in the storage.
#[derive(BorshSerialize, BorshDeserialize, NearSchema)]
#[inside_nearsdk]
#[abi(borsh)]
#[near(inside_nearsdk)]
pub struct LazyOption<T> {
storage_key: Vec<u8>,
#[borsh(skip)]
Expand Down
6 changes: 2 additions & 4 deletions near-sdk/src/collections/lookup_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ use borsh::{to_vec, BorshDeserialize, BorshSerialize};

use crate::collections::append_slice;
use crate::{env, IntoStorageKey};
use near_sdk_macros::NearSchema;
use near_sdk_macros::near;

const ERR_KEY_SERIALIZATION: &str = "Cannot serialize key with Borsh";
const ERR_VALUE_DESERIALIZATION: &str = "Cannot deserialize value with Borsh";
const ERR_VALUE_SERIALIZATION: &str = "Cannot serialize value with Borsh";

/// An non-iterable implementation of a map that stores its content directly on the trie.
#[derive(BorshSerialize, BorshDeserialize, NearSchema)]
#[inside_nearsdk]
#[abi(borsh)]
#[near(inside_nearsdk)]
pub struct LookupMap<K, V> {
key_prefix: Vec<u8>,
#[borsh(skip)]
Expand Down
6 changes: 2 additions & 4 deletions near-sdk/src/collections/tree_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ops::Bound;
use crate::collections::LookupMap;
use crate::collections::{append, Vector};
use crate::{env, IntoStorageKey};
use near_sdk_macros::NearSchema;
use near_sdk_macros::near;

/// TreeMap based on AVL-tree
///
Expand All @@ -16,9 +16,7 @@ use near_sdk_macros::NearSchema;
/// - `range` of K elements: O(Klog(N))
///
#[derive(BorshSerialize, BorshDeserialize, NearSchema)]
#[inside_nearsdk]
#[abi(borsh)]
#[near(inside_nearsdk)]
pub struct TreeMap<K, V> {
root: u64,
val: LookupMap<K, V>,
Expand Down
6 changes: 2 additions & 4 deletions near-sdk/src/collections/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::iter::FusedIterator;
use std::marker::PhantomData;

use borsh::{to_vec, BorshDeserialize, BorshSerialize};
use near_sdk_macros::NearSchema;
use near_sdk_macros::near;

use crate::collections::append_slice;
use crate::{env, IntoStorageKey};
Expand All @@ -21,9 +21,7 @@ fn expect_consistent_state<T>(val: Option<T>) -> T {

/// An iterable implementation of vector that stores its content on the trie.
/// Uses the following map: index -> element.
#[derive(BorshSerialize, BorshDeserialize, NearSchema)]
#[inside_nearsdk]
#[abi(borsh)]
#[near(inside_nearsdk)]
pub struct Vector<T> {
len: u64,
prefix: Vec<u8>,
Expand Down
13 changes: 3 additions & 10 deletions near-sdk/src/json_types/vector.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use borsh::{BorshDeserialize, BorshSerialize};
use near_sdk_macros::NearSchema;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use near_sdk_macros::near;
use serde::{Deserialize, Deserializer, Serializer};

/// Helper class to serialize/deserialize `Vec<u8>` to base64 string.
Expand All @@ -9,14 +8,8 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
Clone,
PartialEq,
Eq,
Serialize,
Deserialize,
BorshDeserialize,
BorshSerialize,
NearSchema,
)]
#[inside_nearsdk]
#[abi(borsh, json)]
#[near(inside_nearsdk, serializers=[borsh, json])]
pub struct Base64VecU8(
#[serde(
serialize_with = "base64_bytes::serialize",
Expand Down

0 comments on commit 43e3f49

Please sign in to comment.