crazy_train/step.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
//! This module defines the `StepTrait` trait, which outlines the necessary methods that
//! any step in the execution process must implement. It also defines the `Plan` struct,
//! which encapsulates a command to be executed as part of a step.
//!
use crate::{
errors,
executer::{self, Output},
randomizer::Randomizer,
};
/// Enum representing the different types of steps that can be executed.
#[derive(Debug)]
pub enum Kind {
Setup,
Plan,
Check,
Test,
}
/// A trait that defines the behavior required for steps in the execution process.
#[allow(clippy::module_name_repetitions)]
pub trait StepTrait {
/// Prepares the setup by creating necessary directories and performing initialization steps.
///
/// # Errors
///
/// Returns an error if the setup fails, such as when it is unable to create the required directory.
fn setup(&self) -> errors::Result<()> {
Ok(())
}
/// Generates a plan for execution.
///
/// # Errors
///
/// when could not prepare the plan
fn plan(&self, randomizer: &Randomizer) -> errors::Result<Plan>;
/// Determines if the execution result indicates success for this step.
///
/// the bool result point if the runner should continue to the next steps or not.
///
/// # Errors
/// When plan result parsing is not the expected behavior.
fn is_success(&self, execution_result: &Output) -> Result<bool, &'static str>;
/// Optionally returns a command to run as a check after the execution of the plan.
fn run_check(&self) -> Option<String> {
None
}
/// Optionally returns a command to run as a test after the execution of the plan.
fn run_test(&self) -> Option<String> {
None
}
/// Serializes the step to a YAML representation.
fn to_yaml(&self) -> serde_yaml::Value;
}
/// A struct that represents a plan for executing a command as part of a step.
#[derive(Debug, Clone)]
pub struct Plan {
pub id: String,
pub command: String,
}
impl Plan {
/// Executes the command defined in the plan.
///
/// # Errors
///
/// on shell command failure.
pub fn execute(&self) -> errors::Result<executer::Output> {
executer::run_sh(&self.command)
}
}