Merge pull request #1124 from Enet4/arg_enum_trailing_comma

Support trailing comma in arg_enum! fields
This commit is contained in:
Kevin K 2017-12-07 16:45:28 -05:00 committed by GitHub
commit e0f8479e0f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 1 deletions

View file

@ -345,6 +345,14 @@ macro_rules! arg_enum {
} }
}); });
}; };
($(#[$($m:meta),+])+ pub enum $e:ident { $($v:ident $(=$val:expr)*,)+ } ) => {
arg_enum!(@impls
($(#[$($m),+])+
pub enum $e {
$($v$(=$val)*),+
}) -> ($e, $($v),+)
);
};
($(#[$($m:meta),+])+ pub enum $e:ident { $($v:ident $(=$val:expr)*),+ } ) => { ($(#[$($m:meta),+])+ pub enum $e:ident { $($v:ident $(=$val:expr)*),+ } ) => {
arg_enum!(@impls arg_enum!(@impls
($(#[$($m),+])+ ($(#[$($m),+])+
@ -353,6 +361,13 @@ macro_rules! arg_enum {
}) -> ($e, $($v),+) }) -> ($e, $($v),+)
); );
}; };
($(#[$($m:meta),+])+ enum $e:ident { $($v:ident $(=$val:expr)*,)+ } ) => {
arg_enum!($(#[$($m:meta),+])+
enum $e:ident {
$($v:ident $(=$val:expr)*),+
}
);
};
($(#[$($m:meta),+])+ enum $e:ident { $($v:ident $(=$val:expr)*),+ } ) => { ($(#[$($m:meta),+])+ enum $e:ident { $($v:ident $(=$val:expr)*),+ } ) => {
arg_enum!(@impls arg_enum!(@impls
($(#[$($m),+])+ ($(#[$($m),+])+
@ -361,6 +376,11 @@ macro_rules! arg_enum {
}) -> ($e, $($v),+) }) -> ($e, $($v),+)
); );
}; };
(pub enum $e:ident { $($v:ident $(=$val:expr)*,)+ } ) => {
arg_enum!(pub enum $e:ident {
$($v:ident $(=$val:expr)*),+
});
};
(pub enum $e:ident { $($v:ident $(=$val:expr)*),+ } ) => { (pub enum $e:ident { $($v:ident $(=$val:expr)*),+ } ) => {
arg_enum!(@impls arg_enum!(@impls
(pub enum $e { (pub enum $e {
@ -368,6 +388,11 @@ macro_rules! arg_enum {
}) -> ($e, $($v),+) }) -> ($e, $($v),+)
); );
}; };
(enum $e:ident { $($v:ident $(=$val:expr)*,)+ } ) => {
arg_enum!(enum $e:ident {
$($v:ident $(=$val:expr)*),+
});
};
(enum $e:ident { $($v:ident $(=$val:expr)*),+ } ) => { (enum $e:ident { $($v:ident $(=$val:expr)*),+ } ) => {
arg_enum!(@impls arg_enum!(@impls
(enum $e { (enum $e {

View file

@ -159,3 +159,15 @@ fn arg_enum() {
} }
assert_eq!("Alpha".parse::<Greek>(), Ok(Greek::Alpha)); assert_eq!("Alpha".parse::<Greek>(), Ok(Greek::Alpha));
} }
#[test]
fn arg_enum_trailing_comma() {
arg_enum!{
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Greek {
Alpha,
Bravo,
}
}
assert_eq!("Alpha".parse::<Greek>(), Ok(Greek::Alpha));
}