pub struct IntegrationEventEnvelope {
pub id: String,
pub event_type: String,
pub source_context: String,
pub aggregate_id: String,
pub occurred_at: DateTime<Utc>,
pub published_at: DateTime<Utc>,
pub version: u32,
pub correlation_id: Option<String>,
pub causation_id: Option<String>,
pub payload: Value,
}Expand description
Type-erased integration event envelope for cross-module transport
The envelope wraps any integration event with metadata and serializes the event payload to JSON for transport between bounded contexts.
§Fields
id: Unique envelope ID (UUID)event_type: Dot-notation event typesource_context: Origin bounded contextpayload: JSON-serialized event data
§Example
let event = UserCreatedIntegrationEvent { ... };
let envelope = IntegrationEventEnvelope::from_event(&event)?;
// Later, deserialize back to the typed event
let restored: UserCreatedIntegrationEvent = envelope.deserialize()?;Fields§
§id: StringUnique envelope ID
event_type: StringEvent type (e.g., “sapiens.user.created”)
source_context: StringSource bounded context
aggregate_id: StringAggregate ID in source context
occurred_at: DateTime<Utc>When the event occurred
published_at: DateTime<Utc>When the envelope was created/published
version: u32Event schema version
correlation_id: Option<String>Correlation ID for distributed tracing
causation_id: Option<String>Causation ID (parent event/envelope that caused this one)
payload: ValueJSON-serialized event payload
Implementations§
Source§impl IntegrationEventEnvelope
impl IntegrationEventEnvelope
Sourcepub fn from_event<E: IntegrationEvent>(event: &E) -> Result<Self, Error>
pub fn from_event<E: IntegrationEvent>(event: &E) -> Result<Self, Error>
Create an envelope from an integration event
Serializes the event to JSON and wraps it with metadata.
§Errors
Returns serde_json::Error if the event cannot be serialized.
Sourcepub fn deserialize<E: IntegrationEvent>(&self) -> Result<E, Error>
pub fn deserialize<E: IntegrationEvent>(&self) -> Result<E, Error>
Deserialize the payload back to a typed event
§Errors
Returns serde_json::Error if the payload doesn’t match the expected type.
Sourcepub fn with_causation_id(self, causation_id: impl Into<String>) -> Self
pub fn with_causation_id(self, causation_id: impl Into<String>) -> Self
Set the causation ID (for event chaining)
Sourcepub fn with_correlation_id(self, correlation_id: impl Into<String>) -> Self
pub fn with_correlation_id(self, correlation_id: impl Into<String>) -> Self
Set the correlation ID (for distributed tracing)
Sourcepub fn matches_pattern(&self, pattern: &str) -> bool
pub fn matches_pattern(&self, pattern: &str) -> bool
Check if this envelope matches a pattern
Patterns support:
- Exact match: “sapiens.user.created”
- Wildcard suffix: “sapiens.user.*” matches “sapiens.user.created”, “sapiens.user.deleted”
- Global wildcard: “*” matches everything