Skip to content

Commit 058445f

Browse files
committed
Added type safe workflow defintion traits and functions
1 parent 9445ab3 commit 058445f

File tree

8 files changed

+1724
-1
lines changed

8 files changed

+1724
-1
lines changed

core/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ bimap = "0.6.1"
9595
clap = { version = "4.0", features = ["derive"] }
9696
criterion = "0.4"
9797
rstest = "0.17"
98+
serde = "1.0"
99+
serde_json = "1.0"
98100
temporal-sdk-core-test-utils = { path = "../test-utils" }
99101
temporal-sdk = { path = "../sdk" }
100102

sdk-core-protos/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,13 @@ pub mod coresdk {
12591259
}
12601260
}
12611261

1262+
/// Trait to map the Failure to Rust Error for idiomatic error handling in Workflow code <br/>
1263+
/// NOTE: This is *NOT* the generic
1264+
/// [failure converter](https://docs.temporal.io/dataconversion#failure-converter)
1265+
pub trait FromFailureExt: std::error::Error {
1266+
fn from_failure(failure: Failure) -> Self;
1267+
}
1268+
12621269
pub trait FromPayloadsExt {
12631270
fn from_payloads(p: Option<Payloads>) -> Self;
12641271
}

sdk/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ base64 = "0.21"
2020
crossbeam = "0.8"
2121
derive_more = "0.99"
2222
futures = "0.3"
23+
futures-core = "0.3"
2324
once_cell = "1.10"
2425
parking_lot = { version = "0.12", features = ["send_guard"] }
2526
prost-types = { version = "0.4", package = "prost-wkt-types" }

sdk/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ mod activity_context;
4848
mod app_data;
4949
pub mod interceptors;
5050
mod payload_converter;
51+
pub mod prelude;
52+
pub mod workflow;
5153
mod workflow_context;
5254
mod workflow_future;
5355

@@ -730,7 +732,7 @@ impl WorkflowFunction {
730732
pub type WorkflowResult<T> = Result<WfExitValue<T>, anyhow::Error>;
731733

732734
/// Workflow functions may return these values when exiting
733-
#[derive(Debug, derive_more::From)]
735+
#[derive(derive_more::From)]
734736
pub enum WfExitValue<T: Debug> {
735737
/// Continue the workflow as a new execution
736738
#[from(ignore)]

sdk/src/prelude.rs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//! Prelude for easy importing of required types when defining workflow and activity definitions
2+
3+
/// Registry prelude
4+
#[allow(unused_imports)]
5+
pub mod registry {
6+
pub use crate::workflow::{
7+
into_activity_0_args, into_activity_0_args_without_ctx, into_activity_1_args,
8+
into_activity_1_args_exit_value, into_activity_1_args_with_errors, into_activity_2_args,
9+
into_workflow_0_args, into_workflow_1_args, into_workflow_1_args_exit_value,
10+
into_workflow_1_args_with_errors, into_workflow_2_args,
11+
};
12+
}
13+
14+
/// Activity prelude
15+
#[allow(unused_imports)]
16+
pub mod activity {
17+
pub use crate::{ActContext, ActExitValue, ActivityCancelledError, NonRetryableActivityError};
18+
pub use serde::{Deserialize, Serialize};
19+
pub use temporal_sdk_core_protos::{
20+
coresdk::FromFailureExt, temporal::api::failure::v1::Failure,
21+
};
22+
}
23+
24+
/// Workflow prelude
25+
#[allow(unused_imports)]
26+
pub mod workflow {
27+
pub use crate::workflow::{
28+
execute_activity_0_args, execute_activity_0_args_without_ctx, execute_activity_1_args,
29+
execute_activity_1_args_exit_value, execute_activity_1_args_with_errors,
30+
execute_activity_2_args, execute_child_workflow_0_args_exit_value,
31+
execute_child_workflow_1_args, execute_child_workflow_2_args,
32+
execute_child_workflow_2_args_exit_value,
33+
};
34+
pub use crate::workflow::{AsyncFn0, AsyncFn1, AsyncFn2, AsyncFn3};
35+
pub use crate::{
36+
ActivityOptions, ChildWorkflowOptions, LocalActivityOptions, Signal, SignalData,
37+
SignalWorkflowOptions, WfContext, WfExitValue, WorkflowResult,
38+
};
39+
use futures::FutureExt;
40+
pub use serde::{Deserialize, Serialize};
41+
pub use std::{
42+
fmt::{Debug, Display},
43+
future::Future,
44+
time::Duration,
45+
};
46+
pub use temporal_sdk_core_protos::{
47+
coresdk::{
48+
activity_result::{self, activity_resolution},
49+
child_workflow::{child_workflow_result, Failure, Success},
50+
workflow_commands::ActivityCancellationType,
51+
AsJsonPayloadExt, FromFailureExt, FromJsonPayloadExt,
52+
},
53+
temporal::api::common::v1::Payload,
54+
};
55+
}

0 commit comments

Comments
 (0)