From 4ff02a66fccdbfa885f2449ca412439165ea5f7e Mon Sep 17 00:00:00 2001 From: Nicolas <> Date: Sat, 9 Apr 2022 19:37:57 +0200 Subject: [PATCH] Replace code with macro --- src/main.rs | 59 +++++++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 36 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7b2e488..5cea92f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,23 +35,27 @@ mod timer; mod runner; mod choose; -#[derive(Copy, Clone, ArgEnum)] -enum AnimationType { - Circle, - Rhombus, - Rotation -} - -impl Options for AnimationType { - fn all() -> Vec where Self: Sized { - use AnimationType::*; - - vec![Circle, Rhombus, Rotation] +macro_rules! options { + ($name:ident { $($opt:ident,)* }) => { + #[derive(Copy, Clone, ArgEnum)] + enum $name { + $($opt,)* + } + impl Options for $name { + fn all() -> Vec { + vec![$($name::$opt,)*] + } + } } } -#[derive(Copy, Clone, ArgEnum)] -enum ColorType { +options!(AnimationType { + Circle, + Rhombus, + Rotation, +}); + +options!(ColorType { Red, Green, Blue, @@ -60,33 +64,16 @@ enum ColorType { LightBlue, Grey, Rainbow, -} +}); -impl Options for ColorType { - fn all() -> Vec where Self: Sized { - use ColorType::*; - - vec![Red, Green, Blue, LightRed, LightGreen, LightBlue, Grey, Rainbow] - } -} - -#[derive(Copy, Clone, ArgEnum)] -enum FillModeType { +options!(FillModeType { Circle, Level, - Stripes -} - -impl Options for FillModeType { - fn all() -> Vec where Self: Sized { - use FillModeType::*; - - vec![Circle, Level] - } -} + Stripes, +}); #[derive(Parser)] -#[clap(author = env!("CARGO_PKG_AUTHORS"), version = env!("CARGO_PKG_VERSION"), about = env!("CARGO_PKG_DESCRIPTION"))] +#[clap(author = env ! ("CARGO_PKG_AUTHORS"), version = env ! ("CARGO_PKG_VERSION"), about = env ! ("CARGO_PKG_DESCRIPTION"))] struct Args { #[clap(short, long, help = "Add animation", arg_enum)] animation: Vec,