io/
lib.rs

1#![no_std]
2
3use gmeta::{InOut, Metadata};
4use gstd::collections::{btree_map::Entry, BTreeMap};
5use gstd::prelude::*;
6use gstd::ActorId;
7use gstd::MessageId;
8
9impl Metadata for Contract {
10    type Init = ();
11    type Handle = InOut<Action, Event>;
12    type Others = ();
13    type Reply = ();
14    type Signal = ();
15    type State = InOut<Query, Reply>;
16}
17
18#[derive(Clone, Default, Encode, Decode, TypeInfo)]
19pub struct Contract(pub BTreeMap<String, String>);
20
21impl Contract {
22    pub fn add_url(&mut self, code: String, url: String) {
23        match self.0.entry(code) {
24            Entry::Vacant(v) => {
25                v.insert(url);
26            }
27            Entry::Occupied(_) => {
28                panic!("failed to add url: code exists")
29            }
30        }
31    }
32    pub fn get_url(&self, code: String) -> Option<String> {
33        self.0.get(&code).cloned()
34    }
35}
36
37#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
38pub enum Action {
39    AddUrl { code: String, url: String },
40}
41
42#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
43pub enum Event {
44    Added { code: String, url: String },
45}
46
47#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
48pub enum Query {
49    All,
50    Code(String),
51    Whoami,
52    BlockNumber,
53    BlockTimestamp,
54    ProgramId,
55    MessageId,
56}
57
58#[derive(Encode, Decode, TypeInfo)]
59pub enum Reply {
60    All(Contract),
61    Url(Option<String>),
62    Whoami(ActorId),
63    BlockNumber(u32),
64    BlockTimestamp(u64),
65    ProgramId(ActorId),
66    MessageId(MessageId),
67}