#enums #string #macro #proc-macro #u8 #mapping #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

#1364 in Rust patterns

Download history 5454/week @ 2023-11-29 4780/week @ 2023-12-06 4761/week @ 2023-12-13 3395/week @ 2023-12-20 2087/week @ 2023-12-27 4611/week @ 2024-01-03 4174/week @ 2024-01-10 4945/week @ 2024-01-17 4667/week @ 2024-01-24 5837/week @ 2024-01-31 5402/week @ 2024-02-07 3974/week @ 2024-02-14 4031/week @ 2024-02-21 3231/week @ 2024-02-28 3642/week @ 2024-03-06 2658/week @ 2024-03-13

14,233 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

~83KB