literal-enum

Crates.ioliteral-enum
lib.rsliteral-enum
version0.1.6
sourcesrc
created_at2023-07-14 05:48:17.854617
updated_at2024-09-01 07:52:32.862974
descriptionConvert an [`enum`] to a [`literal`], and try to convert it back
homepage
repositoryhttps://github.com/hangj/literal-enum
max_upload_size
id915864
size12,177
hangj (hangj)

documentation

https://docs.rs/literal-enum/

README

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");
Commit count: 8

cargo fmt