evx 0.1.0

A type-safe event bus for domain events, CQRS, Redis Pub/Sub, and durable work queues
Documentation
//! Traits implemented by typed application messages.

use serde::{Serialize, de::DeserializeOwned};

use crate::event::scope::EventScope;
use crate::queue::QueuePolicy;

/// Base trait for all messages that can travel through Evx.
pub trait Message: Serialize + DeserializeOwned + Clone + Send + Sync + 'static {
    /// Returns the stable CloudEvents `type`, conventionally in reverse-domain
    /// notation.
    fn message_type() -> &'static str
    where
        Self: Sized;

    /// Returns the CloudEvents `source` identifier.
    fn source() -> &'static str
    where
        Self: Sized;

    /// Selects the broker used to route this message.
    fn scope() -> EventScope
    where
        Self: Sized;

    /// Returns the optional CloudEvents `subject`.
    fn subject(&self) -> Option<String> {
        None
    }
}

/// Request-response message used by `EventBus::call`.
pub trait RequestMessage: Message {
    /// Typed response returned by the registered request handler.
    type Response: Message;
}

/// Application command. Commands may mutate state and return a response.
pub trait CommandMessage: RequestMessage {}

/// Application query. Queries should be read-only and return a response.
pub trait QueryMessage: RequestMessage {}

/// Command that should be delivered through a durable work queue.
pub trait QueuedCommand: CommandMessage {
    /// Returns the delivery and recovery policy for this command type.
    fn queue_policy() -> QueuePolicy
    where
        Self: Sized;
}

/// Domain event. Events are facts that already happened and are published
/// through fire-and-forget fan-out.
pub trait DomainEvent: Message {}