Skip to main content

temporalio_common/
workflow_definition.rs

1use crate::data_converters::{RawValue, TemporalDeserializable, TemporalSerializable};
2
3/// Implement on a marker struct to define a workflow.
4///
5/// Typically, you will want to use the `#[workflow]` and `#[workflow_methods]` macros to define
6/// workflows. However, this trait may be implemented manually if desired.
7pub trait WorkflowDefinition {
8    /// Type of the input argument to the workflow
9    type Input: TemporalDeserializable + TemporalSerializable + 'static;
10    /// Type of the output of the workflow
11    type Output: TemporalDeserializable + TemporalSerializable + 'static;
12    /// The workflow type name
13    fn name(&self) -> &str;
14}
15
16/// Indicates that a type is associated with a [`WorkflowDefinition`], enabling typed
17/// signal, query, and update methods on `WorkflowHandle`.
18///
19/// Structs annotated with `#[workflow_methods]` implement this trait automatically.
20pub trait HasWorkflowDefinition: WorkflowDefinition {
21    /// The [`WorkflowDefinition`] type that [`SignalDefinition`], [`QueryDefinition`], and
22    /// [`UpdateDefinition`] are associated with.
23    type Run: WorkflowDefinition;
24}
25
26/// Marker type for untyped workflow handles. Stores the workflow type name. Uses [`RawValue`]
27/// for both input and output.
28pub struct UntypedWorkflow {
29    name: String,
30}
31impl UntypedWorkflow {
32    /// Create a new `UntypedWorkflow` with the given workflow type name.
33    pub fn new(name: impl Into<String>) -> Self {
34        Self { name: name.into() }
35    }
36}
37impl WorkflowDefinition for UntypedWorkflow {
38    type Input = RawValue;
39    type Output = RawValue;
40    fn name(&self) -> &str {
41        &self.name
42    }
43}
44
45impl HasWorkflowDefinition for UntypedWorkflow {
46    type Run = Self;
47}
48
49/// Implement on a marker struct to define a query.
50///
51/// Typically, you will want to use the `#[query]` attribute inside a `#[workflow_methods]` macro
52/// to define updates. However, this trait may be implemented manually if desired.
53pub trait QueryDefinition {
54    /// The workflow type this query belongs to
55    type Workflow: WorkflowDefinition;
56    /// Type of the input argument to the query.
57    type Input: TemporalDeserializable + TemporalSerializable + 'static;
58    /// Type of the output of the query.
59    type Output: TemporalDeserializable + TemporalSerializable + 'static;
60
61    /// The workflow type name.
62    fn name(&self) -> &str;
63}
64
65/// Implement on a marker struct to define a signal.
66///
67/// Typically, you will want to use the `#[signal]` attribute inside a `#[workflow_methods]` macro
68/// to define signals. However, this trait may be implemented manually if desired.
69pub trait SignalDefinition {
70    /// The workflow type this signal belongs to
71    type Workflow: WorkflowDefinition;
72    /// Type of the input argument to the signal.
73    type Input: TemporalDeserializable + TemporalSerializable + 'static;
74
75    /// The workflow type name.
76    fn name(&self) -> &str;
77}
78
79/// Implement on a marker struct to define an update.
80///
81/// Typically, you will want to use the `#[update]` attribute inside a `#[workflow_methods]` macro
82/// to define updates. However, this trait may be implemented manually if desired.
83pub trait UpdateDefinition {
84    /// The workflow type this update belongs to
85    type Workflow: WorkflowDefinition;
86    /// Type of the input argument to the update.
87    type Input: TemporalDeserializable + TemporalSerializable + 'static;
88    /// Type of the output of the update.
89    type Output: TemporalDeserializable + TemporalSerializable + 'static;
90
91    /// The workflow type name.
92    fn name(&self) -> &str;
93}