acts_next/model/
workflow.rs1use crate::{Act, ActError, ModelBase, Result, Step, Vars, scheduler::NodeTree};
2use serde::{Deserialize, Serialize};
3use serde_json::Value as JsonValue;
4
5#[derive(Debug, Clone, Default, Serialize, Deserialize)]
6pub struct Workflow {
7 #[serde(default)]
8 pub id: String,
9
10 #[serde(default)]
11 pub name: String,
12
13 #[serde(default)]
14 pub desc: String,
15
16 #[serde(default)]
17 pub tag: String,
18
19 #[serde(default)]
20 pub steps: Vec<Step>,
21
22 #[serde(default)]
23 pub env: Vars,
24
25 #[serde(default)]
26 pub inputs: Vars,
27
28 #[serde(default)]
29 pub outputs: Vars,
30
31 #[serde(default)]
32 pub setup: Vec<Act>,
33
34 #[serde(default)]
35 pub on: Vec<Act>,
36
37 #[serde(default)]
38 pub ver: i32,
39}
40
41impl Workflow {
42 pub fn from_yml(s: &str) -> Result<Self> {
43 let workflow = serde_yaml::from_str::<Workflow>(s);
44 match workflow {
45 Ok(v) => Ok(v),
46 Err(e) => Err(ActError::Model(format!("{}", e))),
47 }
48 }
49
50 pub fn from_json(s: &str) -> Result<Self> {
51 let workflow = serde_json::from_str::<Workflow>(s);
52 match workflow {
53 Ok(v) => Ok(v),
54 Err(e) => Err(ActError::Model(format!("{}", e))),
55 }
56 }
57
58 pub fn set_env(&mut self, vars: &Vars) {
59 for (name, value) in vars {
60 self.env
61 .entry(name.clone())
62 .and_modify(|v| *v = value.clone())
63 .or_insert(value.clone());
64 }
65 }
66
67 pub fn set_inputs(&mut self, vars: &Vars) {
68 for (name, value) in vars {
69 self.inputs
70 .entry(name.clone())
71 .and_modify(|v| *v = value.clone())
72 .or_insert(value.clone());
73 }
74 }
75
76 pub fn print(&self) {
77 let mut root = NodeTree::new();
78 root.load(self).unwrap();
79 root.print();
80 }
81
82 pub fn tree_output(&self) -> String {
83 let mut root = NodeTree::new();
84 root.load(self).unwrap();
85 root.tree_output()
86 }
87
88 pub fn step(&self, id: &str) -> Option<&Step> {
89 match self.steps.iter().find(|s| s.id == id) {
90 Some(s) => Some(s),
91 None => None,
92 }
93 }
94 pub fn set_id(&mut self, id: &str) {
95 self.id = id.to_string();
96 }
97
98 pub fn set_ver(&mut self, ver: i32) {
99 self.ver = ver;
100 }
101
102 pub fn to_yml(&self) -> Result<String> {
103 match serde_yaml::to_string(self) {
104 Ok(s) => Ok(s),
105 Err(e) => Err(ActError::Model(e.to_string())),
106 }
107 }
108
109 pub fn to_json(&self) -> Result<String> {
110 match serde_json::to_string(self) {
111 Ok(s) => Ok(s),
112 Err(e) => Err(ActError::Model(e.to_string())),
113 }
114 }
115
116 pub fn valid(&self) -> Result<()> {
117 let mut root = NodeTree::new();
118 root.load(self)?;
119 Ok(())
120 }
121}
122
123impl ModelBase for Workflow {
124 fn id(&self) -> &str {
125 &self.id
126 }
127}
128
129impl Workflow {
131 pub fn new() -> Self {
132 Default::default()
133 }
134
135 pub fn with_id(mut self, id: &str) -> Self {
136 self.id = id.to_string();
137 self
138 }
139
140 pub fn with_name(mut self, name: &str) -> Self {
141 self.name = name.to_string();
142 self
143 }
144
145 pub fn with_tag(mut self, tag: &str) -> Self {
146 self.tag = tag.to_string();
147 self
148 }
149
150 pub fn with_input(mut self, name: &str, value: JsonValue) -> Self {
151 self.inputs.insert(name.to_string(), value);
152 self
153 }
154
155 pub fn with_env(mut self, name: &str, value: JsonValue) -> Self {
156 self.env.insert(name.to_string(), value);
157 self
158 }
159
160 pub fn with_output(mut self, name: &str, value: JsonValue) -> Self {
161 self.outputs.insert(name.to_string(), value);
162 self
163 }
164
165 pub fn with_step(mut self, build: fn(Step) -> Step) -> Self {
166 let step = Step::default();
167 self.steps.push(build(step));
168 self
169 }
170
171 pub fn with_setup(mut self, build: fn(Vec<Act>) -> Vec<Act>) -> Self {
172 let stmts = Vec::new();
173 self.setup = build(stmts);
174 self
175 }
176
177 pub fn with_on(mut self, build: fn(Act) -> Act) -> Self {
178 let act = build(Act::default());
179 self.on.push(act);
180 self
181 }
182}