temporalio_workflow/workflow_context/
view.rs1use std::time::{Duration, SystemTime};
2
3use temporalio_common_wasm::{
4 Memo, Priority, RetryPolicy, WorkflowExecution,
5 data_converters::{PayloadConverter, SerializationContextData},
6 protos::coresdk::{
7 common::NamespacedWorkflowExecution, workflow_activation::InitializeWorkflow,
8 },
9 search_attributes::SearchAttributes,
10};
11
12#[derive(Clone, Debug)]
16#[non_exhaustive]
17pub struct WorkflowContextView {
18 raw: InitializeWorkflow,
19 namespace: String,
20 task_queue: String,
21 run_id: String,
22 payload_converter: PayloadConverter,
23}
24
25impl WorkflowContextView {
26 pub(crate) fn new(
28 namespace: String,
29 task_queue: String,
30 run_id: String,
31 raw: InitializeWorkflow,
32 payload_converter: PayloadConverter,
33 ) -> Self {
34 Self {
35 raw,
36 namespace,
37 task_queue,
38 run_id,
39 payload_converter,
40 }
41 }
42
43 pub(super) fn into_parts(self) -> (String, String, String, InitializeWorkflow) {
44 (self.namespace, self.task_queue, self.run_id, self.raw)
45 }
46
47 pub fn workflow_id(&self) -> &str {
49 &self.raw.workflow_id
50 }
51
52 pub fn run_id(&self) -> &str {
54 &self.run_id
55 }
56
57 pub fn workflow_type(&self) -> &str {
59 &self.raw.workflow_type
60 }
61
62 pub fn task_queue(&self) -> &str {
64 &self.task_queue
65 }
66
67 pub fn namespace(&self) -> &str {
69 &self.namespace
70 }
71
72 pub fn attempt(&self) -> u32 {
74 self.raw.attempt as u32
75 }
76
77 pub fn first_execution_run_id(&self) -> &str {
79 &self.raw.first_execution_run_id
80 }
81
82 pub fn continued_from_run_id(&self) -> Option<&str> {
84 (!self.raw.continued_from_execution_run_id.is_empty())
85 .then_some(self.raw.continued_from_execution_run_id.as_str())
86 }
87
88 pub fn start_time(&self) -> Option<SystemTime> {
90 self.raw.start_time.and_then(|time| time.try_into().ok())
91 }
92
93 pub fn execution_timeout(&self) -> Option<Duration> {
95 self.raw
96 .workflow_execution_timeout
97 .and_then(|timeout| timeout.try_into().ok())
98 }
99
100 pub fn run_timeout(&self) -> Option<Duration> {
102 self.raw
103 .workflow_run_timeout
104 .and_then(|timeout| timeout.try_into().ok())
105 }
106
107 pub fn task_timeout(&self) -> Option<Duration> {
109 self.raw
110 .workflow_task_timeout
111 .and_then(|timeout| timeout.try_into().ok())
112 }
113
114 pub fn parent(&self) -> Option<NamespacedWorkflowInfo> {
116 self.raw
117 .parent_workflow_info
118 .clone()
119 .map(NamespacedWorkflowInfo::from_raw)
120 }
121
122 pub fn root(&self) -> Option<WorkflowExecution> {
124 self.raw.root_workflow.clone().map(Into::into)
125 }
126
127 pub fn retry_policy(&self) -> Option<RetryPolicy> {
129 self.raw.retry_policy.clone().map(Into::into)
130 }
131
132 pub fn cron_schedule(&self) -> Option<&str> {
134 (!self.raw.cron_schedule.is_empty()).then_some(self.raw.cron_schedule.as_str())
135 }
136
137 pub fn priority(&self) -> Priority {
139 self.raw.priority.clone().unwrap_or_default().into()
140 }
141
142 pub fn memo(&self) -> Memo {
144 Memo::from_raw(
145 self.raw.memo.clone(),
146 self.payload_converter.clone(),
147 SerializationContextData::Workflow,
148 )
149 }
150
151 pub fn search_attributes(&self) -> Option<SearchAttributes> {
153 self.raw
154 .search_attributes
155 .as_ref()
156 .map(SearchAttributes::from_proto)
157 }
158
159 pub fn raw(&self) -> &InitializeWorkflow {
161 &self.raw
162 }
163
164 pub fn into_raw(self) -> InitializeWorkflow {
166 self.raw
167 }
168}
169
170#[derive(Clone, Debug)]
172#[non_exhaustive]
173pub struct NamespacedWorkflowInfo {
174 raw: NamespacedWorkflowExecution,
175}
176
177impl NamespacedWorkflowInfo {
178 fn from_raw(raw: NamespacedWorkflowExecution) -> Self {
179 Self { raw }
180 }
181
182 pub fn workflow_id(&self) -> &str {
184 &self.raw.workflow_id
185 }
186
187 pub fn run_id(&self) -> &str {
189 &self.raw.run_id
190 }
191
192 pub fn namespace(&self) -> &str {
194 &self.raw.namespace
195 }
196
197 pub fn raw(&self) -> &NamespacedWorkflowExecution {
199 &self.raw
200 }
201
202 pub fn into_raw(self) -> NamespacedWorkflowExecution {
204 self.raw
205 }
206}