This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
schema.rs
167 lines (128 loc) · 3.44 KB
/
schema.rs
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use quote::quote;
use syn::{FnArg, PatType};
use crate::function::{func_attrs, has_ctor_attr, has_endpoint_attr, has_fundable_attr};
use crate::r#struct::has_storage_attr;
use crate::storage_vars;
use crate::{App, Function, Var};
#[derive(Debug)]
pub struct Schema {
exports: Vec<Export>,
storage: Vec<Var>,
}
#[derive(Debug)]
pub struct Export {
is_ctor: bool,
is_fundable: bool,
api_name: String,
wasm_name: String,
signature: Signature,
}
#[derive(Debug)]
pub struct Signature {
params: Vec<(String, String)>,
returns: Vec<String>,
}
impl Signature {
pub fn new() -> Self {
Self {
params: Vec::new(),
returns: Vec::new(),
}
}
pub fn add_param(&mut self, name: String, ty: String) {
self.params.push((name, ty));
}
pub fn add_return(&mut self, ty: String) {
self.returns.push(ty);
}
pub fn params(&self) -> &[(String, String)] {
&self.params
}
pub fn returns(&self) -> &[String] {
&self.returns
}
}
impl Schema {
pub fn new() -> Self {
Self {
exports: Vec::new(),
storage: Vec::new(),
}
}
pub fn add_export(&mut self, export: Export) {
self.exports.push(export);
}
pub fn endpoints(&self) -> Vec<&Export> {
self.exports
.iter()
.filter(|exp| exp.is_ctor == false)
.collect()
}
pub fn ctors(&self) -> Vec<&Export> {
self.exports.iter().filter(|exp| exp.is_ctor).collect()
}
pub fn exports(&self) -> &[Export] {
&self.exports
}
pub fn storage(&self) -> &[Var] {
&self.storage
}
}
pub fn app_schema(app: &App) -> Schema {
let storage = storage_schema(app);
let exports = app
.functions()
.iter()
.filter(|func| {
let attrs = func_attrs(func).unwrap();
let is_endpoint = has_endpoint_attr(&attrs);
let is_ctor = has_ctor_attr(&attrs);
is_endpoint || is_ctor
})
.map(export_schema)
.collect();
Schema { storage, exports }
}
fn storage_schema(app: &App) -> Vec<Var> {
let storage = app.structs().iter().find(|s| {
let attrs = s.attrs().as_ref().unwrap();
has_storage_attr(attrs)
});
if let Some(storage) = storage {
storage_vars(&storage).unwrap()
} else {
Vec::new()
}
}
fn export_schema(func: &Function) -> Export {
let attrs = func_attrs(func).unwrap();
let is_ctor = has_ctor_attr(&attrs);
let is_fundable = has_fundable_attr(&attrs);
let api_name = func.raw_name().to_string();
// TODO: future PR will uglify the name of the endpoint
// in order to save space in the transactions.
// The original (code) name will appear in the `schema.json` (off-chain).
let wasm_name = func.raw_name().to_string();
let signature = function_sig(func);
Export {
is_ctor,
is_fundable,
api_name,
wasm_name,
signature,
}
}
fn function_sig(func: &Function) -> Signature {
let raw_sig = func.raw_sig();
let mut sig = Signature::new();
for input in &raw_sig.inputs {
if let FnArg::Typed(PatType { pat, ty, .. }) = input {
let name = quote! { #pat };
let ty = quote! { #ty };
sig.add_param(name.to_string(), ty.to_string());
} else {
unreachable!()
}
}
sig
}