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
#![no_std]

use gmeta::{InOut, Metadata};
use gstd::collections::{btree_map::Entry, BTreeMap};
use gstd::prelude::*;
use gstd::ActorId;
use gstd::MessageId;

impl Metadata for Contract {
    type Init = ();
    type Handle = InOut<Action, Event>;
    type Others = ();
    type Reply = ();
    type Signal = ();
    type State = InOut<Query, Reply>;
}

#[derive(Clone, Default, Encode, Decode, TypeInfo)]
pub struct Contract(pub BTreeMap<String, String>);

impl Contract {
    pub fn add_url(&mut self, code: String, url: String) {
        match self.0.entry(code) {
            Entry::Vacant(v) => {
                v.insert(url);
            }
            Entry::Occupied(_) => {
                panic!("failed to add url: code exists")
            }
        }
    }
    pub fn get_url(&self, code: String) -> Option<String> {
        self.0.get(&code).cloned()
    }
}

#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
pub enum Action {
    AddUrl { code: String, url: String },
}

#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
pub enum Event {
    Added { code: String, url: String },
}

#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
pub enum Query {
    All,
    Code(String),
    Whoami,
    BlockNumber,
    BlockTimestamp,
    ProgramId,
    MessageId,
}

#[derive(Encode, Decode, TypeInfo)]
pub enum Reply {
    All(Contract),
    Url(Option<String>),
    Whoami(ActorId),
    BlockNumber(u32),
    BlockTimestamp(u64),
    ProgramId(ActorId),
    MessageId(MessageId),
}