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

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

pub type Memory = VirtualMemory<DefaultMemoryImpl>;

const MAX_VALUE_SIZE: u32 = 130;
const MAX_KEY_SIZE: u32 = 130;

/// Enum representing the status of an achievement.
pub enum AchievementStatusEnum {
    NotAllowed,
    Allowed
}

/// Metadata for an achievement.
#[derive(CandidType, Deserialize, Clone)]
pub struct AchievementMetadata {
    pub achievement_name: String,
    pub achievement_description: String
}

impl AchievementMetadata {
    /// Creates a default instance of `AchievementMetadata`.
    pub fn default() -> Self {
        Self {
            achievement_description: String::default(),
            achievement_name: String::default()
        }   
    }
}

impl Storable for AchievementMetadata {
    fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
        Cow::Owned(Encode!(self).unwrap())
    }

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

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

impl AchievementStatusEnum {
    /// Converts the enum variant to a `u8` value.
    pub fn to_u8(&self) -> u8 {
        match self {
            AchievementStatusEnum::NotAllowed => 0,
            AchievementStatusEnum::Allowed => 1,
        }
    }

    /// Converts a `u8` value to the corresponding enum variant.
    pub fn from_u8(value: u8) -> Self {
        match value {
            0 => AchievementStatusEnum::NotAllowed,
            1 => AchievementStatusEnum::Allowed,
            _ => panic!("Invalid value for AchievementStatusEnum"),
        }
    }

    /// Converts a `u8` value to a string representation of the enum variant.
    pub fn to_string_from_u8(value: u8) -> String {
        match Self::from_u8(value) {
            AchievementStatusEnum::NotAllowed => String::from("not_allowed"),
            AchievementStatusEnum::Allowed => String::from("allowed"),
            _ => panic!("Invalid value for AchievementStatusEnum"),
        }
    }
}

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

/// Represents the status of an achievement.
#[derive(CandidType, Deserialize, Clone)]
pub struct AchievementStatus(pub u8);

impl Storable for PrincipalStorable {
    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 AchievementStatus {
    fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
        self.0.to_bytes()
    }

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

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

/// Represents a signature.
#[derive(CandidType, Serialize, Debug, Deserialize)]
pub struct Signature(pub String);

impl Storable for Signature {
    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,
    };
}