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()
}
}