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: u32Unique identifier to match responses to requests. Generated automatically if not specified.
timecode: i64Timestamp in milliseconds since UNIX epoch. Set automatically on message creation.
topic: StringFully-qualified topic name (FQDN). Format: “domain.subtopic.path” e.g., “ads.plc1.GM.stData”
message_type: MessageTypeThe type/purpose of this message.
data: ValueThe payload data.
- For requests: arguments/parameters
- For responses: result data
crc: u32CRC32 checksum of the message (optional, for integrity verification).
success: boolWhether the operation was successful (for responses).
error_message: StringError message if the operation failed (for responses).
Implementations§
Source§impl CommandMessage
impl CommandMessage
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty CommandMessage with auto-generated transaction_id and timecode.
Sourcepub fn request(topic: &str, data: Value) -> Self
pub fn request(topic: &str, data: Value) -> Self
Create a request message with the specified topic and data.
Sourcepub fn write(topic: &str, data: Value) -> Self
pub fn write(topic: &str, data: Value) -> Self
Create a write request for the specified topic with data.
Sourcepub fn unsubscribe(topic: &str) -> Self
pub fn unsubscribe(topic: &str) -> Self
Create an unsubscribe request for the specified topic.
Sourcepub fn response(transaction_id: u32, data: Value) -> Self
pub fn response(transaction_id: u32, data: Value) -> Self
Create a successful response to a request.
Sourcepub fn error_response(transaction_id: u32, error: &str) -> Self
pub fn error_response(transaction_id: u32, error: &str) -> Self
Create an error response to a request.
Sourcepub fn broadcast(topic: &str, data: Value) -> Self
pub fn broadcast(topic: &str, data: Value) -> Self
Create a broadcast message with the specified topic and data.
Sourcepub fn with_transaction_id(self, id: u32) -> Self
pub fn with_transaction_id(self, id: u32) -> Self
Set the transaction ID.
Sourcepub fn with_topic(self, topic: &str) -> Self
pub fn with_topic(self, topic: &str) -> Self
Set the topic.
Sourcepub fn with_success(self, data: Value) -> Self
pub fn with_success(self, data: Value) -> Self
Set as successful with data.
Sourcepub fn with_error(self, error: &str) -> Self
pub fn with_error(self, error: &str) -> Self
Set as error with message.
Sourcepub fn set_success(&mut self, data: Value)
pub fn set_success(&mut self, data: Value)
Set successful result on this message.
Sourcepub fn into_response(self, data: Value) -> Self
pub fn into_response(self, data: Value) -> Self
Convert this request into a successful response.
Sourcepub fn into_error_response(self, error: &str) -> Self
pub fn into_error_response(self, error: &str) -> Self
Convert this request into an error response.
Sourcepub fn is_response(&self) -> bool
pub fn is_response(&self) -> bool
Check if this is a response message.
Sourcepub fn is_broadcast(&self) -> bool
pub fn is_broadcast(&self) -> bool
Check if this is a broadcast message.
Sourcepub fn is_heartbeat(&self) -> bool
pub fn is_heartbeat(&self) -> bool
Check if this is a heartbeat message.
Sourcepub fn is_request(&self) -> bool
pub fn is_request(&self) -> bool
Check if this is a request (Read, Write, Subscribe, Unsubscribe, or Request).
Sourcepub fn subtopic(&self) -> String
pub fn subtopic(&self) -> String
Get the subtopic (everything after the first dot), normalized to lowercase.
Sourcepub fn compute_crc(&self) -> u32
pub fn compute_crc(&self) -> u32
Compute CRC32 of the message content.
Sourcepub fn update_crc(&mut self)
pub fn update_crc(&mut self)
Update the CRC field with the computed value.
Sourcepub fn verify_crc(&self) -> bool
pub fn verify_crc(&self) -> bool
Verify that the CRC matches the message content.
Trait Implementations§
Source§impl Clone for CommandMessage
impl Clone for CommandMessage
Source§fn clone(&self) -> CommandMessage
fn clone(&self) -> CommandMessage
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more