#enums #macro

macro itsuki

macro that defines a simple zero-based sequential enum

4 releases

0.2.0 May 11, 2024
0.1.2 Apr 15, 2024
0.1.1 Apr 7, 2024
0.1.0 Mar 31, 2024

#89 in #enum

44 downloads per month
Used in 2 crates

MIT license

14KB
245 lines

itsuki

Crate Status

macro that defines a simple zero-based sequential enum ⭐️

Usage

Function-like

use itsuki::define_zero_indexed_enum;

define_zero_indexed_enum! {
    Quintuplets => [Ichika, Nino, Miku, Yotsuba, Itsuki]
}

let miku = Quintuplets::Miku;

assert_eq!(miku, Quintuplets::Miku);
assert_ne!(miku, Quintuplets::Nino);

assert_eq!(Quintuplets::len(), 5);

use Quintuplets::*;

assert_eq!(Quintuplets::vars_vec(), vec![Ichika, Nino, Miku, Yotsuba, Itsuki]);
assert_eq!(Quintuplets::vars_array(), [Ichika, Nino, Miku, Yotsuba, Itsuki]);

assert_eq!(Nino.next(), Miku);
assert_eq!(Itsuki.next(), Ichika);

assert_eq!(Yotsuba.prev(), Miku);
assert_eq!(Ichika.prev(), Itsuki);

assert_eq!(Ichika.next_in(|q| [Miku, Yotsuba].contains(&q)), Miku);
assert_eq!(Miku.next_in(|q| [Miku, Yotsuba].contains(&q)), Yotsuba);

assert_eq!(Nino.prev_in(|q| [Miku, Yotsuba].contains(&q)), Yotsuba);
assert_eq!(Yotsuba.prev_in(|q| [Miku, Yotsuba].contains(&q)), Miku);

assert_eq!(Miku.val(), 2);
assert_eq!(Yotsuba.val(), 3);

assert_eq!(Quintuplets::try_from(0), Ok(Ichika));
assert_eq!(Quintuplets::try_from(4), Ok(Itsuki));
assert_eq!(Quintuplets::try_from(5), Err(()));

assert_eq!(1.try_into(), Ok(Nino));
assert_eq!(3.try_into(), Ok(Yotsuba));

Derive

use itsuki::ZeroIndexedEnum;

#[derive(ZeroIndexedEnum, Debug, Clone, Copy, PartialEq, Eq)]
enum Quintuplets {
    Ichika,
    Nino,
    Miku,
    Yotsuba,
    Itsuki,
}

Attribute

use itsuki::zero_indexed_enum;

#[zero_indexed_enum]
enum Quintuplets {
    Ichika,
    Nino,
    Miku,
    Yotsuba,
    Itsuki,
}

License

MIT

Dependencies

~270–720KB
~17K SLoC