Skip to main content

SagaBuilder

Struct SagaBuilder 

Source
pub struct SagaBuilder<Input, Output, Ctx, Err, State> { /* private fields */ }
Expand description

Type-state builder for constructing type-safe sagas.

The builder enforces at compile-time that:

  • Each step’s input type matches the previous step’s output type
  • The saga’s input type matches the first step’s input
  • The saga’s output type matches the last step’s output

§Compile-time Type Safety

The builder ensures type safety at compile time. Mismatched types will not compile:

use changeset_saga::{SagaBuilder, SagaStep};

struct StepA;
impl SagaStep for StepA {
    type Input = i32;
    type Output = String;  // Outputs String
    type Context = ();
    type Error = ();
    fn name(&self) -> &'static str { "a" }
    fn execute(&self, _: &(), input: i32) -> Result<String, ()> {
        Ok(input.to_string())
    }
}

struct StepB;
impl SagaStep for StepB {
    type Input = i32;  // Expects i32, not String!
    type Output = i32;
    type Context = ();
    type Error = ();
    fn name(&self) -> &'static str { "b" }
    fn execute(&self, _: &(), input: i32) -> Result<i32, ()> {
        Ok(input * 2)
    }
}

// This should fail: StepB expects i32 but StepA outputs String
let saga = SagaBuilder::new()
    .first_step(StepA)
    .then(StepB)  // Compile error here!
    .build();

An empty saga (without calling first_step()) cannot be built:

use changeset_saga::SagaBuilder;

// Cannot build an empty saga - `build()` is only available after `first_step()`
let saga = SagaBuilder::<(), (), (), ()>::new().build();

Implementations§

Source§

impl<Ctx, Err> SagaBuilder<(), (), Ctx, Err, Empty>

Source

pub fn new() -> Self

Create a new saga builder in the empty state.

Source

pub fn first_step<S>( self, step: S, ) -> SagaBuilder<S::Input, S::Output, Ctx, Err, HasSteps<S::Output>>
where S: SagaStep<Context = Ctx, Error = Err> + 'static,

Add the first step to the saga.

This establishes the saga’s input type from the step’s input type.

Source§

impl<Input, CurrentOutput, Ctx, Err> SagaBuilder<Input, CurrentOutput, Ctx, Err, HasSteps<CurrentOutput>>

Source

pub fn then<S>( self, step: S, ) -> SagaBuilder<Input, S::Output, Ctx, Err, HasSteps<S::Output>>
where S: SagaStep<Input = CurrentOutput, Context = Ctx, Error = Err> + 'static,

Add another step to the saga.

The step’s input type must match the current output type.

Source

pub fn build(self) -> Saga<Input, CurrentOutput, Ctx, Err>
where Input: Clone + Send + 'static, CurrentOutput: Send + 'static, Err: Debug,

Build the saga from the accumulated steps.

Trait Implementations§

Source§

impl<Ctx, Err> Default for SagaBuilder<(), (), Ctx, Err, Empty>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Input, Output, Ctx, Err, State> Freeze for SagaBuilder<Input, Output, Ctx, Err, State>

§

impl<Input, Output, Ctx, Err, State> !RefUnwindSafe for SagaBuilder<Input, Output, Ctx, Err, State>

§

impl<Input, Output, Ctx, Err, State> !Send for SagaBuilder<Input, Output, Ctx, Err, State>

§

impl<Input, Output, Ctx, Err, State> !Sync for SagaBuilder<Input, Output, Ctx, Err, State>

§

impl<Input, Output, Ctx, Err, State> Unpin for SagaBuilder<Input, Output, Ctx, Err, State>
where Input: Unpin, Output: Unpin, State: Unpin,

§

impl<Input, Output, Ctx, Err, State> UnsafeUnpin for SagaBuilder<Input, Output, Ctx, Err, State>

§

impl<Input, Output, Ctx, Err, State> !UnwindSafe for SagaBuilder<Input, Output, Ctx, Err, State>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.