dex_sdk/flow.rs
1// Copyright (c) 2026 Super Durable, Inc.
2//
3// Licensed under the Super Durable Source License 1.0.
4// You may not use this file except in compliance with the License.
5// See the LICENSE file in the repository root.
6//
7// SPDX-License-Identifier: LicenseRef-Super-Durable-1.0
8
9use crate::{
10 Context, HandlerResult, PersistenceSchema, RpcList, StepDecision, StepList, Value,
11 short_type_name,
12};
13
14/// Function registered by a [`Flow`] to handle expiration of its soft timeout.
15pub type FlowTimeoutHandler<SomeFlow> = fn(&SomeFlow, &mut Context) -> HandlerResult<StepDecision>;
16
17/// Defines a durable application Flow and its registered API surface.
18///
19/// A Flow owns a typed start input, Step list, persistence schema, and RPC list. Implementations are
20/// registered as owned values in [`crate::Registry`] and shared safely across Worker invocations.
21/// Keep [`Self::flow_type`] and all subordinate names stable while any execution is running.
22///
23/// # Examples
24///
25/// ```
26/// use dex_sdk::{Flow, StepList};
27///
28/// struct OrderFlow;
29///
30/// impl Flow for OrderFlow {
31/// type StartInput = String;
32///
33/// fn steps(&self) -> StepList<'_, Self::StartInput> {
34/// StepList::empty()
35/// }
36/// }
37/// ```
38pub trait Flow: Send + Sync + 'static {
39 /// Value accepted by [`crate::Client::start_flow`] and the starting Step.
40 type StartInput: Value;
41
42 /// Returns the stable Flow type sent to Dex.
43 ///
44 /// The default is the final Rust type-name segment.
45 fn flow_type(&self) -> &'static str {
46 short_type_name::<Self>()
47 }
48
49 /// Returns the starting Step and all other Step definitions.
50 ///
51 /// The default is an empty list, which is valid only for Flows driven entirely by RPCs.
52 fn steps(&self) -> StepList<'_, Self::StartInput> {
53 StepList::empty()
54 }
55
56 /// Declares every Attribute and Channel used by this Flow.
57 ///
58 /// The default schema is empty.
59 fn persistence(&self) -> PersistenceSchema {
60 PersistenceSchema::new()
61 }
62
63 /// Returns the optional handler invoked after this Flow's durable timeout timer expires.
64 ///
65 /// Returning `Some` makes a positive timeout default to [`crate::FlowTimeoutPolicy::Handler`].
66 /// The handler runs at most once and its decision uses normal Step Execute validation.
67 fn timeout_handler(&self) -> Option<FlowTimeoutHandler<Self>>
68 where
69 Self: Sized,
70 {
71 None
72 }
73
74 /// Binds public RPC names to methods on this Flow implementation.
75 ///
76 /// The default list is empty.
77 fn rpcs(&self) -> RpcList<Self>
78 where
79 Self: Sized,
80 {
81 RpcList::new()
82 }
83}