Skip to main content

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 flow_type(&self) -> &'static str {
34///         "OrderFlow"
35///     }
36///
37///     fn steps(&self) -> StepList<'_, Self::StartInput> {
38///         StepList::empty()
39///     }
40/// }
41/// ```
42pub trait Flow: Send + Sync + 'static {
43    /// Value accepted by [`crate::Client::start_flow`] and the starting Step.
44    type StartInput: Value;
45
46    /// Returns the stable Flow type sent to Dex.
47    ///
48    /// The default is the final Rust type-name segment.
49    fn flow_type(&self) -> &'static str {
50        short_type_name::<Self>()
51    }
52
53    /// Returns the starting Step and all other Step definitions.
54    ///
55    /// The default is an empty list, which is valid only for Flows driven entirely by RPCs.
56    fn steps(&self) -> StepList<'_, Self::StartInput> {
57        StepList::empty()
58    }
59
60    /// Declares every Attribute and Channel used by this Flow.
61    ///
62    /// The default schema is empty.
63    fn persistence(&self) -> PersistenceSchema {
64        PersistenceSchema::new()
65    }
66
67    /// Returns the optional handler invoked after this Flow's durable timeout timer expires.
68    ///
69    /// Returning `Some` makes a positive timeout default to [`crate::FlowTimeoutPolicy::Handler`].
70    /// The handler runs at most once and its decision uses normal Step Execute validation.
71    fn timeout_handler(&self) -> Option<FlowTimeoutHandler<Self>>
72    where
73        Self: Sized,
74    {
75        None
76    }
77
78    /// Binds public RPC names to methods on this Flow implementation.
79    ///
80    /// The default list is empty.
81    fn rpcs(&self) -> RpcList<Self>
82    where
83        Self: Sized,
84    {
85        RpcList::new()
86    }
87}