Skip to content

Commit

Permalink
feat: introduced required parameter list
Browse files Browse the repository at this point in the history
  • Loading branch information
felsweg-iota committed Oct 17, 2022
1 parent f58e066 commit 39360cd
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 65 deletions.
119 changes: 57 additions & 62 deletions client/examples/repl/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,8 @@ impl Command for InitCommand {
"init".to_string()
}

fn required_param_length(&self) -> usize {
1
}

fn error_message(&self) -> String {
"requires one argument: <client_path>".to_string()
fn required_parameters(&self) -> Vec<String> {
vec!["<client_path>"].iter().map(|s| s.to_string()).collect()
}
}

Expand Down Expand Up @@ -90,12 +86,11 @@ impl Command for GenerateKeyCommand {
"keygen".to_string()
}

fn required_param_length(&self) -> usize {
3
}

fn error_message(&self) -> String {
"requires three arguments: <key_type> <vault_path> <record_path>".to_string()
fn required_parameters(&self) -> Vec<String> {
vec!["<key_type>", "<vault_path>", "<record_path>"]
.iter()
.map(|s| s.to_string())
.collect()
}
}

Expand Down Expand Up @@ -123,12 +118,8 @@ impl Command for CheckVaultCommand {
"checkvault".to_string()
}

fn required_param_length(&self) -> usize {
1
}

fn error_message(&self) -> String {
"requires one argument: <vault_path>".to_string()
fn required_parameters(&self) -> Vec<String> {
vec!["<vault_path>"].iter().map(|s| s.to_string()).collect()
}
}

Expand Down Expand Up @@ -160,12 +151,11 @@ impl Command for CheckRecordCommand {
"checkrecord".to_string()
}

fn required_param_length(&self) -> usize {
2
}

fn error_message(&self) -> String {
"requires two arguments: <vault_path> <record_path>".to_string()
fn required_parameters(&self) -> Vec<String> {
vec!["<vault_path>", "<record_path>"]
.iter()
.map(|s| s.to_string())
.collect()
}
}

Expand All @@ -189,12 +179,11 @@ impl Command for BackupCommand {
"backup".to_string()
}

fn required_param_length(&self) -> usize {
2
}

fn error_message(&self) -> String {
"requires two arguments: <path_to_snapshot_location> <passphrase>".to_string()
fn required_parameters(&self) -> Vec<String> {
vec!["<path_to_snapshot_location>", "<passphrase>"]
.iter()
.map(|s| s.to_string())
.collect()
}
}

Expand All @@ -218,12 +207,11 @@ impl Command for RestoreCommand {
"restore".to_string()
}

fn required_param_length(&self) -> usize {
2
}

fn error_message(&self) -> String {
"requires two arguments: <path_to_snapshot_location> <passphrase>".to_string()
fn required_parameters(&self) -> Vec<String> {
vec!["<path_to_snapshot_location>", "<passphrase>"]
.iter()
.map(|s| s.to_string())
.collect()
}
}

Expand Down Expand Up @@ -253,12 +241,11 @@ impl Command for Slip10GenerateCommand {
"slip10gen".to_string()
}

fn required_param_length(&self) -> usize {
2
}

fn error_message(&self) -> String {
"requires two arguments: <vault_path> <record_path>".to_string()
fn required_parameters(&self) -> Vec<String> {
vec!["<vault_path>", "<record_path>"]
.iter()
.map(|s| s.to_string())
.collect()
}
}

Expand Down Expand Up @@ -295,13 +282,17 @@ impl Command for Slip10DeriveCommand {
"slip10derive".to_string()
}

fn required_param_length(&self) -> usize {
5
}

fn error_message(&self) -> String {
"requires two arguments: <chain> <vault_path_origin> <record_path_origin> <vault_path_derive> <record_path_derive>"
.to_string()
fn required_parameters(&self) -> Vec<String> {
vec![
"<chain>",
"<vault_path_origin>",
"<record_path_origin>",
"<vault_path_derive>",
"<record_path_derive>",
]
.iter()
.map(|s| s.to_string())
.collect()
}
}

Expand Down Expand Up @@ -331,12 +322,11 @@ impl Command for Bip39GenerateCommand {
"mnemonic".to_string()
}

fn required_param_length(&self) -> usize {
4
}

fn error_message(&self) -> String {
"requires four arguments: <passphrase> <language> <vault_path> <record_path>".to_string()
fn required_parameters(&self) -> Vec<String> {
vec!["<passphrase>", "<language>", "<vault_path>", "<record_path>"]
.iter()
.map(|s| s.to_string())
.collect()
}
}

Expand Down Expand Up @@ -366,13 +356,18 @@ impl Command for Bip39RestoreCommand {
"bip39restore".to_string()
}

fn required_param_length(&self) -> usize {
4
}

fn error_message(&self) -> String {
"requires 3 arguments: <passphrase> <language> <vault_path_origin> <record_path_origin> <vault_path_derive> <record_path_derive>"
.to_string()
fn required_parameters(&self) -> Vec<String> {
vec![
"<passphrase>",
"<language>",
"<vault_path_origin>",
"<record_path_origin>",
"<vault_path_derive>",
"<record_path_derive>",
]
.iter()
.map(|s| s.to_string())
.collect()
}
}

Expand Down
16 changes: 13 additions & 3 deletions client/examples/repl/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub trait Command {

/// Validates the number of input tokens, otherwise displays the provided error message
fn validate(&self, parameter: &Vec<String>) -> Result<(), ReplError<String>> {
if parameter.len().ne(&self.required_param_length()) {
if parameter.len().ne(&self.required_parameters().len()) {
return Err(ReplError::Invalid(format!(
"'{}' {}",
self.name(),
Expand All @@ -84,13 +84,23 @@ pub trait Command {
fn name(&self) -> String;

/// Returns the number of required parameters
fn required_param_length(&self) -> usize {
fn required_parameters(&self) -> Vec<String> {
Default::default()
}

/// Returns the error message for the command
fn error_message(&self) -> String {
Default::default()
let plural = |a: usize| match a {
1 => "",
_ => "s",
};
let req = self.required_parameters();
format!(
"requires {} argument{}: {}",
req.len(),
plural(req.len()),
req.join(" ")
)
}
}

Expand Down

0 comments on commit 39360cd

Please sign in to comment.