#enums #proc-macro #macro

macro complex-enum-macros

Convert enums with non-unit variants to privitives

2 releases

new 0.1.1 Nov 6, 2024
0.1.0 Nov 6, 2024

#401 in Procedural macros

MIT/Apache

7KB
50 lines

complex-enum-macros

A Rust procedural macro for deriving code/discriminant conversion methods for enums, supporting both unit and complex variants.

Features

  • Convert enum variants to their discriminant values
  • Support for unit variants, struct-like variants, and tuple variants
  • Works with explicit discriminants
  • Zero dependencies in the generated code

Installation

Add this to your Cargo.toml:

[dependencies]
complex-enum-macros = "0.1"

Usage

use complex_enum_macros::ToCode;

#[derive(ToCode)]
#[repr(u8)]
pub enum I2cCommand {
    Uptime = 0x00,
    AdcStats = 0x01,
    SampleRate { rate: Option<u32> } = 0x02,
    GoertThreshold { threshold: Option<u16> } = 0x03,
    Reset = 0x06,
    // Variants without explicit discriminants are also supported
    Scan,
    WavStart,
}

fn main() {
    let cmd = I2cCommand::Uptime;
    assert_eq!(cmd.to_code(), Some(0x00));

    let cmd = I2cCommand::SampleRate { rate: Some(1000) };
    assert_eq!(cmd.to_code(), Some(0x02));

    // Variants without explicit discriminants return None
    let cmd = I2cCommand::Scan;
    assert_eq!(cmd.to_code(), None);
}

Dependencies

~225–660KB
~16K SLoC