| Crates.io | macrokit |
| lib.rs | macrokit |
| version | 0.1.0 |
| created_at | 2025-09-04 18:57:29.183422+00 |
| updated_at | 2025-09-04 18:57:29.183422+00 |
| description | A collection of procedural derive macros for Rust. |
| homepage | https://github.com/mbolaric/macrokit |
| repository | https://github.com/mbolaric/macrokit |
| max_upload_size | |
| id | 1824634 |
| size | 24,227 |
A collection of procedural derive macros for Rust.
macrokit currently provides two derive macros:
#[derive(FromReprAsOption)]This is the recommended macro for safe, fallible conversions. It generates a from_repr(value: T) -> Option<Self> function, where T is the integer type from your #[repr(T)] attribute.
Option<Self>, forcing you to handle cases where the integer value does not match any enum variant.Unknown.#[derive(FromReprWithUnknown)]This macro is for infallible conversions. It implements the standard From<T> trait, allowing you to use .into() and From::from() where T is the integer type from your #[repr(T)] attribute..
Unknown which will be used if the integer value does not match any other variant.First, add macrokit to your project's Cargo.toml.
[dependencies]
macrokit = { path = "path/to/macrokit" }
FromReprAsOptionUse this when you want to handle invalid integer values explicitly.
use macrokit::FromReprAsOption;
#[derive(Debug, PartialEq, FromReprAsOption)]
#[repr(u8)]
pub enum Command {
Read = 1,
Write = 2,
}
assert_eq!(Command::from_repr(1), Some(Command::Read));
assert_eq!(Command::from_repr(3), None);
FromReprWithUnknownUse this when you have a clear fallback value and want the convenience of the From trait.
use macrokit::FromReprWithUnknown;
#[derive(Debug, PartialEq, FromReprWithUnknown)]
#[repr(u8)]
pub enum Status {
Active = 0,
Inactive = 1,
Unknown, // This variant is required
}
let status: Status = 1u8.into();
assert_eq!(status, Status::Inactive);
let unknown_status: Status = 99u8.into();
assert_eq!(unknown_status, Status::Unknown);