Repository navigation
Expand file tree
/
Copy pathiteration.rs
More file actions
291 lines (247 loc) · 7.91 KB
/
Copy pathiteration.rs
File metadata and controls
291 lines (247 loc) · 7.91 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (c) 2022-2026 Super Durable, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Iteration: Arbitrary Cycles (WCP-10), Structured Loop (WCP-21), and
//! Recursion (WCP-22).
use dex_sdk::{
Context, Flow, HandlerError, HandlerResult, Step, StepDecision, StepList, SubFlow, Wait,
};
use serde::{Deserialize, Serialize};
use super::extend_trail;
/// How the author submits an article.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ArticleSubmission {
pub has_draft: bool,
pub should_abandon: bool,
}
/// The current revision and the Step types run so far.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ArticleRevision {
pub revision: u32,
pub should_abandon: bool,
pub trail: Vec<String>,
}
const FINAL_ARTICLE_REVISION: u32 = 1;
/// WCP-10 Arbitrary Cycles: the draft-edit cycle has two entry points and
/// two exit points, and GoTo may target a Step that already ran.
#[derive(Default)]
pub struct ArbitraryCyclesFlow {
route_submission: RouteSubmission,
draft_article: DraftArticle,
edit_article: EditArticle,
publish_article: PublishArticle,
abandon_article: AbandonArticle,
}
impl Flow for ArbitraryCyclesFlow {
type StartInput = ArticleSubmission;
fn steps(&self) -> StepList<'_, Self::StartInput> {
StepList::start(&self.route_submission)
.and(&self.draft_article)
.and(&self.edit_article)
.and(&self.publish_article)
.and(&self.abandon_article)
}
}
#[derive(Default)]
struct RouteSubmission;
impl Step for RouteSubmission {
type Input = ArticleSubmission;
fn execute(
&self,
_context: &mut Context,
submission: Self::Input,
) -> HandlerResult<StepDecision> {
let revision = ArticleRevision {
revision: 0,
should_abandon: submission.should_abandon,
trail: vec![self.step_type().to_string()],
};
if submission.has_draft {
return Ok(StepDecision::go_to(&EditArticle, revision));
}
Ok(StepDecision::go_to(&DraftArticle, revision))
}
}
#[derive(Default)]
struct DraftArticle;
impl Step for DraftArticle {
type Input = ArticleRevision;
fn execute(
&self,
_context: &mut Context,
revision: Self::Input,
) -> HandlerResult<StepDecision> {
let trail = extend_trail(revision.trail, self.step_type());
if revision.should_abandon && revision.revision >= FINAL_ARTICLE_REVISION {
return Ok(StepDecision::go_to(&AbandonArticle, trail));
}
Ok(StepDecision::go_to(
&EditArticle,
ArticleRevision { trail, ..revision },
))
}
}
#[derive(Default)]
struct EditArticle;
impl Step for EditArticle {
type Input = ArticleRevision;
fn execute(
&self,
_context: &mut Context,
revision: Self::Input,
) -> HandlerResult<StepDecision> {
let trail = extend_trail(revision.trail, self.step_type());
if revision.revision >= FINAL_ARTICLE_REVISION {
return Ok(StepDecision::go_to(&PublishArticle, trail));
}
Ok(StepDecision::go_to(
&DraftArticle,
ArticleRevision {
revision: revision.revision + 1,
should_abandon: revision.should_abandon,
trail,
},
))
}
}
#[derive(Default)]
struct PublishArticle;
impl Step for PublishArticle {
type Input = Vec<String>;
fn execute(&self, _context: &mut Context, trail: Self::Input) -> HandlerResult<StepDecision> {
Ok(StepDecision::graceful_complete(extend_trail(
trail,
self.step_type(),
)))
}
}
#[derive(Default)]
struct AbandonArticle;
impl Step for AbandonArticle {
type Input = Vec<String>;
fn execute(&self, _context: &mut Context, trail: Self::Input) -> HandlerResult<StepDecision> {
Ok(StepDecision::graceful_complete(extend_trail(
trail,
self.step_type(),
)))
}
}
/// The batches still to process and those already processed.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchProgress {
pub remaining_batches: u32,
pub processed_batches: Vec<String>,
}
/// WCP-21 Structured Loop: a pre-test Step moves to the loop body or the
/// exit, and the body moves back to the test.
#[derive(Default)]
pub struct StructuredLoopFlow {
check_batches: CheckBatches,
process_batch: ProcessBatch,
finish_batches: FinishBatches,
}
impl Flow for StructuredLoopFlow {
type StartInput = BatchProgress;
fn steps(&self) -> StepList<'_, Self::StartInput> {
StepList::start(&self.check_batches)
.and(&self.process_batch)
.and(&self.finish_batches)
}
}
#[derive(Default)]
struct CheckBatches;
impl Step for CheckBatches {
type Input = BatchProgress;
fn execute(
&self,
_context: &mut Context,
progress: Self::Input,
) -> HandlerResult<StepDecision> {
if progress.remaining_batches == 0 {
return Ok(StepDecision::go_to(
&FinishBatches,
progress.processed_batches,
));
}
Ok(StepDecision::go_to(&ProcessBatch, progress))
}
}
#[derive(Default)]
struct ProcessBatch;
impl Step for ProcessBatch {
type Input = BatchProgress;
fn execute(
&self,
_context: &mut Context,
mut progress: Self::Input,
) -> HandlerResult<StepDecision> {
progress
.processed_batches
.push(format!("batch-{}", progress.remaining_batches));
Ok(StepDecision::go_to(
&CheckBatches,
BatchProgress {
remaining_batches: progress.remaining_batches - 1,
processed_batches: progress.processed_batches,
},
))
}
}
#[derive(Default)]
struct FinishBatches;
impl Step for FinishBatches {
type Input = Vec<String>;
fn execute(
&self,
_context: &mut Context,
processed_batches: Self::Input,
) -> HandlerResult<StepDecision> {
Ok(StepDecision::graceful_complete(processed_batches))
}
}
/// WCP-22 Recursion: the Step waits for a SubFlow of its own Flow type.
#[derive(Default)]
pub struct FactorialRecursionFlow {
compute_factorial: ComputeFactorial,
}
impl Flow for FactorialRecursionFlow {
type StartInput = i64;
fn steps(&self) -> StepList<'_, Self::StartInput> {
StepList::start(&self.compute_factorial)
}
}
#[derive(Default)]
struct ComputeFactorial;
impl Step for ComputeFactorial {
type Input = i64;
fn wait_for(&self, _context: &mut Context, n: Self::Input) -> HandlerResult<Wait> {
if n <= 1 {
return Ok(Wait::skip_immediately());
}
let smaller_factorial = SubFlow::run(&FactorialRecursionFlow::default(), n - 1)
.map_err(HandlerError::from_error)?;
Ok(Wait::until(smaller_factorial))
}
fn execute(&self, context: &mut Context, n: Self::Input) -> HandlerResult<StepDecision> {
if n <= 1 {
return Ok(StepDecision::graceful_complete(1_i64));
}
let smaller_factorial: i64 = SubFlow::condition_result(context)?
.single_output()
.map_err(HandlerError::from_error)?;
Ok(StepDecision::graceful_complete(n * smaller_factorial))
}
}