Crates.io | complex-enum-macros |
lib.rs | complex-enum-macros |
version | 0.1.1 |
source | src |
created_at | 2024-11-06 11:03:52.03287 |
updated_at | 2024-11-06 11:37:12.208593 |
description | Convert enums with non-unit variants to privitives |
homepage | |
repository | https://github.com/okhsunrog/complex-enum-macros |
max_upload_size | |
id | 1438103 |
size | 19,739 |
A Rust procedural macro for deriving code/discriminant conversion methods for enums, supporting both unit and complex variants.
Add this to your Cargo.toml
:
[dependencies]
complex-enum-macros = "0.1"
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);
}