Skip to content

Commit

Permalink
models: Store extra model representation fields (related to concrete …
Browse files Browse the repository at this point in the history
…model) as YAML value
  • Loading branch information
kirushyk committed Apr 2, 2021
1 parent 6367128 commit e8d89d9
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/models/exclusive_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ impl AsModel for ExclusiveGateway {
"ExclusiveGateway"
}

fn serialize(&self) -> serde_json::Value {
serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
fn serialize(&self) -> serde_yaml::Value {
serde_yaml::to_value(self).unwrap_or(serde_yaml::Value::Null)
}

fn status(&self) -> String {
Expand Down
4 changes: 2 additions & 2 deletions src/models/gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ impl AsModel for Gate {
"Gate"
}

fn serialize(&self) -> serde_json::Value {
serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
fn serialize(&self) -> serde_yaml::Value {
serde_yaml::to_value(self).unwrap_or(serde_yaml::Value::Null)
}

fn status(&self) -> String {
Expand Down
4 changes: 2 additions & 2 deletions src/models/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ impl AsModel for Generator {
"Generator"
}

fn serialize(&self) -> serde_json::Value {
serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
fn serialize(&self) -> serde_yaml::Value {
serde_yaml::to_value(self).unwrap_or(serde_yaml::Value::Null)
}

fn status(&self) -> String {
Expand Down
4 changes: 2 additions & 2 deletions src/models/load_balancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ impl AsModel for LoadBalancer {
"LoadBalancer"
}

fn serialize(&self) -> serde_json::Value {
serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
fn serialize(&self) -> serde_yaml::Value {
serde_yaml::to_value(self).unwrap_or(serde_yaml::Value::Null)
}

fn status(&self) -> String {
Expand Down
2 changes: 1 addition & 1 deletion src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use self::processor::Processor;
pub use self::stochastic_gate::StochasticGate;
pub use self::storage::Storage;

pub use self::model_repr::ModelExtra;
pub use self::model_repr::ModelRepr;

#[derive(Debug, Clone)]
pub struct ModelMessage {
Expand Down
29 changes: 14 additions & 15 deletions src/models/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ impl Clone for Box<dyn AsModel> {

impl Serialize for Model {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let extra_fields: serde_json::Value = self.inner.serialize();
let extra_fields: serde_yaml::Value = self.inner.serialize();
let mut model = serializer.serialize_map(None)?;
model.serialize_entry("id", &self.id)?;
model.serialize_entry("type", self.inner.get_type())?;
if let serde_json::Value::Object(map) = extra_fields {
if let serde_yaml::Value::Mapping(map) = extra_fields {
for (key, value) in map.iter() {
model.serialize_entry(&key, &value)?;
}
Expand All @@ -61,44 +61,43 @@ impl Serialize for Model {

impl<'de> Deserialize<'de> for Model {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let model_extra = super::ModelExtra::deserialize(deserializer)?;
println!("New Model {:?}", model_extra);
let model_repr = super::ModelRepr::deserialize(deserializer)?;
const VARIANTS: &'static [&'static str] = &[
&"Generator", &"ExclusiveGateway", &"Processor", &"Storage"
];
match &model_extra.model_type[..] {
match &model_repr.model_type[..] {
"Generator" => {
let generator = serde_json::from_value::<super::Generator>(model_extra.extra).map_err(de::Error::custom)?;
let generator = serde_yaml::from_value::<super::Generator>(model_repr.extra).map_err(de::Error::custom)?;
let model = Model::new(
model_extra.id,
model_repr.id,
Box::new(generator)
);
Ok(model)
},
"ExclusiveGateway" => {
if let Ok(exclusive_gateway) = serde_json::from_value::<super::ExclusiveGateway>(model_extra.extra) {
if let Ok(exclusive_gateway) = serde_yaml::from_value::<super::ExclusiveGateway>(model_repr.extra) {
Ok(Model::new(
model_extra.id,
model_repr.id,
Box::new(exclusive_gateway)
))
} else {
Err(de::Error::invalid_value(Unexpected::Other("ExclusiveGateway"), &"ExclusiveGateway"))
}
},
"Processor" => {
if let Ok(processor) = serde_json::from_value::<super::Processor>(model_extra.extra) {
if let Ok(processor) = serde_yaml::from_value::<super::Processor>(model_repr.extra) {
Ok(Model::new(
model_extra.id,
model_repr.id,
Box::new(processor)
))
} else {
Err(de::Error::invalid_value(Unexpected::Other("Processor"), &"Processor"))
}
},
"Storage" => {
if let Ok(storage) = serde_json::from_value::<super::Storage>(model_extra.extra) {
if let Ok(storage) = serde_yaml::from_value::<super::Storage>(model_repr.extra) {
Ok(Model::new(
model_extra.id,
model_repr.id,
Box::new(storage)
))
} else {
Expand Down Expand Up @@ -151,8 +150,8 @@ pub trait AsModel: ModelClone {
fn get_type(&self) -> &'static str {
""
}
fn serialize(&self) -> serde_json::Value {
serde_json::Value::Null
fn serialize(&self) -> serde_yaml::Value {
serde_yaml::Value::Null
}
fn status(&self) -> String;
fn events_ext(
Expand Down
5 changes: 2 additions & 3 deletions src/models/model_repr.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use serde::{Serialize, Deserialize};
use serde_json::Value;

#[derive(Debug, Serialize, Deserialize)]
pub struct ModelExtra {
pub struct ModelRepr {
pub id: String,
#[serde(rename="type")]
pub model_type: String,
#[serde(flatten)]
pub extra: Value
pub extra: serde_yaml::Value
}
4 changes: 2 additions & 2 deletions src/models/parallel_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ impl AsModel for ParallelGateway {
"ParallelGateway"
}

fn serialize(&self) -> serde_json::Value {
serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
fn serialize(&self) -> serde_yaml::Value {
serde_yaml::to_value(self).unwrap_or(serde_yaml::Value::Null)
}

fn status(&self) -> String {
Expand Down
4 changes: 2 additions & 2 deletions src/models/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ impl AsModel for Processor {
"Processor"
}

fn serialize(&self) -> serde_json::Value {
serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
fn serialize(&self) -> serde_yaml::Value {
serde_yaml::to_value(self).unwrap_or(serde_yaml::Value::Null)
}

fn status(&self) -> String {
Expand Down
4 changes: 2 additions & 2 deletions src/models/stochastic_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ impl AsModel for StochasticGate {
"StochasticGate"
}

fn serialize(&self) -> serde_json::Value {
serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
fn serialize(&self) -> serde_yaml::Value {
serde_yaml::to_value(self).unwrap_or(serde_yaml::Value::Null)
}

fn status(&self) -> String {
Expand Down
4 changes: 2 additions & 2 deletions src/models/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ impl AsModel for Storage {
"Storage"
}

fn serialize(&self) -> serde_json::Value {
serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
fn serialize(&self) -> serde_yaml::Value {
serde_yaml::to_value(self).unwrap_or(serde_yaml::Value::Null)
}

fn status(&self) -> String {
Expand Down

0 comments on commit e8d89d9

Please sign in to comment.