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
use ;
use Patch;
use Serialize;
use Effect;
use Error;
use IntoResult;
use crateIOError;
use crateView;
/// A type representing a lazy I/O operation producing a value of type `T` or an error of type `E`.
///
/// `IO<T, E>` represents an operation that:
/// - Accesses a mutable reference to state of type `T` via [`View<T>`]
/// - Can perform asynchronous I/O operations
/// - May fail with an error of type `E`
///
/// Internally, the IO type combines a "pure" computation, which is used during planning and an
/// effectful computation that will be used at runtime.
///
/// This type is primarily used as the return type for job handlers that need to:
/// 1. Modify system state
/// 2. Perform side effects (like network calls, file I/O, etc.)
/// 3. Handle potential errors
///
/// # Type Parameters
///
/// - `T`: The type of value being operated on in the system state
/// - `E`: The error type for I/O operations (defaults to [`Infallible`] for infallible operations)
///
/// # Example
///
/// ```
/// use mahler::{
/// extract::{View, Target},
/// task::{with_io, IO},
/// };
///
/// // A simple job that increments a counter
/// fn plus_one(
/// mut counter: View<i32>,
/// Target(target): Target<i32>
/// ) -> IO<i32> {
/// if *counter < target {
/// *counter += 1;
/// }
///
/// // Perform async I/O operation
/// with_io(counter, |counter| async move {
/// // Simulate some async work
/// tokio::time::sleep(std::time::Duration::from_millis(10)).await;
/// Ok(counter)
/// })
/// }
/// ```
;
/// Convert an IO operation into the internal effect representation.
///
/// This conversion allows IO operations to be executed by the workflow engine.
/// Any I/O errors are wrapped as [`IOError`] and the final result is converted
/// to a JSON patch representing the state changes.
/// Convert a [`View<T>`] directly into an IO operation.
///
/// This creates an IO operation that immediately succeeds with the given view,
/// without performing any actual I/O. This is useful for bailing out early in jobs
/// before creating the effectful computation.
///
/// # Examples
///
/// ```
/// # use mahler::{extract::{View, Target}, task::{with_io, IO}};
/// fn plus_one(mut view: View<u32>, Target(tgt): Target<u32>) -> IO<u32> {
/// if *view >= tgt {
/// // exit early if we already reached the target
/// return view.into();
/// }
///
/// with_io(view, |view| async {
/// // do some async work
/// Ok(view)
/// })
/// // increase the target after the IO operation
/// // terminates (at runtime)
/// .map(|mut counter| {
/// *counter = *counter + 1;
/// counter
/// })
/// }
/// ```
/// Creates an [`IO`] operation from a [`View`] and an asynchronous I/O function.
///
/// This function combines a pure state modification (via the `View`) with an
/// asynchronous I/O operation. The I/O function receives the view and must
/// return a `Future` that resolves to a `Result<View<T>, E>`.
///
/// This is the primary way to create IO operations that perform side effects
/// like network requests, file operations, or other async work.
///
/// # Parameters
///
/// - `pure`: The initial [`View<T>`] containing the state to operate on
/// - `io`: An async function that performs the I/O operation and returns the modified view
///
/// # Examples
///
/// ```
/// use mahler::{
/// extract::{View, Target},
/// task::{with_io, IO},
/// };
/// use std::time::Duration;
///
/// fn plus_one(
/// mut counter: View<i32>,
/// Target(target): Target<i32>
/// ) -> IO<i32, Box<dyn std::error::Error + Send + Sync>> {
/// if *counter < target {
/// *counter += 1;
/// }
///
/// // Perform async I/O (e.g., save to database, send notification, etc.)
/// with_io(counter, |counter| async move {
/// // Simulate async work
/// tokio::time::sleep(Duration::from_millis(100)).await;
///
/// // Could perform actual I/O here:
/// // - Database operations
/// // - HTTP requests
/// // - File I/O
/// // - etc.
///
/// Ok(counter)
/// })
/// }
/// ```