-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the Enum! macro into Enum! and Command!
For an easier transition to structopt, this patch splits the two cases of the Enum! macro into two separate macros (that internally both call the new enum_int! macro).
- Loading branch information
1 parent
31fffa2
commit bf6dd8e
Showing
2 changed files
with
34 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
// arg_util.rs | ||
|
||
// ************************************************************************* | ||
// * Copyright (C) 2019 Daniel Mueller ([email protected]) * | ||
// * Copyright (C) 2019-2020 Daniel Mueller ([email protected]) * | ||
// * * | ||
// * This program is free software: you can redistribute it and/or modify * | ||
// * it under the terms of the GNU General Public License as published by * | ||
|
@@ -24,13 +24,16 @@ macro_rules! count { | |
} | ||
} | ||
|
||
/// A macro for generating an enum with a set of simple (i.e., no | ||
/// parameters) variants and their textual representations. | ||
// TODO: Right now we hard code the derives we create. We may want to | ||
// make this set configurable. | ||
macro_rules! Enum { | ||
macro_rules! Command { | ||
( $name:ident, [ $( $var:ident => ($str:expr, $exec:expr), ) *] ) => { | ||
Enum! {$name, [ | ||
#[derive(Debug, PartialEq)] | ||
pub enum $name { | ||
$( | ||
$var, | ||
)* | ||
} | ||
|
||
enum_int! {$name, [ | ||
$( $var => $str, )* | ||
]} | ||
|
||
|
@@ -49,6 +52,13 @@ macro_rules! Enum { | |
} | ||
} | ||
}; | ||
} | ||
|
||
/// A macro for generating an enum with a set of simple (i.e., no | ||
/// parameters) variants and their textual representations. | ||
// TODO: Right now we hard code the derives we create. We may want to | ||
// make this set configurable. | ||
macro_rules! Enum { | ||
( $name:ident, [ $( $var:ident => $str:expr, ) *] ) => { | ||
#[derive(Clone, Copy, Debug, PartialEq)] | ||
pub enum $name { | ||
|
@@ -57,6 +67,14 @@ macro_rules! Enum { | |
)* | ||
} | ||
|
||
enum_int! {$name, [ | ||
$( $var => $str, )* | ||
]} | ||
}; | ||
} | ||
|
||
macro_rules! enum_int { | ||
( $name:ident, [ $( $var:ident => $str:expr, ) *] ) => { | ||
impl $name { | ||
#[allow(unused)] | ||
pub fn all(&self) -> [$name; count!($($var),*) ] { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
// args.rs | ||
|
||
// ************************************************************************* | ||
// * Copyright (C) 2018-2019 Daniel Mueller ([email protected]) * | ||
// * Copyright (C) 2018-2020 Daniel Mueller ([email protected]) * | ||
// * * | ||
// * This program is free software: you can redistribute it and/or modify * | ||
// * it under the terms of the GNU General Public License as published by * | ||
|
@@ -128,7 +128,7 @@ impl From<DeviceModel> for nitrokey::Model { | |
|
||
/// A top-level command for nitrocli. | ||
#[allow(unused_doc_comments)] | ||
Enum! {Command, [ | ||
Command! {Command, [ | ||
Config => ("config", config), | ||
Encrypted => ("encrypted", encrypted), | ||
Hidden => ("hidden", hidden), | ||
|
@@ -141,7 +141,7 @@ Enum! {Command, [ | |
Unencrypted => ("unencrypted", unencrypted), | ||
]} | ||
|
||
Enum! {ConfigCommand, [ | ||
Command! {ConfigCommand, [ | ||
Get => ("get", config_get), | ||
Set => ("set", config_set), | ||
]} | ||
|
@@ -181,7 +181,7 @@ impl<T> ConfigOption<T> { | |
} | ||
} | ||
|
||
Enum! {OtpCommand, [ | ||
Command! {OtpCommand, [ | ||
Clear => ("clear", otp_clear), | ||
Get => ("get", otp_get), | ||
Set => ("set", otp_set), | ||
|
@@ -213,13 +213,13 @@ Enum! {OtpSecretFormat, [ | |
Hex => "hex", | ||
]} | ||
|
||
Enum! {PinCommand, [ | ||
Command! {PinCommand, [ | ||
Clear => ("clear", pin_clear), | ||
Set => ("set", pin_set), | ||
Unblock => ("unblock", pin_unblock), | ||
]} | ||
|
||
Enum! {PwsCommand, [ | ||
Command! {PwsCommand, [ | ||
Clear => ("clear", pws_clear), | ||
Get => ("get", pws_get), | ||
Set => ("set", pws_set), | ||
|
@@ -257,7 +257,7 @@ fn reset(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> { | |
commands::reset(ctx) | ||
} | ||
|
||
Enum! {UnencryptedCommand, [ | ||
Command! {UnencryptedCommand, [ | ||
Set => ("set", unencrypted_set), | ||
]} | ||
|
||
|
@@ -314,7 +314,7 @@ fn unencrypted_set(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> { | |
commands::unencrypted_set(ctx, mode) | ||
} | ||
|
||
Enum! {EncryptedCommand, [ | ||
Command! {EncryptedCommand, [ | ||
Close => ("close", encrypted_close), | ||
Open => ("open", encrypted_open), | ||
]} | ||
|
@@ -364,7 +364,7 @@ fn encrypted_close(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> { | |
commands::encrypted_close(ctx) | ||
} | ||
|
||
Enum! {HiddenCommand, [ | ||
Command! {HiddenCommand, [ | ||
Close => ("close", hidden_close), | ||
Create => ("create", hidden_create), | ||
Open => ("open", hidden_open), | ||
|