Skip to content

Commit

Permalink
model_factory: Decouple ExclusiveGateway, Processor and Storage deser…
Browse files Browse the repository at this point in the history
…ializers
  • Loading branch information
kirushyk committed Apr 4, 2021
1 parent 0c177b5 commit fa9af2c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
7 changes: 7 additions & 0 deletions src/models/exclusive_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ impl ExclusiveGateway {
}
}

pub fn from_value(value: serde_yaml::Value) -> Option<Box<dyn AsModel>> {
match serde_yaml::from_value::<Self>(value) {
Ok(exclusive_gateway) => Some(Box::new(exclusive_gateway)),
Err(_) => None
}
}

fn need_snapshot_metrics(&self) -> bool {
self.ports_in.snapshot.is_some() && self.ports_out.snapshot.is_some()
}
Expand Down
27 changes: 3 additions & 24 deletions src/models/model_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,16 @@ use std::collections::HashMap;

use lazy_static::lazy_static;

fn exclusive_gateway_new(value: serde_yaml::Value) -> Option<Box<dyn AsModel>> {
match serde_yaml::from_value::<super::ExclusiveGateway>(value) {
Ok(exclusive_gateway) => Some(Box::new(exclusive_gateway)),
Err(_) => None
}
}

fn processor_new(value: serde_yaml::Value) -> Option<Box<dyn AsModel>> {
match serde_yaml::from_value::<super::Processor>(value) {
Ok(processor) => Some(Box::new(processor)),
Err(_) => None
}
}

fn storage_new(value: serde_yaml::Value) -> Option<Box<dyn AsModel>> {
match serde_yaml::from_value::<super::Storage>(value) {
Ok(storage) => Some(Box::new(storage)),
Err(_) => None
}
}

use std::sync::Mutex;

pub type ModelConstructor = fn(serde_yaml::Value) -> Option<Box<dyn AsModel>>;
lazy_static! {
static ref CONSTRUCTORS: Mutex<HashMap<&'static str, ModelConstructor>> = {
let mut m = HashMap::new();
m.insert("Generator", super::Generator::from_value as ModelConstructor);
m.insert("ExclusiveGateway", exclusive_gateway_new as ModelConstructor);
m.insert("Processor", processor_new as ModelConstructor);
m.insert("Storage", storage_new as ModelConstructor);
m.insert("ExclusiveGateway", super::ExclusiveGateway::from_value as ModelConstructor);
m.insert("Processor", super::Processor::from_value as ModelConstructor);
m.insert("Storage", super::Storage::from_value as ModelConstructor);
Mutex::new(m)
};
static ref VARIANTS: Vec<&'static str> = {
Expand Down
7 changes: 7 additions & 0 deletions src/models/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ impl Processor {
}
}

pub fn from_value(value: serde_yaml::Value) -> Option<Box<dyn AsModel>> {
match serde_yaml::from_value::<Self>(value) {
Ok(processor) => Some(Box::new(processor)),
Err(_) => None
}
}

fn need_snapshot_metrics(&self) -> bool {
self.ports_in.snapshot.is_some() && self.ports_out.snapshot.is_some()
}
Expand Down
7 changes: 7 additions & 0 deletions src/models/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ impl Storage {
}
}

pub fn from_value(value: serde_yaml::Value) -> Option<Box<dyn AsModel>> {
match serde_yaml::from_value::<Self>(value) {
Ok(storage) => Some(Box::new(storage)),
Err(_) => None
}
}

fn need_snapshot_metrics(&self) -> bool {
self.ports_in.snapshot.is_some() && self.ports_out.snapshot.is_some()
}
Expand Down

0 comments on commit fa9af2c

Please sign in to comment.