Derive Macro Command

Source
#[derive(Command)]
{
    // Attributes available to this derive:
    #[stream]
}
Expand description

Derive macro for implementing the CommandStreams trait.

This macro generates a complete implementation of CommandStreams based on fields marked with #[stream]. You only need to implement CommandLogic for your domain logic.

§Example

use eventcore_macros::Command;
use eventcore::{CommandLogic, prelude::*};

#[derive(Command, Clone)]
struct TransferMoney {
    #[stream]
    from_account: StreamId,
    #[stream]
    to_account: StreamId,
    amount: Money,
}

#[async_trait]
impl CommandLogic for TransferMoney {
    type State = AccountBalances;
    type Event = BankingEvent;

    fn apply(&self, state: &mut Self::State, event: &StoredEvent<Self::Event>) {
        // Your event folding logic
    }

    async fn handle(
        &self,
        read_streams: ReadStreams<Self::StreamSet>,
        state: Self::State,
        input: Self::Input,
        stream_resolver: &mut StreamResolver,
    ) -> CommandResult<Vec<StreamWrite<Self::StreamSet, Self::Event>>> {
        // Your business logic
    }
}

This automatically generates:

  • Complete CommandStreams trait implementation
  • type Input = Self (the command struct serves as input)
  • type StreamSet = TransferMoneyStreamSet (phantom type for stream access)
  • fn read_streams() implementation extracting from #[stream] fields