1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use super::StorageMapper;
use crate::abi::{TypeAbi, TypeDescriptionContainer, TypeName};
use crate::api::{EndpointFinishApi, ErrorApi, StorageReadApi, StorageWriteApi};
use crate::io::EndpointResult;
use crate::storage::{storage_get, storage_set};
use crate::types::BoxedBytes;
use core::marker::PhantomData;
use dharitri_codec::{TopDecode, TopEncode};

/// Manages a single serializable item in storage.
pub struct SingleValueMapper<SA, T>
where
	SA: StorageReadApi + StorageWriteApi + ErrorApi + Clone + 'static,
	T: TopEncode + TopDecode + 'static,
{
	api: SA,
	key: BoxedBytes,
	_phantom: core::marker::PhantomData<T>,
}

impl<SA, T> StorageMapper<SA> for SingleValueMapper<SA, T>
where
	SA: StorageReadApi + StorageWriteApi + ErrorApi + Clone + 'static,
	T: TopEncode + TopDecode,
{
	fn new(api: SA, key: BoxedBytes) -> Self {
		SingleValueMapper {
			api,
			key,
			_phantom: PhantomData,
		}
	}
}

impl<SA, T> SingleValueMapper<SA, T>
where
	SA: StorageReadApi + StorageWriteApi + ErrorApi + Clone + 'static,
	T: TopEncode + TopDecode,
{
	/// Retrieves current value from storage.
	pub fn get(&self) -> T {
		storage_get(self.api.clone(), self.key.as_slice())
	}

	/// Saves argument to storage.
	pub fn set(&self, new_value: &T) {
		storage_set(self.api.clone(), self.key.as_slice(), new_value);
	}

	/// Returns whether the storage managed by this is empty
	pub fn is_empty(&self) -> bool {
		self.api.storage_load_len(self.key.as_slice()) == 0
	}

	/// Clears the storage for this mapper
	pub fn clear(&self) {
		self.api.storage_store_slice_u8(self.key.as_slice(), &[]);
	}

	/// Syntactic sugar, to more compactly express a get, update and set in one line.
	/// Takes whatever lies in storage, apples the given closure and saves the final value back to storage.
	pub fn update<F: FnOnce(&mut T)>(&self, f: F) {
		let mut value = self.get();
		f(&mut value);
		self.set(&value);
	}
}

impl<SA, FA, T> EndpointResult<FA> for SingleValueMapper<SA, T>
where
	SA: StorageReadApi + StorageWriteApi + ErrorApi + Clone + 'static,
	FA: EndpointFinishApi + 'static,
	T: TopEncode + TopDecode + EndpointResult<FA>,
{
	fn finish(&self, api: FA) {
		self.get().finish(api);
	}
}

impl<SA, T> TypeAbi for SingleValueMapper<SA, T>
where
	SA: StorageReadApi + StorageWriteApi + ErrorApi + Clone + 'static,
	T: TopEncode + TopDecode + TypeAbi,
{
	fn type_name() -> TypeName {
		T::type_name()
	}

	fn provide_type_descriptions<TDC: TypeDescriptionContainer>(accumulator: &mut TDC) {
		T::provide_type_descriptions(accumulator)
	}
}