enum2contract is a no_std compatible rust derive macro that lets users specify contracts for pub/sub style messaging using strongly typed rust enums.
Add this to your Cargo.toml
:
toml
enum2contract = { version = "0.1.2", features = ["serde-derive"] }
Example:
```rust use enum2contract::EnumContract;
pub enum Message { #[topic("notify/{group}")] Notify,
#[topic("notify_all")]
NotifyAll,
#[topic("system/{id}/start/{mode}")]
Start { immediate: bool },
}
fn createnotifymessage() { let (topic, payload) = Message::notify("partial"); asserteq!(topic, "notify/partial"); asserteq!(payload, NotifyPayload::default()); }
fn createnotifyallmessage() { let (topic, payload) = Message::notifyall(); asserteq!(topic, "notifyall"); assert_eq!(payload, NotifyAllPayload::default()); }
fn createstartmessage() { let (topic, payload) = Message::start("76", "idle"); asserteq!(topic, "system/76/start/idle"); asserteq!(payload, StartPayload { immediate: false }); } ```
This crate is
#![no_std]
compatible!