use js_sys::Array;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
use crate::utils::set_panic_hook;
use super::Simulation as CoreSimulation;
#[wasm_bindgen]
#[derive(Default, Serialize, Deserialize)]
pub struct Simulation {
simulation: CoreSimulation,
}
#[wasm_bindgen]
impl Simulation {
pub fn post_json(models: &str, connectors: &str) -> Self {
set_panic_hook();
Self {
simulation: CoreSimulation::post(
serde_json::from_str(models).unwrap(),
serde_json::from_str(connectors).unwrap(),
),
}
}
pub fn put_json(&mut self, models: &str, connectors: &str) {
self.simulation.put(
serde_json::from_str(models).unwrap(),
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) -> Simulation {
set_panic_hook();
Self {
simulation: CoreSimulation::post(
serde_yaml::from_str(models).unwrap(),
serde_yaml::from_str(connectors).unwrap(),
),
}
}
pub fn put_yaml(&mut self, models: &str, connectors: &str) {
self.simulation.put(
serde_yaml::from_str(models).unwrap(),
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 get_status(&self, model_id: &str) -> String {
self.simulation.get_status(model_id).unwrap()
}
pub fn get_records_json(&self, model_id: &str) -> String {
serde_json::to_string(self.simulation.get_records(model_id).unwrap()).unwrap()
}
pub fn get_records_yaml(&self, model_id: &str) -> String {
serde_yaml::to_string(self.simulation.get_records(model_id).unwrap()).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()
}
}