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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright (c) 2026 Super Durable, Inc.
//
// Licensed under the Sustainable Use License 1.0.
// You may not use this file except in compliance with the License.
// See the LICENSE file in the repository root.
//
// SPDX-License-Identifier: LicenseRef-Sustainable-Use-1.0
use std::marker::PhantomData;
use crate::{Condition, Context, HandlerResult, Value};
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct ChannelLoad {
pub(crate) name: String,
}
/// Selects one ChannelMap instance's pending messages for a handler invocation.
///
/// Create instance loads with [`ChannelMap::load_messages`], then attach them with
/// [`Rpc::load_channel_map_instance`](crate::Rpc::load_channel_map_instance).
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ChannelMapLoad {
pub(crate) name: String,
pub(crate) instance: Option<String>,
}
/// Identifies one typed value that is still pending in a Channel.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ChannelMessage<T> {
/// UUIDv7 assigned by Dex when the message was published.
pub message_id: String,
/// Decoded Channel value.
pub value: T,
}
/// Defines one durable FIFO queue of typed messages.
///
/// Add the Channel to [`crate::PersistenceSchema`]. Clients and handlers may publish messages;
/// Steps create [`Condition`] values that wait for queue-size bounds and read the messages consumed
/// by the satisfied condition.
///
/// # Examples
///
/// ```
/// use dex_sdk::{Channel, Wait};
///
/// let approvals = Channel::<String>::new("approvals");
/// let wait = Wait::until(approvals.for_one());
/// ```
pub struct Channel<T> {
name: String,
marker: PhantomData<fn() -> T>,
}
impl<T> Channel<T> {
/// Defines a Channel with a stable `name`.
///
/// Slash is prohibited because it is a reserved character.
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
marker: PhantomData,
}
}
/// Stages one message from the current Step or RPC invocation.
///
/// # Errors
///
/// Returns a [`crate::HandlerError`] when `value` cannot be encoded.
pub fn publish(&self, context: &mut Context, value: T) -> HandlerResult<()>
where
T: Value,
{
context.publish(self, value)
}
/// Stages best-effort deletion of one pending message from a handler.
pub fn delete(&self, context: &mut Context, message_id: &str) -> HandlerResult<()> {
context.delete_channel_message(self, message_id)
}
/// Returns the invocation snapshot's queued-message count.
pub fn size(&self, context: &Context) -> HandlerResult<usize> {
context.channel_size(self)
}
/// Returns loaded pending messages from the invocation snapshot in FIFO order.
///
/// # Errors
///
/// Returns a [`crate::HandlerError`] when this Channel was not selected by the handler options
/// or when decoding fails.
pub fn pending_messages(&self, context: &Context) -> HandlerResult<Vec<ChannelMessage<T>>>
where
T: Value,
{
context.pending_channel_messages(self)
}
/// Finds one pending message by its Dex-assigned ID.
///
/// Returns `Ok(None)` when the loaded snapshot does not contain `message_id`.
///
/// # Errors
///
/// Returns the same errors as [`Self::pending_messages`].
pub fn find_pending_message(
&self,
context: &Context,
message_id: &str,
) -> HandlerResult<Option<ChannelMessage<T>>>
where
T: Value,
{
Ok(self
.pending_messages(context)?
.into_iter()
.find(|message| message.message_id == message_id))
}
/// Decodes messages consumed by this Channel's satisfied condition.
///
/// # Errors
///
/// Returns a [`crate::HandlerError`] when a message cannot be decoded.
pub fn condition_results(&self, context: &Context) -> HandlerResult<Vec<T>>
where
T: Value,
{
context.channel_results(self)
}
/// Creates a condition that consumes exactly one queued message.
pub fn for_one(&self) -> Condition {
self.range(Some(1), Some(1))
}
/// Creates a condition that consumes exactly `count` queued messages.
pub fn for_n(&self, count: usize) -> Condition {
self.range(Some(count), Some(count))
}
/// Creates a condition satisfied when at least `count` messages are queued.
pub fn at_least(&self, count: usize) -> Condition {
self.range(Some(count), None)
}
/// Creates a non-blocking condition consuming up to `count` queued messages.
///
/// When its surrounding Wait completes, it consumes messages queued then. An empty queue yields
/// no messages.
pub fn at_most(&self, count: usize) -> Condition {
self.range(None, Some(count))
}
/// Creates a condition that waits for the lower bound, then consumes up to the upper bound.
///
/// Omitting the lower bound makes the condition complete immediately. Consumption uses only
/// messages queued when the condition completes.
pub fn range(&self, at_least: Option<usize>, at_most: Option<usize>) -> Condition {
Condition::channel(self.name.clone(), None, at_least, at_most)
}
/// Creates a guard used by conditional Flow completion to require an empty queue.
pub fn when_empty(&self) -> ChannelGuard {
ChannelGuard {
name: self.name.clone(),
instance: None,
}
}
pub(crate) fn name(&self) -> &str {
&self.name
}
}
impl<T> Clone for Channel<T> {
fn clone(&self) -> Self {
Self {
name: self.name.clone(),
marker: PhantomData,
}
}
}
/// Defines independently queued Channel instances under one name.
///
/// Supply an instance string for every publish, read, condition, and completion guard. Add the map
/// definition once to [`crate::PersistenceSchema`].
/// Slash is prohibited in instance keys because it is a reserved character.
pub struct ChannelMap<T> {
name: String,
marker: PhantomData<fn() -> T>,
}
impl<T> ChannelMap<T> {
/// Defines a Channel map with a stable `name`.
///
/// Slash is prohibited because it is a reserved character.
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
marker: PhantomData,
}
}
/// Selects one slash-free map `instance`'s pending messages for a handler Context.
pub fn load_messages(&self, instance: impl Into<String>) -> ChannelMapLoad {
ChannelMapLoad {
name: self.name.clone(),
instance: Some(instance.into()),
}
}
/// Stages one message for `instance` from the current invocation.
///
/// # Errors
///
/// Returns a [`crate::HandlerError`] when `value` cannot be encoded.
pub fn publish(&self, context: &mut Context, instance: &str, value: T) -> HandlerResult<()>
where
T: Value,
{
context.publish_map(self, instance, value)
}
/// Stages best-effort deletion of one pending message from a Channel-map instance.
pub fn delete(
&self,
context: &mut Context,
instance: &str,
message_id: &str,
) -> HandlerResult<()> {
context.delete_channel_map_message(self, instance, message_id)
}
/// Returns the invocation snapshot's message count for `instance`.
pub fn size(&self, context: &Context, instance: &str) -> HandlerResult<usize> {
context.channel_map_size(self, instance)
}
/// Returns one instance's loaded pending messages in FIFO order.
///
/// # Errors
///
/// Returns a [`crate::HandlerError`] when the instance was not selected by the handler options
/// or when a Value cannot be decoded.
pub fn pending_messages(
&self,
context: &Context,
instance: &str,
) -> HandlerResult<Vec<ChannelMessage<T>>>
where
T: Value,
{
context.pending_channel_map_messages(self, instance)
}
/// Finds one pending message by ID within one loaded map instance.
///
/// Returns `Ok(None)` when the loaded snapshot does not contain `message_id`.
///
/// # Errors
///
/// Returns the same errors as [`Self::pending_messages`].
pub fn find_pending_message(
&self,
context: &Context,
instance: &str,
message_id: &str,
) -> HandlerResult<Option<ChannelMessage<T>>>
where
T: Value,
{
Ok(self
.pending_messages(context, instance)?
.into_iter()
.find(|message| message.message_id == message_id))
}
/// Returns the number of non-empty instances visible to the current handler.
pub fn map_size(&self, context: &Context) -> HandlerResult<usize> {
Ok(self.all_instance_keys(context)?.len())
}
/// Returns sorted decoded non-empty keys, including buffered publishes.
pub fn all_instance_keys(&self, context: &Context) -> HandlerResult<Vec<String>> {
context.channel_map_keys(self)
}
/// Decodes messages consumed by `instance`'s satisfied condition.
///
/// # Errors
///
/// Returns a [`crate::HandlerError`] when a message cannot be decoded.
pub fn condition_results(&self, context: &Context, instance: &str) -> HandlerResult<Vec<T>>
where
T: Value,
{
context.channel_map_results(self, instance)
}
/// Creates an `instance` condition that consumes exactly one message.
pub fn for_one(&self, instance: &str) -> Condition {
self.range(instance, Some(1), Some(1))
}
/// Creates an `instance` condition that consumes exactly `count` messages.
pub fn for_n(&self, instance: &str, count: usize) -> Condition {
self.range(instance, Some(count), Some(count))
}
/// Creates an `instance` condition with an inclusive lower queue-size bound.
pub fn at_least(&self, instance: &str, count: usize) -> Condition {
self.range(instance, Some(count), None)
}
/// Creates a non-blocking `instance` condition consuming up to `count` queued messages.
///
/// When its surrounding Wait completes, it consumes messages queued then. An empty queue yields
/// no messages.
pub fn at_most(&self, instance: &str, count: usize) -> Condition {
self.range(instance, None, Some(count))
}
/// Creates an `instance` condition that waits for the lower bound, then consumes to its upper.
///
/// Omitting the lower bound makes the condition complete immediately. Consumption uses only
/// messages queued when the condition completes.
pub fn range(
&self,
instance: &str,
at_least: Option<usize>,
at_most: Option<usize>,
) -> Condition {
crate::registry::assert_map_instance(instance);
Condition::channel(
self.name.clone(),
Some(instance.to_string()),
at_least,
at_most,
)
}
/// Creates a conditional-completion guard requiring `instance` to be empty.
pub fn when_empty(&self, instance: &str) -> ChannelGuard {
crate::registry::assert_map_instance(instance);
ChannelGuard {
name: self.name.clone(),
instance: Some(instance.to_string()),
}
}
pub(crate) fn name(&self) -> &str {
&self.name
}
}
impl<T> Clone for ChannelMap<T> {
fn clone(&self) -> Self {
Self {
name: self.name.clone(),
marker: PhantomData,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
/// Identifies a Channel or Channel-map instance that must be empty before conditional completion.
pub struct ChannelGuard {
name: String,
instance: Option<String>,
}
impl ChannelGuard {
pub(crate) fn physical_name(&self) -> String {
match self.instance.as_deref() {
Some(instance) => crate::registry::physical_name(&self.name, instance),
None => self.name.clone(),
}
}
}