use std::collections::HashMap;
use crate::{
errors,
executer::{self, Output},
randomizer::Randomizer,
};
#[derive(Debug)]
pub enum Kind {
Setup,
Plan,
Check,
Test,
}
#[allow(clippy::module_name_repetitions)]
pub trait StepTrait {
fn setup(&self) -> errors::Result<()> {
Ok(())
}
fn plan(&self, randomizer: &Randomizer) -> errors::Result<Plan>;
fn is_success(
&self,
execution_result: &Output,
plan_ctx: &PlanCtx,
) -> Result<bool, &'static str>;
fn run_check(&self) -> Option<String> {
None
}
fn run_test(&self) -> Option<String> {
None
}
fn to_yaml(&self) -> serde_yaml::Value;
}
#[derive(Debug, Clone)]
pub struct Plan {
pub id: String,
pub command: String,
pub ctx: PlanCtx,
}
#[derive(Default, Debug, Clone)]
pub struct PlanCtx {
pub vars: HashMap<String, String>,
}
impl Plan {
pub fn execute(&self) -> errors::Result<executer::Output> {
executer::run_sh(&self.command)
}
#[must_use]
pub fn new<T>(command: impl Into<String>) -> Self {
Self {
id: std::any::type_name::<T>().to_string(),
command: command.into(),
ctx: PlanCtx::default(),
}
}
#[must_use]
pub fn with_vars<T>(command: impl Into<String>, vars: HashMap<String, String>) -> Self {
Self {
id: std::any::type_name::<T>().to_string(),
command: command.into(),
ctx: PlanCtx { vars },
}
}
}