#enums #proc-macro #convert #macro #no-alloc #no-std

macro no-std litenum

minimal convertion utilities between literal and enum

7 releases (3 stable)

1.1.1 Aug 16, 2024
1.0.0 Aug 15, 2024
0.1.3 Aug 12, 2023

#2092 in Rust patterns

34 downloads per month

MIT license

10KB
132 lines

litenum

litenum is the minimal utility for conversion between literal and enum !

Features

  • minimal inplementation
  • no std, no alloc

How to use

to literal

#[litenum::to]
enum AnkerTarget {
    _blank,
    _self,
    _top,
    _parent,
}

fn main() {
    assert_eq!(
        AnkerTarget::_blank.lit(),
        "_blank",
    )
}

from literal

#[litenum::from]
#[derive(Debug, PartialEq)]
enum AnkerTarget {
    _blank,
    _self,
    _top,
    _parent,
}

fn main() {
    assert_eq!(
        AnkerTarget::from_lit("_blank"),
        Some(AnkerTarget::_blank),
    )
}

impl both at once

#[litenum::ium] // same as
                // `#[litenum::to] #[litenum::from]`
#[derive(Debug, PartialEq)]
enum AnkerTarget {
    _blank,
    _self,
    _top,
    _parent,
}

fn main() {
    assert_eq!(
        AnkerTarget::_blank.lit(),
        "_blank",
    );

    assert_eq!(
        AnkerTarget::from_lit("_blank"),
        Some(AnkerTarget::_blank),
    );
}

Dependencies

~1.5MB
~38K SLoC