Skip to main content

appletheia_application/outbox/command/
command_envelope.rs

1use serde::{Deserialize, Serialize};
2
3use crate::command::{Command, CommandNameOwned};
4use crate::request_context::{CausationId, CorrelationId, MessageId};
5
6use super::CommandEnvelopeError;
7use super::SerializedCommand;
8
9#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
10pub struct CommandEnvelope {
11    pub command_name: CommandNameOwned,
12    pub command: SerializedCommand,
13    pub correlation_id: CorrelationId,
14    pub message_id: MessageId,
15    pub causation_id: CausationId,
16}
17
18impl CommandEnvelope {
19    pub fn try_into_command<C>(&self) -> Result<C, CommandEnvelopeError>
20    where
21        C: Command,
22    {
23        let expected = CommandNameOwned::from(C::NAME);
24        if self.command_name != expected {
25            return Err(CommandEnvelopeError::CommandNameMismatch {
26                expected: expected.to_string(),
27                actual: self.command_name.to_string(),
28            });
29        }
30
31        let json = self.command.value().clone();
32        Ok(serde_json::from_value(json)?)
33    }
34}