use serde_json::Map;
use std::{collections::BTreeMap, rc::Rc};
pub type Bag = Vec<Msg>;
pub type Mail = Vec<MailItem>;
pub type Value = serde_json::Value;
pub struct Outputs {
pub(crate) bag: Bag,
}
impl Outputs {
pub(crate) fn new() -> Self {
Outputs {
bag: Default::default(),
}
}
pub fn put(&mut self, port: &str, value: Value) {
self.bag.push(Msg {
port: port.to_owned(),
value: Rc::new(value),
});
}
}
#[derive(Debug, Clone)]
pub struct MailItem {
pub model_name: String,
pub y_bag: Bag,
}
#[derive(Debug, Clone)]
pub struct Msg {
pub(crate) port: String,
pub(crate) value: Rc<Value>,
}
impl Msg {
pub fn new(port: &str, value: Value) -> Self {
Self {
port: port.to_owned(),
value: Rc::new(value),
}
}
pub fn value(&self) -> &Value {
&*self.value
}
pub fn port(&self) -> &str {
&self.port
}
}
pub struct SimResult {
pub tags: Vec<String>,
pub result: Value,
}
pub type ModelSimResults = BTreeMap<String, SimResult>;
impl From<&MailItem> for Value {
fn from(mail: &MailItem) -> Self {
let msg_vec: Vec<Value> = mail.y_bag.iter().map(Value::from).collect();
let mut bag_map = Map::new();
bag_map.insert(mail.model_name.clone(), Value::Array(msg_vec));
Value::Object(bag_map)
}
}
impl From<&Msg> for Value {
fn from(msg: &Msg) -> Self {
let mut val_map = Map::new();
val_map.insert("PORT".to_owned(), Value::String(msg.port.to_owned()));
val_map.insert("VALUE".to_owned(), msg.value.as_ref().to_owned());
Value::Object(val_map)
}
}