use crate::{ModelBase, Variant, Vars, model::Step};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Branch {
#[serde(default)]
pub name: String,
#[serde(default)]
pub desc: String,
#[serde(default)]
pub id: String,
#[serde(default)]
pub vars: Vec<Variant>,
#[serde(default)]
pub run: Option<String>,
pub r#if: Option<String>,
#[serde(default)]
pub steps: Vec<Step>,
#[serde(default)]
pub next: Option<String>,
#[serde(default)]
pub r#else: bool,
#[serde(default)]
pub needs: Vec<String>,
#[serde(default)]
pub options: Vars,
#[serde(default)]
pub metadata: Vars,
#[serde(default)]
pub exposes: Vec<Variant>,
}
impl ModelBase for Branch {
fn id(&self) -> &str {
&self.id
}
}
impl Branch {
pub fn new() -> Self {
Default::default()
}
pub fn with_id(mut self, id: &str) -> Self {
self.id = id.to_string();
self
}
pub fn with_name(mut self, name: &str) -> Self {
self.name = name.to_string();
self
}
pub fn with_var<T>(mut self, name: &str, value: T) -> Self
where
T: Serialize + Clone,
{
self.vars.push(Variant::create(name, value));
self
}
pub fn vars(&self) -> Vars {
let mut ret = Vars::new();
self.vars.iter().for_each(|var| {
ret.set(&var.name, var.value.clone());
});
ret
}
pub fn with_option<T>(mut self, name: &str, value: T) -> Self
where
T: Serialize + Clone,
{
self.options.set(name, value);
self
}
pub fn with_expose(mut self, var: Variant) -> Self {
self.exposes.push(var);
self
}
pub fn exposes(&self) -> Vars {
let mut vars = Vars::new();
self.exposes.iter().for_each(|var| {
vars.set(&var.name, var.value.clone());
});
vars
}
pub fn with_next(mut self, next: &str) -> Self {
self.next = Some(next.to_string());
self
}
pub fn with_else(mut self, default: bool) -> Self {
self.r#else = default;
self
}
pub fn with_if(mut self, r#if: &str) -> Self {
self.r#if = Some(r#if.to_string());
self
}
pub fn with_run(mut self, run: &str) -> Self {
self.run = Some(run.to_string());
self
}
pub fn with_step(mut self, build: fn(Step) -> Step) -> Self {
let step = Step::default();
self.steps.push(build(step));
self
}
pub fn with_need(mut self, need: &str) -> Self {
self.needs.push(need.to_string());
self
}
pub fn with_metadata<T>(mut self, name: &str, value: T) -> Self
where
T: Serialize + Clone,
{
self.metadata.set(name, value);
self
}
}