#enums #macro-derive #contract #strongly-typed #macro #pub-sub

macro enum2contract

enum2contract is a no_std compatible rust derive macro that lets users specify contracts for pub/sub style messaging using strongly typed rust enums. Conversion methods for JSON and binary are created for the generated payloads

7 releases

0.1.6 Apr 11, 2023
0.1.5 Apr 10, 2023
0.1.3 Mar 26, 2023

#341 in Procedural macros

Download history 306/week @ 2023-12-02 546/week @ 2023-12-09 360/week @ 2023-12-16 1/week @ 2023-12-23 125/week @ 2023-12-30 112/week @ 2024-01-06 150/week @ 2024-01-13 237/week @ 2024-01-20 144/week @ 2024-01-27 220/week @ 2024-02-03 185/week @ 2024-02-10 142/week @ 2024-02-17 219/week @ 2024-02-24 272/week @ 2024-03-02 148/week @ 2024-03-09 103/week @ 2024-03-16

765 downloads per month

MIT license

14KB
209 lines

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.

Conversion methods for JSON and binary are created for the generated payloads.

Usage

Add this to your Cargo.toml:

enum2contract = "0.1.6"
serde = { version = "1.0.158", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.94", default-features = false, features = ["alloc"] }
postcard = { version = "1.0.4", features = ["alloc"] }

Example:

use enum2contract::EnumContract;
use serde::{Deserialize, Serialize};

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

    #[topic("notify_all")]
    NotifyAll,

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

#[test]
fn topic() {
    assert_eq!(Message::notify_topic("subset"), "notify/subset");

    assert_eq!(Message::notify_all_topic(), "notify_all");

    assert_eq!(
        Message::start_topic(&3.to_string(), "idle"),
        "system/3/start/idle"
    );
}

#[test]
fn message() {
    assert_eq!(
        Message::notify("subgroup"),
        ("notify/subgroup".to_string(), NotifyPayload::default())
    );

    assert_eq!(
        Message::notify_all(),
        ("notify_all".to_string(), NotifyAllPayload::default())
    );

    assert_eq!(
        Message::start(&3.to_string(), "idle"),
        ("system/3/start/idle".to_string(), StartPayload::default())
    );
}

#[test]
fn notify_payload_from_json_with_data() {
    let json = r#"{"immediate":true,"timeout":40}"#;
    let payload = StartPayload::from_json(json).unwrap();
    assert_eq!(
        payload,
        StartPayload {
            immediate: true,
            timeout: 40,
        }
    );
}

This crate is #![no_std] compatible but requires alloc.

Dependencies

~1.7–2.3MB
~52K SLoC