diff --git a/near-sdk-macros/src/lib.rs b/near-sdk-macros/src/lib.rs index 96907d93e..cdb185744 100644 --- a/near-sdk-macros/src/lib.rs +++ b/near-sdk-macros/src/lib.rs @@ -40,6 +40,7 @@ struct NearMacroArgs { serializers: Option, contract_state: Option, contract_metadata: Option, + inside_nearsdk: Option, } /// This attribute macro is used to reduce enhance near_bindgen macro. @@ -139,18 +140,32 @@ 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! {} @@ -158,9 +173,9 @@ pub fn near(attr: TokenStream, item: TokenStream) -> TokenStream { 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! {} @@ -179,7 +194,8 @@ pub fn near(attr: TokenStream, item: TokenStream) -> TokenStream { if let Ok(input) = syn::parse::(item.clone()) { expanded = quote! { #near_bindgen_annotation - #[derive(near_sdk::NearSchema)] + #[derive(#near_sdk_crate::NearSchema)] + #inside_nearsdk_attr #borsh #json #abis @@ -188,7 +204,8 @@ pub fn near(attr: TokenStream, item: TokenStream) -> TokenStream { } else if let Ok(input) = syn::parse::(item.clone()) { expanded = quote! { #near_bindgen_annotation - #[derive(near_sdk::NearSchema)] + #[derive(#near_sdk_crate::NearSchema)] + #inside_nearsdk_attr #borsh #json #abis @@ -196,7 +213,7 @@ pub fn near(attr: TokenStream, item: TokenStream) -> TokenStream { }; } else if let Ok(input) = syn::parse::(item) { expanded = quote! { - #[near_sdk::near_bindgen] + #[#near_sdk_crate::near_bindgen] #input }; } else { diff --git a/near-sdk/src/collections/lazy_option.rs b/near-sdk/src/collections/lazy_option.rs index 781c7ba79..ad88b3835 100644 --- a/near-sdk/src/collections/lazy_option.rs +++ b/near-sdk/src/collections/lazy_option.rs @@ -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 { storage_key: Vec, #[borsh(skip)] diff --git a/near-sdk/src/collections/lookup_map.rs b/near-sdk/src/collections/lookup_map.rs index 57dde6488..5e9cef796 100644 --- a/near-sdk/src/collections/lookup_map.rs +++ b/near-sdk/src/collections/lookup_map.rs @@ -7,7 +7,7 @@ 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"; @@ -15,9 +15,7 @@ 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 { key_prefix: Vec, #[borsh(skip)] diff --git a/near-sdk/src/collections/tree_map.rs b/near-sdk/src/collections/tree_map.rs index ccba0cb87..1c8300f7e 100644 --- a/near-sdk/src/collections/tree_map.rs +++ b/near-sdk/src/collections/tree_map.rs @@ -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 /// @@ -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 { root: u64, val: LookupMap, diff --git a/near-sdk/src/collections/vector.rs b/near-sdk/src/collections/vector.rs index 6f957ef27..35a40cee1 100644 --- a/near-sdk/src/collections/vector.rs +++ b/near-sdk/src/collections/vector.rs @@ -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}; @@ -21,9 +21,7 @@ fn expect_consistent_state(val: Option) -> 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 { len: u64, prefix: Vec, diff --git a/near-sdk/src/json_types/vector.rs b/near-sdk/src/json_types/vector.rs index fd9fea63c..b606eefac 100644 --- a/near-sdk/src/json_types/vector.rs +++ b/near-sdk/src/json_types/vector.rs @@ -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` to base64 string. @@ -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",