Skip to main content

EmergentMessage

Struct EmergentMessage 

Source
pub struct EmergentMessage {
    pub id: MessageId,
    pub message_type: MessageType,
    pub source: PrimitiveName,
    pub correlation_id: Option<CorrelationId>,
    pub causation_id: Option<CausationId>,
    pub timestamp_ms: Timestamp,
    pub payload: Value,
    pub metadata: Option<Value>,
}
Expand description

Standard message envelope for all Emergent communications.

All messages in Emergent use this standard envelope format. Developers specify a message_type string and put their domain data in the payload field.

§Example

use emergent_client::EmergentMessage;
use serde_json::json;

let message = EmergentMessage::new("user.created")
    .with_source("user_service")
    .with_payload(json!({
        "user_id": "u_12345",
        "email": "user@example.com"
    }));

Fields§

§id: MessageId

Unique message ID (TypeID format: msg_<uuid_v7>).

§message_type: MessageType

Message type for routing (e.g., “email.received”, “timer.tick”).

§source: PrimitiveName

Source client that published this message.

§correlation_id: Option<CorrelationId>

Optional correlation ID for request-response or tracing.

§causation_id: Option<CausationId>

Optional causation ID (ID of message that triggered this one).

§timestamp_ms: Timestamp

Timestamp when message was created (Unix ms).

§payload: Value

User-defined payload (any serializable data).

§metadata: Option<Value>

Optional metadata for debugging, tracing, etc.

Implementations§

Source§

impl EmergentMessage

Source

pub fn new(message_type: &str) -> Self

Create a new message with the given type.

Generates a unique ID and sets the current timestamp.

§Panics

Panics if the message type is invalid.

Source

pub fn new_with_id_and_timestamp( message_type: &str, id: MessageId, timestamp_ms: Timestamp, ) -> Self

Create a new message with explicit ID and timestamp.

This is a pure function suitable for testing and deterministic message creation. For production use, prefer new() which generates a unique ID and current timestamp.

§Panics

Panics if the message type is invalid.

Source

pub fn with_source(self, source: &str) -> Self

Set the source of this message.

§Panics

Panics if the source name is invalid.

Source

pub fn with_payload(self, payload: impl Serialize) -> Self

Set the payload of this message.

Source

pub fn with_correlation_id(self, id: impl Into<CorrelationId>) -> Self

Set the correlation ID (for request-response patterns).

Source

pub fn with_causation_id(self, id: impl Into<CausationId>) -> Self

Set the causation ID (ID of the message that triggered this one).

Source

pub fn with_correlation_id_option(self, id: Option<&CorrelationId>) -> Self

Set the correlation ID from an optional value.

Useful for copying correlation IDs from request messages to responses.

Source

pub fn with_causation_from_message(self, msg_id: &MessageId) -> Self

Set the causation ID from a MessageId.

This is a convenience method that converts the MessageId to a CausationId.

Source

pub fn with_metadata(self, metadata: impl Serialize) -> Self

Set optional metadata.

Source

pub fn id(&self) -> &MessageId

Get the message ID.

Source

pub fn message_type(&self) -> &MessageType

Get the message type.

Source

pub fn source(&self) -> &PrimitiveName

Get the source.

Source

pub fn payload(&self) -> &Value

Get the raw payload value.

Source

pub fn payload_as<T: DeserializeOwned>(&self) -> Result<T, Error>

Deserialize the payload into a specific type.

§Errors

Returns an error if the payload cannot be deserialized into type T.

Source

pub fn has_stdout_payload(&self) -> bool

Check whether this message has an exec-source payload shape.

Returns true if the payload is a JSON object with a stdout string field, which is the envelope format produced by the exec-source primitive.

Source

pub fn unwrap_stdout(self) -> Self

Unwrap an exec-source payload by extracting and parsing the stdout field.

If the payload is an object with a stdout string field (the envelope format produced by exec-source), extracts that string and attempts to parse it as JSON. If parsing succeeds, the payload is replaced with the parsed value. If stdout is not valid JSON, the payload is replaced with the raw string value.

If the payload does not have the exec-source shape, the message is returned unchanged.

This eliminates the need for a dedicated exec-handler running jq -c '.stdout | fromjson' in the pipeline.

Source

pub fn to_json(&self) -> Result<Vec<u8>, Error>

Serialize the message to JSON bytes.

§Errors

Returns an error if serialization fails.

Source

pub fn from_json(data: &[u8]) -> Result<Self, Error>

Deserialize a message from JSON bytes.

§Errors

Returns an error if deserialization fails.

Source

pub fn to_msgpack(&self) -> Result<Vec<u8>, Error>

Serialize the message to MessagePack bytes.

§Errors

Returns an error if serialization fails.

Source

pub fn from_msgpack(data: &[u8]) -> Result<Self, Error>

Deserialize a message from MessagePack bytes.

§Errors

Returns an error if deserialization fails.

Trait Implementations§

Source§

impl Clone for EmergentMessage

Source§

fn clone(&self) -> EmergentMessage

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EmergentMessage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for EmergentMessage

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for EmergentMessage

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> ActonMessage for T
where T: Any + Send + Sync + Debug + DynClone + 'static,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Implementation of as_any for the blanket impl.

Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Implementation of as_any_mut for the blanket impl.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,