temporalio-common-wasm 0.5.0

WASM-safe shared functionality for the Temporal Rust workflow surface
Documentation
//! Contains types for activity definitions, used by the code generated by the macros for defining
//! activities, or directly by users targeting activities in other languages.

use crate::{
    data_converters::{TemporalDeserializable, TemporalSerializable},
    error::{ApplicationFailure, FailurePayloads},
};

/// Implement on a marker struct to define an activity.
///
/// Typically, you will want to use the `#[activity]` attribute within an `#[activities]` macro to
/// define activities. However, this trait may be implemented manually if desired.
pub trait ActivityDefinition {
    /// Type of the input argument to the workflow
    type Input: TemporalDeserializable + TemporalSerializable + 'static;
    /// Type of the output of the workflow
    type Output: TemporalDeserializable + TemporalSerializable + 'static;

    /// The name that will be used for the activity type.
    fn name() -> &'static str
    where
        Self: Sized;
}

/// Returned as errors from activity functions.
#[derive(Debug)]
pub enum ActivityError {
    /// Return this error to attach application-failure metadata to an activity failure.
    Application(Box<ApplicationFailure>),
    /// Return this error to indicate your activity is cancelling
    Cancelled {
        /// Optional cancellation details.
        details: Option<FailurePayloads>,
    },
    /// Return this error to indicate that the activity will be completed outside of this activity
    /// definition, by an external client.
    WillCompleteAsync,
}

impl ActivityError {
    /// Construct a cancelled error without details
    pub fn cancelled() -> Self {
        Self::Cancelled { details: None }
    }

    /// Construct a cancelled error with details that will be converted using the active data
    /// converter.
    pub fn cancelled_with_details<T>(details: T) -> Self
    where
        T: Into<FailurePayloads>,
    {
        Self::Cancelled {
            details: Some(details.into()),
        }
    }

    /// Construct an application activity error.
    pub fn application(err: ApplicationFailure) -> Self {
        Self::Application(err.into())
    }
}

impl<E> From<E> for ActivityError
where
    E: Into<anyhow::Error>,
{
    fn from(source: E) -> Self {
        match source.into().downcast::<ApplicationFailure>() {
            Ok(application_failure) => Self::Application(Box::new(application_failure)),
            Err(err) => Self::Application(ApplicationFailure::new(err).into()),
        }
    }
}