Skip to content

Commit

Permalink
fix borshschema
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed Feb 28, 2024
1 parent c2eef9b commit c8f140b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
11 changes: 11 additions & 0 deletions near-sdk/src/collections/lazy_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@ use borsh::{to_vec, BorshDeserialize, BorshSerialize};

use crate::env;
use crate::IntoStorageKey;
#[cfg(feature = "abi")]
use borsh::BorshSchema;

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.
#[cfg(feature = "abi")]
#[derive(BorshSerialize, BorshDeserialize, BorshSchema)]
pub struct LazyOption<T> {
storage_key: Vec<u8>,
#[borsh(skip)]
el: PhantomData<T>,
}

#[cfg(not(feature = "abi"))]
#[derive(BorshSerialize, BorshDeserialize)]
pub struct LazyOption<T> {
storage_key: Vec<u8>,
Expand Down
12 changes: 10 additions & 2 deletions near-sdk/src/collections/lookup_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::marker::PhantomData;
#[cfg(feature = "abi")]
use borsh::BorshSchema;
use borsh::{to_vec, BorshDeserialize, BorshSerialize};
use near_sdk_macros::NearSchema;

use crate::collections::append_slice;
use crate::{env, IntoStorageKey};
Expand All @@ -17,7 +16,16 @@ 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)]
#[cfg(feature = "abi")]
#[derive(BorshSerialize, BorshDeserialize, BorshSchema)]
pub struct LookupMap<K, V> {
key_prefix: Vec<u8>,
#[borsh(skip)]
el: PhantomData<(K, V)>,
}

#[cfg(not(feature = "abi"))]
#[derive(BorshSerialize, BorshDeserialize)]
pub struct LookupMap<K, V> {
key_prefix: Vec<u8>,
#[borsh(skip)]
Expand Down
15 changes: 13 additions & 2 deletions near-sdk/src/collections/tree_map.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use borsh::{BorshDeserialize, BorshSerialize};
use near_sdk_macros::NearSchema;
#[cfg(feature = "abi")]
use borsh::BorshSchema;
use std::ops::Bound;

use crate::collections::LookupMap;
Expand All @@ -15,7 +16,17 @@ use crate::{env, IntoStorageKey};
/// - `above`/`below`: O(log(N))
/// - `range` of K elements: O(Klog(N))
///
#[derive(BorshSerialize, BorshDeserialize, NearSchema)]
#[cfg(feature = "abi")]
#[derive(BorshSerialize, BorshDeserialize, BorshSchema)]
pub struct TreeMap<K, V> {
root: u64,
val: LookupMap<K, V>,
tree: Vector<Node<K>>,
}

#[cfg(not(feature = "abi"))]
#[derive(BorshSerialize, BorshDeserialize)]
pub struct TreeMap<K, V> {
root: u64,
val: LookupMap<K, V>,
Expand Down
12 changes: 11 additions & 1 deletion near-sdk/src/collections/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,17 @@ 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)]
#[cfg(feature = "abi")]
#[derive(BorshSerialize, BorshDeserialize, BorshSchema)]
pub struct Vector<T> {
len: u64,
prefix: Vec<u8>,
#[borsh(skip)]
el: PhantomData<T>,
}

#[cfg(not(feature = "abi"))]
#[derive(BorshSerialize, BorshDeserialize)]
pub struct Vector<T> {
len: u64,
prefix: Vec<u8>,
Expand Down
7 changes: 7 additions & 0 deletions near-sdk/src/json_types/vector.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "abi")]
use borsh::BorshSchema;

/// Helper class to serialize/deserialize `Vec<u8>` to base64 string.
#[cfg(feature = "abi")]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize, BorshSerialize, BorshSchema)]
pub struct Base64VecU8(#[serde(with = "base64_bytes")] pub Vec<u8>);

#[cfg(not(feature = "abi"))]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize, BorshSerialize)]
pub struct Base64VecU8(#[serde(with = "base64_bytes")] pub Vec<u8>);

Expand Down

0 comments on commit c8f140b

Please sign in to comment.