Skip to main content

temporalio_common_wasm/
activity_definition.rs

1//! Contains types for activity definitions, used by the code generated by the macros for defining
2//! activities, or directly by users targeting activities in other languages.
3
4use crate::{
5    data_converters::{TemporalDeserializable, TemporalSerializable},
6    error::{ApplicationFailure, FailurePayloads},
7};
8
9/// Implement on a marker struct to define an activity.
10///
11/// Typically, you will want to use the `#[activity]` attribute within an `#[activities]` macro to
12/// define activities. However, this trait may be implemented manually if desired.
13pub trait ActivityDefinition {
14    /// Type of the input argument to the workflow
15    type Input: TemporalDeserializable + TemporalSerializable + 'static;
16    /// Type of the output of the workflow
17    type Output: TemporalDeserializable + TemporalSerializable + 'static;
18
19    /// The name that will be used for the activity type.
20    fn name() -> &'static str
21    where
22        Self: Sized;
23}
24
25/// Returned as errors from activity functions.
26#[derive(Debug)]
27pub enum ActivityError {
28    /// Return this error to attach application-failure metadata to an activity failure.
29    Application(Box<ApplicationFailure>),
30    /// Return this error to indicate your activity is cancelling
31    Cancelled {
32        /// Optional cancellation details.
33        details: Option<FailurePayloads>,
34    },
35    /// Return this error to indicate that the activity will be completed outside of this activity
36    /// definition, by an external client.
37    WillCompleteAsync,
38}
39
40impl ActivityError {
41    /// Construct a cancelled error without details
42    pub fn cancelled() -> Self {
43        Self::Cancelled { details: None }
44    }
45
46    /// Construct a cancelled error with details that will be converted using the active data
47    /// converter.
48    pub fn cancelled_with_details<T>(details: T) -> Self
49    where
50        T: Into<FailurePayloads>,
51    {
52        Self::Cancelled {
53            details: Some(details.into()),
54        }
55    }
56
57    /// Construct an application activity error.
58    pub fn application(err: ApplicationFailure) -> Self {
59        Self::Application(err.into())
60    }
61}
62
63impl<E> From<E> for ActivityError
64where
65    E: Into<anyhow::Error>,
66{
67    fn from(source: E) -> Self {
68        match source.into().downcast::<ApplicationFailure>() {
69            Ok(application_failure) => Self::Application(Box::new(application_failure)),
70            Err(err) => Self::Application(ApplicationFailure::new(err).into()),
71        }
72    }
73}