use crate::criterion::Criterion;
use crate::executor::{AGENT, HUMAN, RULE};
use serde_yaml::{Mapping, Value as Yaml};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Step {
pub name: String,
pub description: String,
pub executor: String,
pub criteria: Vec<Criterion>,
}
impl Step {
pub fn human(&self) -> bool {
self.executor == HUMAN
}
pub fn name(&self) -> String {
self.name.clone()
}
pub fn description(&self) -> String {
self.description.clone()
}
pub fn executor(&self) -> String {
self.executor.clone()
}
pub fn criteria(&self) -> Vec<Criterion> {
self.criteria.clone()
}
pub fn rules(&self) -> Vec<Criterion> {
self.of_kind(RULE)
}
pub fn agents(&self) -> Vec<Criterion> {
self.of_kind(AGENT)
}
pub fn gates(&self) -> Vec<Criterion> {
self.of_kind(HUMAN)
}
fn of_kind(&self, kind: &str) -> Vec<Criterion> {
self.criteria
.iter()
.filter(|item| item.executor() == kind)
.cloned()
.collect()
}
pub fn to_yaml(&self) -> Yaml {
let mut map = Mapping::new();
map.insert(Yaml::String("name".into()), Yaml::String(self.name.clone()));
if !self.description.is_empty() {
map.insert(
Yaml::String("description".into()),
Yaml::String(self.description.clone()),
);
}
map.insert(
Yaml::String("executor".into()),
Yaml::String(self.executor.clone()),
);
if !self.criteria.is_empty() {
map.insert(
Yaml::String("criteria".into()),
Yaml::Sequence(self.criteria.iter().map(Criterion::to_yaml).collect()),
);
}
Yaml::Mapping(map)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Workflow {
pub name: String,
pub description: String,
pub steps: Vec<Step>,
}
impl Workflow {
pub fn description(&self) -> String {
self.description.clone()
}
pub fn steps(&self) -> Vec<Step> {
self.steps.clone()
}
pub fn step_names(&self) -> Vec<String> {
self.steps.iter().map(|step| step.name.clone()).collect()
}
pub fn step(&self, name: &str) -> Option<Step> {
self.steps.iter().find(|step| step.name == name).cloned()
}
pub fn to_yaml(&self) -> Yaml {
let mut map = Mapping::new();
map.insert(Yaml::String("name".into()), Yaml::String(self.name.clone()));
if !self.description.is_empty() {
map.insert(
Yaml::String("description".into()),
Yaml::String(self.description.clone()),
);
}
map.insert(
Yaml::String("steps".into()),
Yaml::Sequence(self.steps.iter().map(Step::to_yaml).collect()),
);
Yaml::Mapping(map)
}
}