From ba66c4bebc4dcf416f71e801f4d16b8726dc1173 Mon Sep 17 00:00:00 2001
From: dvermd <888-dvermd@users.noreply.git.duniter.org>
Date: Thu, 23 Jan 2020 22:51:59 +0100
Subject: [PATCH] [fix] core:commands: #108 rework keys cli options

---
 Cargo.lock                         | 20 +++++++++++++++
 lib/core/core/Cargo.toml           |  2 ++
 lib/core/core/src/commands/keys.rs | 39 ++++++++++++++++++++----------
 3 files changed, 48 insertions(+), 13 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index dc959cf6..ccd2bc1c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1051,6 +1051,8 @@ dependencies = [
  "serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)",
  "structopt 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
+ "strum 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "strum_macros 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "unwrap 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
@@ -2721,6 +2723,22 @@ dependencies = [
  "syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "strum"
+version = "0.17.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "strum_macros"
+version = "0.17.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "subtle"
 version = "1.0.0"
@@ -3427,6 +3445,8 @@ dependencies = [
 "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
 "checksum structopt 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "30b3a3e93f5ad553c38b3301c8a0a0cec829a36783f6a0c467fc4bf553a5f5bf"
 "checksum structopt-derive 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea692d40005b3ceba90a9fe7a78fa8d4b82b0ce627eebbffc329aab850f3410e"
+"checksum strum 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)" = "530efb820d53b712f4e347916c5e7ed20deb76a4f0457943b3182fb889b06d2c"
+"checksum strum_macros 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5e6e163a520367c465f59e0a61a23cfae3b10b6546d78b6f672a382be79f7110"
 "checksum subtle 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee"
 "checksum syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)" = "dff0acdb207ae2fe6d5976617f887eb1e35a2ba52c13c7234c790960cdad9238"
 "checksum synstructure 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545"
diff --git a/lib/core/core/Cargo.toml b/lib/core/core/Cargo.toml
index b8bc60d0..0709aad3 100644
--- a/lib/core/core/Cargo.toml
+++ b/lib/core/core/Cargo.toml
@@ -31,6 +31,8 @@ serde = "1.0.*"
 serde_derive = "1.0.*"
 serde_json = "1.0.*"
 structopt= "0.3.4"
+strum = "0.17.1"
+strum_macros = "0.17.1"
 unwrap = "1.2.1"
 
 [features]
diff --git a/lib/core/core/src/commands/keys.rs b/lib/core/core/src/commands/keys.rs
index b67fef8e..710cc66d 100644
--- a/lib/core/core/src/commands/keys.rs
+++ b/lib/core/core/src/commands/keys.rs
@@ -21,6 +21,7 @@ use crate::DursCore;
 use clear_on_drop::clear::Clear;
 use durs_conf::keys::*;
 use durs_conf::DuRsConf;
+use strum_macros::EnumString;
 
 #[derive(StructOpt, Debug, Clone)]
 #[structopt(
@@ -94,17 +95,29 @@ pub enum ModifySubCommand {
 #[derive(StructOpt, Debug, Copy, Clone)]
 /// ClearOpt
 pub struct ClearOpt {
-    #[structopt(short = "m", long = "member")]
-    /// True if we change member key
-    pub member: bool,
-
-    #[structopt(short = "n", long = "network")]
-    /// True if we change network key
-    pub network: bool,
-
-    #[structopt(short = "a", long = "all")]
-    /// True if we change member and network key
-    pub all: bool,
+    /// Key to clear: member, network or all (both member and network)
+    key: KeyKind,
+}
+#[derive(Debug, Copy, Clone, PartialEq, EnumString)]
+#[strum(serialize_all = "kebab_case")]
+/// KeyKind
+pub enum KeyKind {
+    /// Member key
+    MEMBER,
+    /// Network key
+    NETWORK,
+    /// Both Member and Network keys
+    ALL,
+}
+impl KeyKind {
+    /// Returns if key kind is member
+    pub fn is_member(self) -> bool {
+        self == KeyKind::MEMBER || self == KeyKind::ALL
+    }
+    /// Returns if key kind is network
+    pub fn is_network(self) -> bool {
+        self == KeyKind::NETWORK || self == KeyKind::ALL
+    }
 }
 
 #[derive(StructOpt, Debug, Clone)]
@@ -169,8 +182,8 @@ impl DursExecutableCoreCommand for KeysOpt {
             },
             KeysSubCommand::Clear(clear_opt) => {
                 let new_keypairs = clear_keys(
-                    clear_opt.network || clear_opt.all,
-                    clear_opt.member || clear_opt.all,
+                    clear_opt.key.is_network(),
+                    clear_opt.key.is_member(),
                     keypairs,
                 );
                 save_keypairs(profile_path, &keypairs_file, new_keypairs)
-- 
GitLab