Skip to content

Commit

Permalink
add plan ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
kaplanelad committed Oct 20, 2024
1 parent b69f724 commit c03996d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 23 deletions.
4 changes: 3 additions & 1 deletion examples/run.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crazy_train::{
step::{Plan, StepTrait},
step::{Plan, PlanCtx, StepTrait},
Result, StringDef,
};
use serde::{Deserialize, Serialize};
Expand All @@ -17,6 +17,7 @@ impl StepTrait for StepOne {
fn is_success(
&self,
execution_result: &crazy_train::executer::Output,
_plan_ctx: &PlanCtx,
) -> Result<bool, &'static str> {
if execution_result.status_code == Some(0) {
Ok(true)
Expand All @@ -43,6 +44,7 @@ impl StepTrait for StepTwo {
fn is_success(
&self,
execution_result: &crazy_train::executer::Output,
_plan_ctx: &PlanCtx,
) -> Result<bool, &'static str> {
if execution_result.status_code == Some(0) {
Err("expected failure command")
Expand Down
51 changes: 34 additions & 17 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ impl Runner {
"{}",
format!("Execute plan finished in {:?}", start.elapsed()).yellow()
);
let is_success = step.is_success(&result).map_err(|err| Error::StepError {
kind: step::Kind::Plan,
description: err.to_string(),
command_output: result,
})?;
let is_success =
step.is_success(&result, &step_plan.ctx)
.map_err(|err| Error::StepError {
kind: step::Kind::Plan,
description: err.to_string(),
command_output: result,
})?;

if !is_success {
continue;
Expand Down Expand Up @@ -159,6 +161,7 @@ mod tests {
use std::{collections::HashMap, path::PathBuf};

use serde::{Deserialize, Serialize};
use step::PlanCtx;

use super::*;
use crate::{executer::Output, generator::StringDef, step::Plan};
Expand All @@ -180,17 +183,28 @@ mod tests {

fn plan(&self, randomizer: &Randomizer) -> Result<Plan> {
let eco_string = randomizer.string(StringDef::default()).to_string();
Ok(Plan {
id: std::any::type_name::<Self>().to_string(),
command: format!(
Ok(Plan::with_vars::<Self>(
format!(
"echo {eco_string} >> {}",
self.location.join("test.txt").display()
),
ctx: Some(HashMap::from([("foo".to_string(), "bar".to_string())])),
})
HashMap::from([("foo".to_string(), "bar".to_string())]),
))
}

fn is_success(&self, execution_result: &Output) -> Result<bool, &'static str> {
fn is_success(
&self,
execution_result: &Output,
plan_ctx: &PlanCtx,
) -> Result<bool, &'static str> {
if let Some(foo_var) = plan_ctx.vars.get("foo") {
if foo_var != "bar" {
return Err("foo value should be equal to var");
}
} else {
return Err("foo plan ctx var not found");
};

if execution_result.status_code == Some(0) {
Ok(true)
} else {
Expand Down Expand Up @@ -224,17 +238,20 @@ mod tests {

fn plan(&self, randomizer: &Randomizer) -> Result<Plan> {
let eco_string = randomizer.string(StringDef::default()).to_string();
Ok(Plan {
id: std::any::type_name::<Self>().to_string(),
command: format!(
Ok(Plan::with_vars::<Self>(
format!(
"cat {eco_string} >> {}",
self.location.join("test.txt").display()
),
ctx: Some(HashMap::from([("foo".to_string(), "bar".to_string())])),
})
HashMap::from([("foo".to_string(), "bar".to_string())]),
))
}

fn is_success(&self, execution_result: &Output) -> Result<bool, &'static str> {
fn is_success(
&self,
execution_result: &Output,
_plan_ctx: &PlanCtx,
) -> Result<bool, &'static str> {
if execution_result.status_code == Some(1) {
Ok(true)
} else {
Expand Down
19 changes: 14 additions & 5 deletions src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ pub trait StepTrait {
///
/// # Errors
/// When plan result parsing is not the expected behavior.
fn is_success(&self, execution_result: &Output) -> Result<bool, &'static str>;
fn is_success(
&self,
execution_result: &Output,
plan_ctx: &PlanCtx,
) -> 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> {
Expand All @@ -65,7 +69,12 @@ pub trait StepTrait {
pub struct Plan {
pub id: String,
pub command: String,
pub ctx: Option<HashMap<String, String>>,
pub ctx: PlanCtx,
}

#[derive(Default, Debug, Clone)]
pub struct PlanCtx {
pub vars: HashMap<String, String>,
}

impl Plan {
Expand All @@ -83,16 +92,16 @@ impl Plan {
Self {
id: std::any::type_name::<T>().to_string(),
command: command.into(),
ctx: None,
ctx: PlanCtx::default(),
}
}

#[must_use]
pub fn with_ctx<T>(command: impl Into<String>, ctx: HashMap<String, String>) -> Self {
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: Some(ctx),
ctx: PlanCtx { vars },
}
}
}

0 comments on commit c03996d

Please sign in to comment.