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
#![deny(missing_docs)]
use async_trait::async_trait;
use futures::{stream, Stream};
use std::{fmt, sync::Arc};
#[derive(Debug)]
pub struct Streamline<C, E, S>
where
S: State<Context = C, Error = E>,
{
context: Option<C>,
current: Progress<S, E, C>,
}
impl<C, E, S> Streamline<C, E, S>
where
S: State<Context = C, Error = E>,
{
pub fn build(state: S) -> Self {
Self {
context: None,
current: Progress::from(state),
}
}
pub fn context(mut self, context: C) -> Self {
self.context = Some(context);
self
}
pub fn run(self) -> impl Stream<Item = Progress<S, E, C>> {
stream::unfold(Some(self), Self::reduce)
}
async fn reduce(state_machine: Option<Self>) -> Option<(Progress<S, E, C>, Option<Self>)> {
if let Some(mut state_machine) = state_machine {
let context = state_machine.context.as_ref();
let next_state = match &state_machine.current {
Progress::Ok(inner) => match inner.next(context).await {
Ok(None) => None,
Ok(Some(next)) => Some(Progress::Ok(next)),
Err(source) => Some(Progress::Revert(RevertProgress::Reverting {
step: inner.clone(),
source: Arc::new(source),
})),
},
Progress::Revert(RevertProgress::Reverting { step, source }) => {
match step.revert(context).await {
Ok(None) => Some(Progress::Revert(RevertProgress::Reverted {
source: source.clone(),
})),
Ok(Some(next)) => Some(Progress::Revert(RevertProgress::Reverting {
step: next,
source: source.clone(),
})),
Err(error) => Some(Progress::Revert(RevertProgress::Failure {
source: source.clone(),
error,
})),
}
}
_ => None,
};
if let Some(next_state) = next_state {
let current = std::mem::replace(&mut state_machine.current, next_state);
Some((current, Some(state_machine)))
} else {
let current = std::mem::take(&mut state_machine.current);
Some((current, None))
}
} else {
None
}
}
}
#[async_trait(?Send)]
pub trait State: Clone + fmt::Debug + Default + PartialEq {
type Context;
type Error;
async fn next(&self, context: Option<&Self::Context>) -> Result<Option<Self>, Self::Error>;
async fn revert(&self, _context: Option<&Self::Context>) -> Result<Option<Self>, Self::Error> {
Ok(None)
}
}
#[derive(Debug, PartialEq)]
pub enum RevertProgress<S, E, C>
where
S: State<Context = C, Error = E>,
{
Reverting {
step: S,
source: Arc<E>,
},
Reverted {
source: Arc<E>,
},
Failure {
source: Arc<E>,
error: E,
},
}
#[derive(Debug, PartialEq)]
pub enum Progress<S, E, C>
where
S: State<Context = C, Error = E>,
{
Ok(S),
Revert(RevertProgress<S, E, C>),
}
impl<S, E, C> Default for Progress<S, E, C>
where
S: State<Context = C, Error = E>,
{
fn default() -> Self {
Self::Ok(S::default())
}
}
impl<S, E, C> From<S> for Progress<S, E, C>
where
S: State<Context = C, Error = E>,
{
fn from(state: S) -> Self {
Self::Ok(state)
}
}