Expand description
§MQTT Typed Client Macros
This crate provides procedural macros for generating typed MQTT subscribers and publishers with automatic topic parameter extraction and payload serialization.
§Overview
The main macro mqtt_topic allows you to annotate a struct with
a topic pattern, automatically generating the necessary trait implementations
and helper methods for MQTT subscription and publishing.
§Features
- Topic Parameter Extraction: Automatically extracts named parameters from MQTT topics
- Type Safety: Compile-time validation of struct fields against topic patterns
- Flexible Payload Handling: Support for custom payload types with automatic serialization
- Optional Topic Access: Include the full topic match information if needed
- Dual Mode Generation: Generate subscriber, publisher, or both methods
- Generated Helper Methods: Convenient subscription and publishing methods with pattern constants
§Quick Start
ⓘ
use mqtt_typed_client_macros::mqtt_topic;
use std::sync::Arc;
use mqtt_typed_client_core::topic::topic_match::TopicMatch;
#[derive(Debug)]
#[mqtt_topic("sensors/{sensor_id}/temperature/{room}")]
struct TemperatureReading {
sensor_id: u32, // Extracted from {sensor_id} in topic
room: String, // Extracted from {room} in topic
payload: f64, // Message payload (temperature value)
topic: Arc<TopicMatch>, // Optional: full topic match info
}
// Generated constants:
// TemperatureReading::TOPIC_PATTERN = "sensors/{sensor_id}/temperature/{room}"
// TemperatureReading::MQTT_PATTERN = "sensors/+/temperature/+"
// Generated methods:
// let subscriber = TemperatureReading::subscribe(&client).await?;
// TemperatureReading::publish(&client, sensor_id, room, &data).await?;§Supported Field Types
- Topic Parameters: Any field name matching a
{parameter}in the topic pattern payload: The message payload, can be any deserializable typetopic: Must beArc<TopicMatch>, provides access to full topic information
§Topic Pattern Syntax
{param_name}- Named parameter that becomes a struct field+- Anonymous single-level wildcard (not extracted)#- Anonymous multi-level wildcard (not extracted, must be last){param_name:#}- Named multi-level wildcard (extracted as string)
§⚠️ Publisher Limitations
Multi-level wildcards (# or {param:#}) are not supported for publishers
because they represent variable-length topic segments that cannot be constructed
from fixed parameters.
ⓘ
use mqtt_typed_client_macros::mqtt_topic;
// ✅ This works for both subscriber and publisher
#[mqtt_topic("sensors/{sensor_id}/data")]
struct SensorData { sensor_id: u32, payload: f64 }
// ✅ This works for subscriber only
#[mqtt_topic("alerts/{category}/#", subscriber)]
struct Alert { category: String, payload: String }
// ❌ This will cause a compile error
// #[mqtt_topic("events/{event_type}/{details:#}")]
// struct Event { /* ... */ }
// 🔧 Solution: Use separate structs
#[mqtt_topic("events/{event_type}/{details:#}", subscriber)]
struct EventReceived { event_type: String, details: String, payload: Vec<u8> }
#[mqtt_topic("events/{event_type}", publisher)]
struct EventToSend { event_type: String, payload: Vec<u8> }Attribute Macros§
- mqtt_
topic - Generate a typed MQTT subscriber and/or publisher from a struct and topic pattern