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
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Hash)]
pub struct FnParameter {
#[serde(rename = "type")]
pub parameter_type: String,
pub name: String,
}
impl FnParameter {
#[allow(dead_code)]
pub fn new<S: Into<String>>(n: S, t: S) -> FnParameter {
FnParameter {
name: n.into(),
parameter_type: t.into(),
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Hash)]
pub struct FnDeclaration {
#[serde(default)]
pub name: String,
pub inputs: Vec<FnParameter>,
pub outputs: Vec<FnParameter>,
}
impl Default for FnDeclaration {
fn default() -> Self {
FnDeclaration {
name: String::new(),
inputs: Vec::new(),
outputs: Vec::new(),
}
}
}
impl FnDeclaration {
pub fn new() -> Self {
Default::default()
}
}
#[derive(Default, Serialize, Deserialize, Clone, Debug, PartialEq, Hash)]
pub struct TraitFns {
#[serde(default)]
pub functions: Vec<String>,
}
impl TraitFns {
pub fn new() -> Self {
Default::default()
}
}
#[derive(Default, Serialize, Deserialize, Clone, Debug, PartialEq, Hash)]
pub struct Trait {
#[serde(default)]
pub functions: Vec<FnDeclaration>,
}
impl Trait {
pub fn new() -> Self {
Default::default()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_trait_fns_build_and_compare() {
let fixture: TraitFns = serde_json::from_str(
r#"{
"functions": ["test"]
}"#,
)
.unwrap();
let mut trait_fns = TraitFns::new();
trait_fns.functions.push(String::from("test"));
assert_eq!(fixture, trait_fns);
}
#[test]
fn test_trait_build_and_compare() {
let fixture: Trait = serde_json::from_str(
r#"{
"functions": [
{
"name": "test",
"inputs" : [
{
"name": "post",
"type": "string"
}
],
"outputs" : [
{
"name": "hash",
"type": "string"
}
]
}
]
}"#,
)
.unwrap();
let mut trt = Trait::new();
let mut fn_dec = FnDeclaration::new();
fn_dec.name = String::from("test");
let input = FnParameter::new("post", "string");
let output = FnParameter::new("hash", "string");
fn_dec.inputs.push(input);
fn_dec.outputs.push(output);
trt.functions.push(fn_dec);
assert_eq!(fixture, trt);
}
}