-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.rs
More file actions
115 lines (103 loc) · 3.75 KB
/
commands.rs
File metadata and controls
115 lines (103 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
mod add;
mod show;
mod delete;
mod help;
use std::collections::HashMap;
use crate::utils::error;
use crate::utils::success::ExecutionResult;
///
/// # Items
/// - [`CommandType::ADD`] - Add to bookmarks
/// - [`CommandType::SHOW`] - Show bookmark(s)
/// - [`CommandType::DELETE`] - Delete given bookmark
/// - [`CommandType::CONFIG`] - Set/Get config values
/// - [`CommandType::HELP`] - Display help
/// - [`CommandType::NONE`] - For initialization, do NOT use i
///
/// Use [`Command::new`] with one of the enum items listed above to create command.
///
/// # Examples
#[derive(Debug)]
pub enum CommandType {
NONE, // Default value. Should not be used besides initialization of command.
ADD,
SHOW,
DELETE,
HELP,
VERSION
}
pub struct Command {
type_of : CommandType,
args : Option<Vec<String>>
}
impl Command {
pub fn new(type_of : CommandType, args: Option<Vec<String>>) -> Command {
Command {
type_of,
args
}
}
pub fn print_command(&self) {
println!("[DBG|CMD:{:?} --> {:?}]", self.type_of, self.args);
}
/// Execute
pub fn execute(&self, store: &mut HashMap<String, String>) -> ExecutionResult {
let mut result : ExecutionResult = Default::default();
let params = &self.args;
match self.type_of {
CommandType::NONE => {
error::print_error_and_exit("Impossible command.".into(), error::ErrorCode::ImpossibleCommand);
}
CommandType::ADD => {
result = crate::commands::add::add(params, store);
}
CommandType::SHOW => {
result = crate::commands::show::show(params, store.to_owned());
}
CommandType::DELETE => {
result = crate::commands::delete::delete(params, store);
}
CommandType::HELP => {
crate::commands::help::print_help();
}
CommandType::VERSION => {
crate::commands::help::print_version();
}
}
result
// FIXME put command in execution functions to get rid of following checks.
// match &self.args {
// // Commands with no argument
// None => {
// match self.type_of {
// CommandType::HELP => {
// crate::utils::startup::print_help();
// }
// _ => {
// error::print_error_and_exit("Command cannot be verified with given args".into(),
// error::ErrorCode::CommandVerificationError);
// }
// }
// return ExecutionResult{ success: false, write_to_file: false };
// },
// Some(params) => {
// match &self.type_of {
// CommandType::NONE => {
// error::print_error_and_exit("Impossible command.".into(), error::ErrorCode::ImpossibleCommand);
// // return ExecutionResult{ success: false, write_to_file: false };
// } // Command NONE
// CommandType::ADD => {
// // return add(params, store)
// } // Command ADD
// CommandType::SHOW => {
//
// } // Command SHOW
// CommandType::DELETE => {} // Command DELETE
// CommandType::CONFIG => {} // Command CONFIG
// CommandType::HELP => {} // Command HELP
// } // match command types
// } // Commands
// } // match
// return ExecutionResult{ success: false, write_to_file: false };
}
}