Skip to main content

CommandMessage

Struct CommandMessage 

Source
pub struct CommandMessage {
    pub transaction_id: u32,
    pub timecode: i64,
    pub topic: String,
    pub message_type: MessageType,
    pub data: Value,
    pub crc: u32,
    pub success: bool,
    pub error_message: String,
}
Expand description

CommandMessage is the unified message format for all IPC communication.

This struct serves as both request and response, with message_type indicating the role and success/error_message fields populated for responses.

§Example: Creating a read request

use mechutil::ipc::{CommandMessage, MessageType};

let msg = CommandMessage::read("modbus.server1.holding_0");
assert_eq!(msg.message_type, MessageType::Read);

§Example: Creating a response

use mechutil::ipc::CommandMessage;
use serde_json::json;

let mut response = CommandMessage::response(42, json!({"value": 123}));
assert!(response.success);

Fields§

§transaction_id: u32

Unique identifier to match responses to requests. Generated automatically if not specified.

§timecode: i64

Timestamp in milliseconds since UNIX epoch. Set automatically on message creation.

§topic: String

Fully-qualified topic name (FQDN). Format: “domain.subtopic.path” e.g., “ads.plc1.GM.stData”

§message_type: MessageType

The type/purpose of this message.

§data: Value

The payload data.

  • For requests: arguments/parameters
  • For responses: result data
§crc: u32

CRC32 checksum of the message (optional, for integrity verification).

§success: bool

Whether the operation was successful (for responses).

§error_message: String

Error message if the operation failed (for responses).

Implementations§

Source§

impl CommandMessage

Source

pub fn new() -> Self

Create an empty CommandMessage with auto-generated transaction_id and timecode.

Source

pub fn request(topic: &str, data: Value) -> Self

Create a request message with the specified topic and data.

Source

pub fn read(topic: &str) -> Self

Create a read request for the specified topic.

Source

pub fn write(topic: &str, data: Value) -> Self

Create a write request for the specified topic with data.

Source

pub fn subscribe(topic: &str) -> Self

Create a subscribe request for the specified topic.

Source

pub fn unsubscribe(topic: &str) -> Self

Create an unsubscribe request for the specified topic.

Source

pub fn response(transaction_id: u32, data: Value) -> Self

Create a successful response to a request.

Source

pub fn error_response(transaction_id: u32, error: &str) -> Self

Create an error response to a request.

Source

pub fn broadcast(topic: &str, data: Value) -> Self

Create a broadcast message with the specified topic and data.

Source

pub fn heartbeat() -> Self

Create a heartbeat message.

Source

pub fn control(topic: &str, data: Value) -> Self

Create a control message.

Source

pub fn with_transaction_id(self, id: u32) -> Self

Set the transaction ID.

Source

pub fn with_topic(self, topic: &str) -> Self

Set the topic.

Source

pub fn with_data(self, data: Value) -> Self

Set the data payload.

Source

pub fn with_success(self, data: Value) -> Self

Set as successful with data.

Source

pub fn with_error(self, error: &str) -> Self

Set as error with message.

Source

pub fn set_success(&mut self, data: Value)

Set successful result on this message.

Source

pub fn set_error(&mut self, error: &str)

Set error result on this message.

Source

pub fn into_response(self, data: Value) -> Self

Convert this request into a successful response.

Source

pub fn into_error_response(self, error: &str) -> Self

Convert this request into an error response.

Source

pub fn is_response(&self) -> bool

Check if this is a response message.

Source

pub fn is_broadcast(&self) -> bool

Check if this is a broadcast message.

Source

pub fn is_heartbeat(&self) -> bool

Check if this is a heartbeat message.

Source

pub fn is_request(&self) -> bool

Check if this is a request (Read, Write, Subscribe, Unsubscribe, or Request).

Source

pub fn domain(&self) -> &str

Get the domain (first segment of the topic).

Source

pub fn subtopic(&self) -> String

Get the subtopic (everything after the first dot), normalized to lowercase.

Source

pub fn compute_crc(&self) -> u32

Compute CRC32 of the message content.

Source

pub fn update_crc(&mut self)

Update the CRC field with the computed value.

Source

pub fn verify_crc(&self) -> bool

Verify that the CRC matches the message content.

Trait Implementations§

Source§

impl Clone for CommandMessage

Source§

fn clone(&self) -> CommandMessage

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 CommandMessage

Source§

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

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

impl Default for CommandMessage

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for CommandMessage

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 CommandMessage

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> 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

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