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
use parity_scale_codec::{Decode, Encode};
use scale_info::prelude::vec::Vec;

// For more info refer to:
// https://github.com/fragcolor-xyz/shards/blob/devel/include/shards.h

#[cfg(not(feature = "std"))]
type String = Vec<u8>;

#[derive(Encode, Decode, Copy, Clone, PartialEq, Debug, Eq, scale_info::TypeInfo)]
pub enum CodeType {
    /// A list of shards, to be injected into more complex blocks of code or wires
    Shards,
    /// An actual wire
    Wire,
}

#[derive(Encode, Decode, Copy, Clone, PartialEq, Debug, Eq, scale_info::TypeInfo)]
pub enum VariableType {
    None,
    Any,
    /// VendorID, TypeID
    Enum(u32, u32),
    Bool,
    Int,    // A 64bits int
    Int2,   // A vector of 2 64bits ints
    Int3,   // A vector of 3 32bits ints
    Int4,   // A vector of 4 32bits ints
    Int8,   // A vector of 8 16bits ints
    Int16,  // A vector of 16 8bits ints
    Float,  // A 64bits float
    Float2, // A vector of 2 64bits floats
    Float3, // A vector of 3 32bits floats
    Float4, // A vector of 4 32bits floats
    Color,  // A vector of 4 uint8
    // Non Blittables
    Bytes,
    String,
    Image,
    Seq,
    Table,
    /// VendorID, TypeID
    Object(u32, u32),
    Audio,
}

#[derive(Encode, Decode, Clone, PartialEq, Debug, Eq, scale_info::TypeInfo)]
pub struct CodeInfo {
    pub kind: CodeType,
    pub requires: Vec<(String, VariableType)>,
    pub exposes: Vec<(String, VariableType)>,
    pub inputs: Vec<VariableType>,
    pub output: VariableType,
}

#[derive(Encode, Decode, Clone, PartialEq, Debug, Eq, scale_info::TypeInfo)]
pub enum TraitInfo {
    SingleType(VariableType),
    MultipleTypes(Vec<VariableType>),
    Code(CodeInfo),
}

pub type Traits = Vec<(String, TraitInfo)>;

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

    #[test]
    fn encode_decode_simple_1() {
        let mut trait1 = vec![("int1".to_string(), TraitInfo::SingleType(VariableType::Int))];

        // THIS IS the way we reprocess the trait declaration before sorting it on chain and hashing it
        trait1.dedup_by(|a, b| a.0.to_lowercase() == b.0.to_lowercase());
        trait1.sort_by(|a, b| a.0.to_lowercase().cmp(&b.0.to_lowercase()));

        let e_trait1 = trait1.encode();

        let d_trait1 = Traits::decode(&mut e_trait1.as_slice()).unwrap();

        assert!(trait1 == d_trait1);
    }
}