enum2contract

github crates.io docs.rs

enum2contract is a no_std compatible rust derive macro that lets users specify contracts for pub/sub style messaging using strongly typed rust enums.

Usage

Add this to your Cargo.toml:

toml enum2contract = { version = "0.1.2", features = ["serde-derive"] }

Example:

```rust use enum2contract::EnumContract;

[derive(EnumContract)]

pub enum Message { #[topic("notify/{group}")] Notify,

#[topic("notify_all")]
NotifyAll,

#[topic("system/{id}/start/{mode}")]
Start { immediate: bool },

}

[test]

fn createnotifymessage() { let (topic, payload) = Message::notify("partial"); asserteq!(topic, "notify/partial"); asserteq!(payload, NotifyPayload::default()); }

[test]

fn createnotifyallmessage() { let (topic, payload) = Message::notifyall(); asserteq!(topic, "notifyall"); assert_eq!(payload, NotifyAllPayload::default()); }

[test]

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!