#enums #string #macro #proc-macro #u8 #generation #enum-utils

enum-utils-from-str

Code generation for mapping from strings to arbitrary values

2 releases

0.1.2 Oct 14, 2019
0.1.0 May 7, 2019

#45 in #u8

Download history 3969/week @ 2024-07-23 3772/week @ 2024-07-30 3710/week @ 2024-08-06 3609/week @ 2024-08-13 4009/week @ 2024-08-20 3470/week @ 2024-08-27 4387/week @ 2024-09-03 3452/week @ 2024-09-10 3660/week @ 2024-09-17 3935/week @ 2024-09-24 4220/week @ 2024-10-01 4396/week @ 2024-10-08 3982/week @ 2024-10-15 5001/week @ 2024-10-22 4682/week @ 2024-10-29 3921/week @ 2024-11-05

18,454 downloads per month
Used in 16 crates (via enum-utils)

MIT license

13KB
286 lines

enum-utils

crates.io docs.rs Build Status

A set of procedural macros for deriving useful functionality on enums.

See the API docs for more information.

FromStr

An efficient, configurable FromStr implementation for C-like enums.

#[derive(Debug, PartialEq, enum_utils::FromStr)]
enum Test {
    Alpha,
    Beta,
}

assert_eq!("Alpha".parse(), Ok(Test::Alpha));
assert_eq!("Beta".parse(), Ok(Test::Beta));

IterVariants

A static method returning an iterator over the variants of an enum.

#[derive(Debug, PartialEq, Eq, enum_utils::IterVariants)]
#[repr(u8)]
pub enum Direction {
    North = 1,
    East,
    South,
    West,
}

use Direction::*;
assert_eq!(Direction::iter().collect::<Vec<_>>(), vec![North, East, South, West]);

TryFromRepr and ReprFrom

Conversion to and from the discriminant of a C-like enum.

use std::convert::TryInto;

#[derive(Debug, Clone, Copy, PartialEq, Eq, enum_utils::ReprFrom, enum_utils::TryFromRepr)]
#[repr(u8)]
pub enum Direction {
    North = 1,
    East,
    South,
    West
}

use Direction::*;
assert_eq!(1u8, North.into());
assert_eq!(4u8, West.into());
assert_eq!(North, 1u8.try_into().unwrap());
assert_eq!(West,  4u8.try_into().unwrap());

Dependencies

~78KB