1use crate::base::{Error, WorkflowIx};
4use serde::ser::SerializeStruct;
5use serde::{Serialize, Serializer};
6
7#[derive(Debug)]
9#[non_exhaustive]
10pub enum RunStatus {
11 Completed,
13 CompletedWithErrors,
15 Stopped(Error),
21 TimedOut,
25 Cancelled,
29}
30
31#[derive(Debug)]
33#[non_exhaustive]
34pub enum Selection {
35 Ran,
37 SkippedByCondition,
39 ConditionFailed(Error),
41 SkippedByRollout,
43}
44
45#[derive(Clone, Copy, Debug, PartialEq, Eq)]
47#[non_exhaustive]
48pub enum WorkflowExit {
49 Completed,
51 Halted,
53 Failed,
55 Stopped,
57}
58
59#[derive(Debug)]
61#[non_exhaustive]
62pub struct WorkflowRecord<'r> {
63 pub id: &'r str,
65 pub index: WorkflowIx,
67 pub selection: &'r Selection,
69 pub sweeps: u32,
72 pub exit: Option<WorkflowExit>,
74}
75
76impl Serialize for RunStatus {
81 fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
82 s.serialize_str(match self {
83 Self::Completed => "completed",
84 Self::CompletedWithErrors => "completed_with_errors",
85 Self::Stopped(_) => "stopped",
86 Self::TimedOut => "timed_out",
87 Self::Cancelled => "cancelled",
88 })
89 }
90}
91
92impl Serialize for Selection {
97 fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
98 s.serialize_str(match self {
99 Self::Ran => "ran",
100 Self::SkippedByCondition => "skipped_by_condition",
101 Self::ConditionFailed(_) => "condition_failed",
102 Self::SkippedByRollout => "skipped_by_rollout",
103 })
104 }
105}
106
107impl Serialize for WorkflowExit {
110 fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
111 s.serialize_str(match self {
112 Self::Completed => "completed",
113 Self::Halted => "halted",
114 Self::Failed => "failed",
115 Self::Stopped => "stopped",
116 })
117 }
118}
119
120impl Serialize for WorkflowRecord<'_> {
124 fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
125 let error = match self.selection {
126 Selection::ConditionFailed(e) => Some(e),
127 _ => None,
128 };
129 let mut o = s.serialize_struct("WorkflowRecord", 6)?;
130 o.serialize_field("id", self.id)?;
131 o.serialize_field("index", &self.index)?;
132 o.serialize_field("selection", self.selection)?;
133 match error {
134 Some(e) => o.serialize_field("error", e)?,
135 None => o.skip_field("error")?,
136 }
137 o.serialize_field("sweeps", &self.sweeps)?;
138 o.serialize_field("exit", &self.exit)?;
139 o.end()
140 }
141}
142
143#[cfg(feature = "ts")]
144crate::base::ts::ts_decl!(
145 RunStatus,
146 "RunStatus",
147 [" How a run ended: a stopped run's error is its `error`."],
148 |_cfg| crate::base::ts::literals([
149 "completed",
150 "completed_with_errors",
151 "stopped",
152 "timed_out",
153 "cancelled",
154 ])
155);
156
157#[cfg(feature = "ts")]
158crate::base::ts::ts_decl!(
159 Selection,
160 "Selection",
161 [
162 " Why a workflow did or did not run: a failed condition's error is its",
163 " record's `error`.",
164 ],
165 |_cfg| crate::base::ts::literals([
166 "ran",
167 "skipped_by_condition",
168 "condition_failed",
169 "skipped_by_rollout",
170 ])
171);
172
173#[cfg(feature = "ts")]
174crate::base::ts::ts_decl!(
175 WorkflowExit,
176 "WorkflowExit",
177 [" How a workflow that ran ended."],
178 |_cfg| crate::base::ts::literals(["completed", "halted", "failed", "stopped"])
179);
180
181#[cfg(feature = "ts")]
182crate::base::ts::ts_decl!(
183 WorkflowRecord<'_>,
184 "WorkflowRecord",
185 [" One workflow's part in a run."],
186 |cfg| {
187 use crate::base::ts::{Prop, object};
188 object(&[
189 Prop::new("id", "string", &[" The workflow's id."]),
190 Prop::new(
191 "index",
192 WorkflowIx::name(cfg),
193 &[" The workflow's position in the program."],
194 ),
195 Prop::new(
196 "selection",
197 Selection::name(cfg),
198 &[" Whether it was selected."],
199 ),
200 Prop::optional(
201 "error",
202 Error::name(cfg),
203 &[
204 " Why its condition failed to evaluate; present only when `selection` is",
205 " `condition_failed`.",
206 ],
207 ),
208 Prop::new(
209 "sweeps",
210 "number",
211 &[
212 " The sweeps begun: one for a workflow without a loop that ran, and for",
213 " a loop each sweep its condition admitted.",
214 ],
215 ),
216 Prop::new(
217 "exit",
218 Option::<WorkflowExit>::name(cfg),
219 &[" How it ended; `null` when it did not run."],
220 ),
221 ])
222 }
223);