Skip to content

Commit d1219f0

Browse files
committed
feat(macros): arg_enum! and simple_enum! auto-implement Display
enums created with simple_enum! and arg_enum! now auto-implement std::fmt::Display where the variant only is displayed. Closes #120
1 parent 0c264a8 commit d1219f0

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/macros.rs

+44
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ macro_rules! value_t_or_exit {
277277
/// Convenience macro generated a simple enum with variants to be used as a type when parsing
278278
/// arguments.
279279
///
280+
/// **NOTE:** This macro automaically implements std::str::FromStr and std::fmt::Display
281+
///
280282
/// # Example
281283
///
282284
/// ```no_run
@@ -322,6 +324,14 @@ macro_rules! simple_enum {
322324
}
323325
}
324326
}
327+
328+
impl ::std::fmt::Display for $e {
329+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
330+
match *self {
331+
$($e::$v => write!(f, stringify!($v)),)+
332+
}
333+
}
334+
}
325335
};
326336
}
327337

@@ -330,6 +340,8 @@ macro_rules! simple_enum {
330340
///
331341
/// **NOTE:** Case insensitivity is supported for ASCII characters
332342
///
343+
/// **NOTE:** This macro automaically implements std::str::FromStr and std::fmt::Display
344+
///
333345
/// These enums support pub (or not) and use of the #[derive()] traits
334346
///
335347
///
@@ -385,6 +397,14 @@ macro_rules! arg_enum {
385397
}
386398
}
387399
}
400+
401+
impl ::std::fmt::Display for $e {
402+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
403+
match *self {
404+
$($e::$v => write!(f, stringify!($v)),)+
405+
}
406+
}
407+
}
388408
};
389409
(pub enum $e:ident { $($v:ident),+ } ) => {
390410
pub enum $e {
@@ -411,6 +431,14 @@ macro_rules! arg_enum {
411431
}
412432
}
413433
}
434+
435+
impl ::std::fmt::Display for $e {
436+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
437+
match *self {
438+
$($e::$v => write!(f, stringify!($v)),)+
439+
}
440+
}
441+
}
414442
};
415443
(#[derive($($d:ident),+)] enum $e:ident { $($v:ident),+ } ) => {
416444
#[derive($($d,)+)]
@@ -438,6 +466,14 @@ macro_rules! arg_enum {
438466
}
439467
}
440468
}
469+
470+
impl ::std::fmt::Display for $e {
471+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
472+
match *self {
473+
$($e::$v => write!(f, stringify!($v)),)+
474+
}
475+
}
476+
}
441477
};
442478
(#[derive($($d:ident),+)] pub enum $e:ident { $($v:ident),+ } ) => {
443479
#[derive($($d,)+)]
@@ -465,6 +501,14 @@ macro_rules! arg_enum {
465501
}
466502
}
467503
}
504+
505+
impl ::std::fmt::Display for $e {
506+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
507+
match *self {
508+
$($e::$v => write!(f, stringify!($v)),)+
509+
}
510+
}
511+
}
468512
};
469513
}
470514

0 commit comments

Comments
 (0)