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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! *Feature flag: `composability`*
//! Modular contracts using native trait composition.

use serde::{de::DeserializeOwned, Serialize};
use crate::{prelude::*, storage::{concat, namespace::{key_prefix, key_prefix_nested}}};

#[cfg(not(target_arch = "wasm32"))]
pub mod tester;
#[cfg(not(target_arch = "wasm32"))]
pub use tester::*;

pub trait BaseComposable<S, A, Q> {
    fn storage(&self) -> &S;
    fn storage_mut(&mut self) -> &mut S;
    fn api(&self) -> &A;
    fn querier(&self) -> &Q;
}

pub trait Composable<S, A, Q>: BaseComposable<S, A, Q> {
    fn set<Value: Serialize>(&mut self, key: &[u8], value: &Value) -> UsuallyOk;
    /// Uses a single slice as namespace and it combines it with the key
    fn set_ns<Value: Serialize>(&mut self, ns: &[u8], key: &[u8], value: &Value) -> UsuallyOk;
    /// Uses an array of slices which are then combined according to the following spec:
    /// https://github.com/webmaster128/key-namespacing#nesting
    fn set_multi_ns<Value: Serialize>(
        &mut self,
        ns: &[&[u8]],
        key: &[u8],
        value: &Value,
    ) -> UsuallyOk;

    fn get<Value: DeserializeOwned>(&self, key: &[u8]) -> Eventually<Value>;
    fn get_ns<Value: DeserializeOwned>(&self, ns: &[u8], key: &[u8]) -> Eventually<Value>;
    fn get_multi_ns<Value: DeserializeOwned>(&self, ns: &[&[u8]], key: &[u8]) -> Eventually<Value>;

    fn remove(&mut self, key: &[u8]);
    fn remove_ns(&mut self, ns: &[u8], key: &[u8]);
    fn remove_multi_ns(&mut self, ns: &[&[u8]], key: &[u8]);

    fn humanize<Value: Humanize>(&self, value: Value) -> StdResult<Value::Output>;
    fn canonize<Value: Canonize>(&self, value: Value) -> StdResult<Value::Output>;
}

/// Trait for handle messages
pub trait HandleDispatch <S, A, Q, C> where
    S: Storage,
    A: Api,
    Q: Querier,
    C: Composable<S, A, Q>
{
    fn dispatch_handle (self, core: &mut C, env: Env) -> StdResult<HandleResponse>;
}

/// Trait for query messages
pub trait QueryDispatch <S, A, Q, C, R> where
    S: Storage,
    A: Api,
    Q: Querier,
    C: Composable<S, A, Q>
{
    fn dispatch_query (self, core: &C) -> StdResult<R>;
}

/// Implement the Composable Core for a `struct { storage, api, querier }`
#[macro_export]
macro_rules! make_composable {
    ($Struct:ty) => {
        // base trait with no generic methods, in order to to support `dyn`
        impl<S: Storage, A: Api, Q: Querier> BaseComposable<S, A, Q> for $Struct {
            #[inline]
            fn storage(&self) -> &S {
                &self.storage
            }

            #[inline]
            fn storage_mut(&mut self) -> &mut S {
                &mut self.storage
            }

            #[inline]
            fn api(&self) -> &A {
                &self.api
            }

            #[inline]
            fn querier(&self) -> &Q {
                &self.querier
            }
        }

        impl<S: Storage, A: Api, Q: Querier> Composable<S, A, Q> for $Struct {
            #[inline]
            fn set<Value: serde::Serialize>(&mut self, key: &[u8], value: &Value) -> UsuallyOk {
                self.storage.set(key, &to_vec(value)?);
                Ok(())
            }

            #[inline]
            fn set_ns<Value: serde::Serialize>(
                &mut self,
                ns: &[u8],
                key: &[u8],
                value: &Value,
            ) -> UsuallyOk {
                let ns = key_prefix(ns);
                self.set(&concat(&ns, key), value)
            }

            #[inline]
            fn set_multi_ns<Value: serde::Serialize>(
                &mut self,
                ns: &[&[u8]],
                key: &[u8],
                value: &Value,
            ) -> UsuallyOk {
                let ns = key_prefix_nested(ns);
                self.set(&concat(&ns, key), value)
            }

            #[inline]
            fn get<Value: serde::de::DeserializeOwned>(&self, key: &[u8]) -> Eventually<Value> {
                match self.storage.get(key) {
                    Some(data) => Ok(Some(from_slice(&data)?)),
                    None => Ok(None)
                }
            }

            #[inline]
            fn get_ns<Value: serde::de::DeserializeOwned>(
                &self,
                ns: &[u8],
                key: &[u8],
            ) -> Eventually<Value> {
                let ns = key_prefix(ns);
                self.get(&concat(&ns, key))
            }

            #[inline]
            fn get_multi_ns<Value: serde::de::DeserializeOwned>(
                &self,
                ns: &[&[u8]],
                key: &[u8],
            ) -> Eventually<Value> {
                let ns = key_prefix_nested(ns);
                self.get(&concat(&ns, key))
            }

            #[inline]
            fn remove(&mut self, key: &[u8]) {
                self.storage.remove(key);
            }

            #[inline]
            fn remove_ns(&mut self, ns: &[u8], key: &[u8]) {
                let ns = key_prefix(ns);
                self.remove(&concat(&ns, key));
            }

            #[inline]
            fn remove_multi_ns(&mut self, ns: &[&[u8]], key: &[u8]) {
                let ns = key_prefix_nested(ns);
                self.remove(&concat(&ns, key));
            }

            #[inline]
            fn humanize<Value: Humanize>(&self, value: Value) -> StdResult<Value::Output> {
                value.humanize(&self.api)
            }

            #[inline]
            fn canonize<Value: Canonize>(&self, value: Value) -> StdResult<Value::Output> {
                value.canonize(&self.api)
            }
        }
    };
}

make_composable!(Extern<S, A, Q>);