run/
run.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
use crazy_train::{
    step::{Plan, StepTrait},
    Result, StringDef,
};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct StepOne {}

impl StepTrait for StepOne {
    fn plan(&self, randomizer: &crazy_train::Randomizer) -> Result<crazy_train::step::Plan> {
        let eco_string = randomizer.string(StringDef::default()).to_string();
        Ok(Plan {
            id: std::any::type_name::<Self>().to_string(),
            command: format!("echo {eco_string}"),
        })
    }

    fn is_success(
        &self,
        execution_result: &crazy_train::executer::Output,
    ) -> Result<bool, &'static str> {
        if execution_result.status_code == Some(0) {
            Ok(true)
        } else {
            Err("status code should be 0")
        }
    }

    fn to_yaml(&self) -> serde_yaml::Value {
        serde_yaml::to_value(self).expect("serialize")
    }
}

#[derive(Serialize, Deserialize)]
struct StepTwo {}

impl StepTrait for StepTwo {
    fn plan(&self, randomizer: &crazy_train::Randomizer) -> Result<crazy_train::step::Plan> {
        let eco_string = randomizer.string(StringDef::default()).to_string();
        Ok(Plan {
            id: std::any::type_name::<Self>().to_string(),
            command: format!("unknown-command {eco_string}"),
        })
    }

    fn is_success(
        &self,
        execution_result: &crazy_train::executer::Output,
    ) -> Result<bool, &'static str> {
        if execution_result.status_code == Some(0) {
            Err("expected failure command")
        } else {
            Ok(true)
        }
    }

    fn to_yaml(&self) -> serde_yaml::Value {
        serde_yaml::to_value(self).expect("serialize")
    }
}

fn main() {
    let step_one = StepOne {};
    let step_two = StepTwo {};
    let runner = crazy_train::new(vec![Box::new(step_one), Box::new(step_two)]);

    let res = runner.run();
    println!("{res:#?}");
}