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

macro no-std litenum

minimal convertion utilities between literal and enum

4 releases

0.1.3 Aug 12, 2023
0.1.2 Aug 9, 2023
0.1.1 Aug 9, 2023
0.1.0 Aug 8, 2023

#2488 in Rust patterns

27 downloads per month

MIT license

9KB
110 lines

litenum

litenum is minimal utilities for convertion 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.to_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]  // equals to
                 // `#[litenum::to] #[litenum::from]`
#[derive(Debug, PartialEq)]
enum AnkerTarget {
    _blank,
    _self,
    _top,
    _parent,
}

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

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

Dependencies

~1.5MB
~33K SLoC