Skip to content

Instantly share code, notes, and snippets.

@porglezomp
Created June 26, 2016 03:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save porglezomp/2910948e72b8928900f992dbf55ff415 to your computer and use it in GitHub Desktop.
Save porglezomp/2910948e72b8928900f992dbf55ff415 to your computer and use it in GitHub Desktop.
macro_rules! parsable_enum {
($(#[$attrs:meta])* enum $name:ident { $($member:ident),*}) =>
(parsable_enum! { $(#[$attrs])* enum $name { $($member),* ,}});
($(#[$attrs:meta])* enum $name:ident { $($member:ident),* , }) => {
$(#[$attrs])*
pub enum $name {
$($member),+
}
use std::str::FromStr;
impl FromStr for $name {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
$(stringify!($member) => Ok($name::$member)),+ ,
e => Err(e.into()),
}
}
}
}
}
parsable_enum! {
#[derive(PartialEq, Eq, Debug)]
enum Possible {
A,
B,
C,
D,
E,
F,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment