#literals #enums #convert #traits #try #back #into-literal

macro literal-enum

Convert an [enum] to a [literal], and try to convert it back

7 releases

0.1.6 Sep 1, 2024
0.1.5 Sep 6, 2023
0.1.2 Jul 16, 2023

#2170 in Procedural macros

Download history 13/week @ 2024-09-12 31/week @ 2024-09-19 36/week @ 2024-09-26 8/week @ 2024-10-03 7/week @ 2024-10-10 7/week @ 2024-10-17 2/week @ 2024-10-24 8/week @ 2024-10-31 6/week @ 2024-11-07 4/week @ 2024-11-14 5/week @ 2024-11-21 4/week @ 2024-11-28 23/week @ 2024-12-05 32/week @ 2024-12-12 2/week @ 2024-12-19

57 downloads per month
Used in 4 crates (via dnspod-lib)

MIT license

10KB
150 lines

literal-enum

Automatically implements the TryFrom<Literal> trait and Into<Literal> trait for an enum where the literals must be the same type(one of [&'static str, &'static [u8], u8, char, u32, bool])

If you need to convert other interger type(i8, i16, u16, i32, i64, u64, i128, u128), you can use intype-enum

Usage Example

use literal_enum::LiteralEnum;

#[derive(LiteralEnum)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
enum Command {
    /// increment pointer
    #[lit = b'>']
    IncrementPointer,
    /// decrement pointer
    #[lit = b'<']
    DecrementPointer,
}

assert_eq!(Command::try_from(b'>').unwrap(), Command::IncrementPointer);
let b: u8 = Command::IncrementPointer.into();
assert_eq!(b, b'>');
use literal_enum::LiteralEnum;

#[derive(LiteralEnum)]
enum E {
    A,
    AAAA,
}

let s: &'static str = E::A.into();
assert_eq!(s, "A");

Dependencies

~210–640KB
~15K SLoC