1use crazy_train::{
2 step::{Plan, PlanCtx, StepTrait},
3 Result, StringDef,
4};
5use serde::{Deserialize, Serialize};
6
7#[derive(Serialize, Deserialize)]
8struct StepOne {}
9
10impl StepTrait for StepOne {
11 fn plan(&self, randomizer: &crazy_train::Randomizer) -> Result<crazy_train::step::Plan> {
12 let eco_string = randomizer.string(StringDef::default()).to_string();
13
14 Ok(Plan::new::<Self>(format!("echo {eco_string}")))
15 }
16
17 fn is_success(
18 &self,
19 execution_result: &crazy_train::executer::Output,
20 _plan_ctx: &PlanCtx,
21 ) -> Result<bool, &'static str> {
22 if execution_result.status_code == Some(0) {
23 Ok(true)
24 } else {
25 Err("status code should be 0")
26 }
27 }
28
29 fn to_yaml(&self) -> serde_yaml::Value {
30 serde_yaml::to_value(self).expect("serialize")
31 }
32}
33
34#[derive(Serialize, Deserialize)]
35struct StepTwo {}
36
37impl StepTrait for StepTwo {
38 fn plan(&self, randomizer: &crazy_train::Randomizer) -> Result<crazy_train::step::Plan> {
39 let eco_string = randomizer.string(StringDef::default()).to_string();
40
41 Ok(Plan::new::<Self>(format!("unknown-command {eco_string}")))
42 }
43
44 fn is_success(
45 &self,
46 execution_result: &crazy_train::executer::Output,
47 _plan_ctx: &PlanCtx,
48 ) -> Result<bool, &'static str> {
49 if execution_result.status_code == Some(0) {
50 Err("expected failure command")
51 } else {
52 Ok(true)
53 }
54 }
55
56 fn to_yaml(&self) -> serde_yaml::Value {
57 serde_yaml::to_value(self).expect("serialize")
58 }
59}
60
61fn main() {
62 let step_one = StepOne {};
63 let step_two = StepTwo {};
64 let runner = crazy_train::new(vec![Box::new(step_one), Box::new(step_two)]);
65
66 let res = runner.run();
67 println!("{res:#?}");
68}