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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! Utilities for interacting with the native key-value storage.

pub mod iterable;
pub mod map;

mod single_item;
mod item_space;

pub use single_item::*;
pub use item_space::*;

use std::{any, convert::{TryFrom, TryInto}};

use crate::{
    bin_serde::{FadromaSerialize, FadromaDeserialize, FadromaSerializeExt, Deserializer},
    cosmwasm_std::{
        Storage, StdResult, StdError, CanonicalAddr,
        Addr, Uint64, Uint128, Uint256, Uint512
    }
};

/// Construct a storage namespace. It creates a
/// zero-sized struct with the given type name and
/// implements [`Namespace`] on it with the provied
/// byte slice literal.
/// 
/// # Examples
/// 
/// ```
/// use fadroma::storage::Namespace;
/// 
/// fadroma::namespace!(MyNamespace, b"ns_bytes");
/// assert_eq!(MyNamespace::NAMESPACE, b"ns_bytes");
/// ```
#[macro_export]
macro_rules! namespace {
    ($visibility:vis $name:ident, $bytes: literal) => {
        $visibility struct $name;

        impl $crate::storage::Namespace for $name {
            const NAMESPACE: &'static [u8] = $bytes;
        }
    };
}

pub type Segments<'a> = &'a [&'a [u8]];

/// Represents a namespace, usually acting as a prefix
/// to a dynamically generated key. We only do this so
/// that we can have strongly typed keys and storage types.
/// Use the [`namespace`] macro to generate one.
/// 
/// # Examples
/// 
/// ```
/// use fadroma::storage::Namespace;
/// 
/// fadroma::namespace!(MyNamespace, b"ns_bytes");
/// assert_eq!(MyNamespace::NAMESPACE, b"ns_bytes");
/// ```
pub trait Namespace {
    const NAMESPACE: &'static [u8];
}

/// Implemented for types that act as CW storage keys by writing
/// bytes into the given buffer. What those bytes represent and
/// where they are coming from as well as how they are written
/// into the buffer entirely depends on the implementing type.
/// 
/// The [`Key::size`] method is used to report the amount of
/// bytes that the key will write. This allows us to efficiently
/// allocate the exact amount of memory that we will need to construct
/// the final key. It exists because the provided buffer might be larger
/// than the size of the given key since multiple keys can be concatenated
/// or a prefix (such as a [`Namespace`]) might be added to the final key.
/// This depends entirely on the given scenario and is taken care of by the
/// storage types. There are already several key types provided which should
/// cover pretty much all use cases.
pub trait Key {
    fn size(&self) -> usize;
    fn write_segments(&self, buf: &mut Vec<u8>);
}

/// Represents types that can be used to construct a [`TypedKey`] and
/// its variants. Although it has the exact same method definitions as
/// the [`Key`] trait, it differs in its specific usage scenario and as
/// such the two traits are not connected in any way at the type level.
pub trait Segment {
    fn size(&self) -> usize;
    fn write_segment(&self, buf: &mut Vec<u8>);
}

/// A key with an arbitrary number of segments.
/// Writes them in order of the iteration.
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
pub struct CompositeKey<'a>(Segments<'a>);

/// A key which consists of a static byte slice.
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
pub struct StaticKey(pub &'static [u8]);

/// A key with a pre-defined number of segments.
/// Writes them in order of the iteration.
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
pub struct FixedSegmentSizeKey<'a, const N: usize>([&'a [u8]; N]);

/// A strongly-typed key with segments defined by the concrete type
/// which must implement [`Segment`]. For typed keys which consist of
/// multiple types use [`TypedKey2`], [`TypedKey3`] and [`TypedKey4`].
/// Constructs the key in order of definition.
/// 
/// # Examples
/// 
/// ```
/// use fadroma::{
///     cosmwasm_std::testing::mock_dependencies,
///     storage::{Key, ItemSpace, TypedKey2}
/// };
/// 
/// fadroma::namespace!(NumbersNs, b"numbers");
/// 
/// // Storage for u64 numbers with a key that consists of b"numbers" + a string + a byte.
/// const NUMBERS: ItemSpace::<u64, NumbersNs, TypedKey2<String, u8>> = ItemSpace::new();
/// 
/// let mut deps = mock_dependencies();
/// let storage = deps.as_mut().storage;
/// 
/// let string_segment = "hello".to_string();
/// let number_segment = 33u8;
/// 
/// NUMBERS.save(storage, (&string_segment, &number_segment), &1).unwrap();
/// 
/// // Can also be constructed like this
/// let key = TypedKey2::from((&string_segment, &number_segment));
/// NUMBERS.save(storage, key.clone(), &1).unwrap();
/// 
/// let mut bytes: Vec<u8> = Vec::with_capacity(key.size());
/// key.write_segments(&mut bytes);
/// 
/// assert_eq!(
///     bytes,
///     [string_segment.as_bytes(), &number_segment.to_be_bytes()].concat()
/// );
/// ```
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
pub struct TypedKey<'a, T: Segment + ?Sized>(&'a T);

/// Save something to the storage.
#[inline]
pub fn save<T: FadromaSerialize> (
    storage: &mut dyn Storage,
    key: impl AsRef<[u8]>,
    value: &T
) -> StdResult<()> {
    let bytes = serialize(value)?;
    storage.set(key.as_ref(), &bytes);

    Ok(())
}

/// Remove something from the storage.
#[inline]
pub fn remove(
    storage: &mut dyn Storage,
    key: impl AsRef<[u8]>
) {
    storage.remove(key.as_ref());
}

/// Load something from the storage.
#[inline]
pub fn load<T: FadromaDeserialize> (
    storage: &dyn Storage,
    key: impl AsRef<[u8]>
) -> StdResult<Option<T>> {
    match storage.get(key.as_ref()) {
        Some(data) => {
            let item = deserialize::<T>(&data)?;

            Ok(Some(item))
        },
        None => Ok(None)
    }
}

#[inline(always)]
pub(crate) fn serialize<T: FadromaSerialize>(value: &T) -> StdResult<Vec<u8>> {
    value.serialize().map_err(|e|
        StdError::serialize_err(any::type_name::<T>(), e)
    )
}

#[inline(always)]
pub(crate) fn deserialize<T: FadromaDeserialize>(bytes: &[u8]) -> StdResult<T> {
    let mut de = Deserializer::from(&bytes);

    de.deserialize::<T>().map_err(|e|
        StdError::parse_err(any::type_name::<T>(), e)
    )
}

impl<'a> Key for CompositeKey<'a> {
    #[inline]
    fn size(&self) -> usize {
        self.0.iter().map(|x| x.len()).sum()
    }

    #[inline]
    fn write_segments(&self, buf: &mut Vec<u8>) {
        for segment in self.0 {
            buf.extend_from_slice(segment);
        }
    }
}

impl<'a> CompositeKey<'a> {
    #[inline]
    pub fn new(segments: Segments<'a>) -> Self {
        Self(segments)
    }
}

impl<'a> From<Segments<'a>> for CompositeKey<'a> {
    #[inline]
    fn from(segments: Segments<'a>) -> Self {
        Self(segments)
    }
}

impl Key for StaticKey {
    #[inline]
    fn size(&self) -> usize {
        self.0.len()
    }

    #[inline]
    fn write_segments(&self, buf: &mut Vec<u8>) {
        buf.extend_from_slice(self.0);
    }
}

impl<'a, const N: usize> Key for FixedSegmentSizeKey<'a, N> {
    #[inline]
    fn size(&self) -> usize {
        self.0.iter().map(|x| x.len()).sum()
    }

    #[inline]
    fn write_segments(&self, buf: &mut Vec<u8>) {
        for segment in self.0 {
            buf.extend_from_slice(segment);
        }
    }
}

impl<'a, const N: usize> FixedSegmentSizeKey<'a, N> {
    #[inline]
    pub fn new(segments: [&'a [u8]; N]) -> Self {
        Self(segments)
    }
}

impl<'a, const N: usize> From<[&'a [u8]; N]> for FixedSegmentSizeKey<'a, N> {
    #[inline]
    fn from(segments: [&'a [u8]; N]) -> Self {
        Self(segments)
    }
}

impl<'a, const N: usize> TryFrom<Segments<'a>> for FixedSegmentSizeKey<'a, N> {
    type Error = StdError;

    #[inline]
    fn try_from(segments: Segments<'a>) -> Result<Self, Self::Error> {
        let segments: [&'a [u8]; N] = segments.try_into()
            .map_err(|_|
                StdError::invalid_data_size(N, segments.len())
            )?;

        Ok(Self(segments))
    }
}

impl<'a, T: Segment + ?Sized> Key for TypedKey<'a, T> {
    #[inline]
    fn size(&self) -> usize {
        self.0.size()
    }

    #[inline]
    fn write_segments(&self, buf: &mut Vec<u8>) {
        self.0.write_segment(buf);
    }
}

impl<'a, T: Segment + ?Sized> From<&'a T> for TypedKey<'a, T> {
    #[inline]
    fn from(value: &'a T) -> Self {
        Self(value)
    }
}

macro_rules! impl_typed_key {
    ($name:ident $(<$lt:lifetime, $($param:ident),+>)+ [$($num:tt),+]) => {
        #[derive(Clone, Copy, PartialEq, Hash, Debug)]
        pub struct $name $(<$lt, $($param: Segment + ?Sized),+>)+ (($($(&$lt $param),+)+));

        impl $(<$lt, $($param: Segment + ?Sized),+>)+ Key for $name $(<$lt, $($param),+>)+ {
            #[inline]
            fn size(&self) -> usize {
                self.0.0.size() $(+ self.0.$num.size())+
            }
        
            #[inline]
            fn write_segments(&self, buf: &mut Vec<u8>) {
                self.0.0.write_segment(buf);
                $(self.0.$num.write_segment(buf);)+
            }
        }

        impl $(<$lt, $($param: Segment + ?Sized),+>)+ From<($($(&$lt $param),+)+)> for $name $(<$lt, $($param),+>)+ {
            #[inline]
            fn from(value: ($($(&$lt $param),+)+)) -> Self {
                Self(value)
            }
        }
    };
}

impl_typed_key!(TypedKey2<'a, T1, T2> [1]);
impl_typed_key!(TypedKey3<'a, T1, T2, T3> [1, 2]);
impl_typed_key!(TypedKey4<'a, T1, T2, T3, T4> [1, 2, 3]);

impl<T: Namespace> Key for T {
    #[inline]
    fn size(&self) -> usize {
        Self::NAMESPACE.len()
    }

    #[inline]
    fn write_segments(&self, buf: &mut Vec<u8>) {
        buf.extend_from_slice(Self::NAMESPACE);
    }
}

impl<T: Key> Segment for T {
    #[inline]
    fn size(&self) -> usize {
        <Self as Key>::size(self)
    }

    #[inline]
    fn write_segment(&self, buf: &mut Vec<u8>) {
        self.write_segments(buf);
    }
}

impl Segment for &str {
    #[inline]
    fn size(&self) -> usize {
        self.as_bytes().len()
    }

    #[inline]
    fn write_segment(&self, buf: &mut Vec<u8>) {
        buf.extend_from_slice(self.as_bytes());
    }
}

impl Segment for String {
    #[inline]
    fn size(&self) -> usize {
        self.as_bytes().len()
    }

    #[inline]
    fn write_segment(&self, buf: &mut Vec<u8>) {
        buf.extend_from_slice(self.as_bytes());
    }
}

impl Segment for Addr {
    #[inline]
    fn size(&self) -> usize {
        self.as_bytes().len()
    }

    #[inline]
    fn write_segment(&self, buf: &mut Vec<u8>) {
        buf.extend_from_slice(self.as_bytes());
    }
}

impl Segment for CanonicalAddr {
    #[inline]
    fn size(&self) -> usize {
        self.len()
    }

    #[inline]
    fn write_segment(&self, buf: &mut Vec<u8>) {
        buf.extend_from_slice(&self);
    }
}

macro_rules! impl_num_segment {
    ($data:ty) => {
        impl Segment for $data {
            #[inline]
            fn size(&self) -> usize {
                std::mem::size_of::<Self>()
            }
        
            #[inline]
            fn write_segment(&self, buf: &mut Vec<u8>) {
                buf.extend_from_slice(&self.to_be_bytes());
            }
        }
    };
}

impl_num_segment!(u8);
impl_num_segment!(u16);
impl_num_segment!(u32);
impl_num_segment!(u64);
impl_num_segment!(u128);
impl_num_segment!(Uint64);
impl_num_segment!(Uint128);
impl_num_segment!(Uint256);
impl_num_segment!(Uint512);

#[inline]
fn not_found_error<T>() -> StdError {
    StdError::not_found(format!("Storage load: {}", any::type_name::<T>()))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn typed_keys() {
        const WORD: &str = "test";

        fn test(key: impl Key, len: usize) {
            assert_eq!(key.size(), WORD.len() * len);

            let mut buf = Vec::with_capacity(key.size());
            key.write_segments(&mut buf);

            assert_eq!(buf, WORD.as_bytes().repeat(len));
        }

        test(TypedKey2::from((&WORD, &WORD)), 2);
        test(TypedKey3::from((&WORD, &WORD, &WORD)), 3);
        test(TypedKey4::from((&WORD, &WORD, &WORD, &WORD)), 4);
    }
}