finte

Crates.iofinte
lib.rsfinte
version0.2.1
sourcesrc
created_at2021-06-29 16:50:10.521042
updated_at2021-07-23 01:33:33.01855
descriptionconvert between integer and Rust enum
homepage
repositoryhttps://github.com/hismito/finte
max_upload_size
id416233
size3,091
(hismito)

documentation

README

Finte

Finte is a proc-macro crate to auto generate conversion code between integer and Rust enum

Example

#[derive(finte::IntEnum)]
#[repr(u16)]
pub enum RustEdition {
    Prev = 2015,
    Now  = 2018,
    Next = 2021,
}


// the above generates

impl finte::IntEnum for RustEdition {
    type Int = u16;

    fn try_from_int(value: Self::Int) -> Option<Self> {
        match value {
            2015 => Some(Self::Prev),
            2018 => Some(Self::Now),
            2021 => Some(Self::Next),
            _ => None,
        }
    }

    fn int_value(&self) -> Self::Int {
        match self {
            Self::Prev => 2015,
            Self::Now  => 2018,
            Self::Next => 2021,
        }
    }
}
Commit count: 19

cargo fmt