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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use js_sys::Array;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
use crate::utils::set_panic_hook;
use super::Simulation;
#[wasm_bindgen]
#[derive(Default, Serialize, Deserialize)]
pub struct WebSimulation {
simulation: Simulation,
}
#[wasm_bindgen]
impl WebSimulation {
pub fn post_json(models: &str, connectors: &str) -> Self {
set_panic_hook();
Self {
simulation: Simulation {
models: serde_json::from_str(models).unwrap(),
connectors: serde_json::from_str(connectors).unwrap(),
..Simulation::default()
},
}
}
pub fn put_json(&mut self, models: &str, connectors: &str) {
self.simulation.models = serde_json::from_str(models).unwrap();
self.simulation.connectors = serde_json::from_str(connectors).unwrap();
}
pub fn get_json(&self) -> String {
serde_json::to_string_pretty(&self.simulation).unwrap()
}
pub fn post_yaml(models: &str, connectors: &str) -> WebSimulation {
set_panic_hook();
Self {
simulation: Simulation {
models: serde_yaml::from_str(models).unwrap(),
connectors: serde_yaml::from_str(connectors).unwrap(),
..Simulation::default()
},
}
}
pub fn put_yaml(&mut self, models: &str, connectors: &str) {
self.simulation.models = serde_yaml::from_str(models).unwrap();
self.simulation.connectors = serde_yaml::from_str(connectors).unwrap();
}
pub fn get_yaml(&self) -> String {
serde_yaml::to_string(&self.simulation).unwrap()
}
pub fn get_messages_js(&self) -> Array {
self.simulation
.get_messages()
.clone()
.into_iter()
.map(JsValue::from)
.collect()
}
pub fn get_messages_json(&self) -> String {
serde_json::to_string(&self.simulation.get_messages()).unwrap()
}
pub fn get_messages_yaml(&self) -> String {
serde_yaml::to_string(&self.simulation.get_messages()).unwrap()
}
pub fn get_global_time(&self) -> f64 {
self.simulation.get_global_time()
}
pub fn status(&self, model_id: &str) -> String {
self.simulation.status(model_id).unwrap()
}
pub fn reset(&mut self) {
self.simulation.reset();
}
pub fn reset_messages(&mut self) {
self.simulation.reset_messages();
}
pub fn reset_global_time(&mut self) {
self.simulation.reset_global_time();
}
pub fn inject_input_json(&mut self, message: &str) {
self.simulation
.inject_input(serde_json::from_str(message).unwrap());
}
pub fn inject_input_yaml(&mut self, message: &str) {
self.simulation
.inject_input(serde_yaml::from_str(message).unwrap());
}
pub fn step_js(&mut self) -> Array {
self.simulation
.step()
.unwrap()
.into_iter()
.map(JsValue::from)
.collect()
}
pub fn step_json(&mut self) -> String {
serde_json::to_string(&self.simulation.step().unwrap()).unwrap()
}
pub fn step_yaml(&mut self) -> String {
serde_yaml::to_string(&self.simulation.step().unwrap()).unwrap()
}
pub fn step_until_js(&mut self, until: f64) -> Array {
self.simulation
.step_until(until)
.unwrap()
.into_iter()
.map(JsValue::from)
.collect()
}
pub fn step_until_json(&mut self, until: f64) -> String {
serde_json::to_string(&self.simulation.step_until(until).unwrap()).unwrap()
}
pub fn step_until_yaml(&mut self, until: f64) -> String {
serde_yaml::to_string(&self.simulation.step_until(until).unwrap()).unwrap()
}
pub fn step_n_js(&mut self, n: usize) -> Array {
self.simulation
.step_n(n)
.unwrap()
.into_iter()
.map(JsValue::from)
.collect()
}
pub fn step_n_json(&mut self, n: usize) -> String {
serde_json::to_string(&self.simulation.step_n(n).unwrap()).unwrap()
}
pub fn step_n_yaml(&mut self, n: usize) -> String {
serde_yaml::to_string(&self.simulation.step_n(n).unwrap()).unwrap()
}
}