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
//! This module defines storable types and their implementations for use with stable structures.

use candid::{Principal, CandidType, Deserialize, Encode, Decode};
use ic_stable_structures::memory_manager::VirtualMemory;
use ic_stable_structures::{
    storable::Bound, DefaultMemoryImpl, Storable,
};
use std::borrow::Cow;

pub type Memory = VirtualMemory<DefaultMemoryImpl>;

pub const MAX_VALUE_SIZE: u32 = 100;
pub const MAX_KEY_SIZE: u32 = 100;

/// A wrapper for `Principal` to make it storable.
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct StorablePrincipal(pub Principal);

/// Represents the permission status of a canister.
#[derive(CandidType, Deserialize, Clone)]
pub struct CanisterPermission(pub bool);

/// Represents a sum of principal and achievement.
#[derive(CandidType, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct PrincipalSum(pub String);

/// Metadata for the reputation module.
#[derive(CandidType, Deserialize, Clone)]
pub struct ReputationModuleMetadata {
    pub achievement_collection: Principal,
    pub issuer_name: String,
    pub issuer_description: String,
    pub total_issued: u128
}

/// Represents a standard supported by the reputation module.
#[derive(CandidType, Deserialize)]
pub struct Standard {
    pub name: String,
    pub url: String,
}

macro_rules! impl_storable {
    ($($t:ty),*) => {
        $(
            impl Storable for $t {
                fn to_bytes(&self) -> Cow<[u8]> {
                    Cow::Owned(Encode!(self).unwrap())
                }

                fn from_bytes(bytes: Cow<[u8]>) -> Self {
                    Decode!(bytes.as_ref(), Self).unwrap()
                }

                const BOUND: Bound = Bound::Bounded {
                    max_size: MAX_VALUE_SIZE,
                    is_fixed_size: false,
                };
            }
        )*
    };
}

impl Storable for StorablePrincipal {
    fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
        self.0.to_bytes()
    }

    fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
        Self(Principal::from_bytes(bytes))
    }

    const BOUND: Bound = Bound::Bounded {
        max_size: MAX_KEY_SIZE,
        is_fixed_size: false,
    };
}

impl Storable for PrincipalSum {
    fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
        self.0.to_bytes()
    }

    fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
        Self(String::from_bytes(bytes))
    }

    const BOUND: Bound = Bound::Bounded {
        max_size: MAX_KEY_SIZE,
        is_fixed_size: false,
    };
}

impl ReputationModuleMetadata {
    /// Creates a default instance of `ReputationModuleMetadata`.
    pub fn default() -> Self {
        Self {
            achievement_collection: Principal::anonymous(),
            issuer_description: String::default(),
            issuer_name: String::default(),
            total_issued: u128::default()
        }   
    }
}

impl_storable!(ReputationModuleMetadata, Standard);

impl Storable for CanisterPermission {
    fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
        self.0.to_bytes()
    }

    fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
        Self(bool::from_bytes(bytes))
    }

    const BOUND: Bound = Bound::Bounded {
        max_size: MAX_KEY_SIZE,
        is_fixed_size: false,
    };
}