Skip to content

Commit

Permalink
feat(cmd_parse): take a refrence to the hashmap
Browse files Browse the repository at this point in the history
  • Loading branch information
0x61nas committed Aug 24, 2023
1 parent 0ed22dd commit f4d845e
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/helper/docs/cmd_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ use std::collections::HashMap;
/// `<cmd> [<subcommand>] [<args>]`
/// the subcommand and args are optional.
///
/// This function will add at least the `cmd` key to the `values_map` and 3 keys at most.
/// - `cmd`: The command name.
/// - `subcommand`: The command subcommand.
/// - `args`: The command arguments.
///
/// # Example
/// ```
/// # use halp::helper::docs::cmd_parse::parse_cmd;
/// # use std::collections::HashMap;
///
/// let git_commit = "git commit -a";
/// let mut values_map = parse_cmd(git_commit);
/// let mut values_map = HashMap::with_capacity(3);
/// parse_cmd(git_commit, &mut values_map);
/// assert!(values_map.contains_key("cmd"));
/// assert_eq!(values_map.get("cmd").unwrap(), "git");
/// assert!(values_map.contains_key("subcommand"));
Expand All @@ -22,8 +29,7 @@ use std::collections::HashMap;
///
/// # Panics
/// THIS FUNCTION WILL PANIC IF THE COMMAND STRING IS EMPTY.
pub fn parse_cmd(cmd: &str) -> HashMap<String, String> {
let mut values_map = HashMap::with_capacity(3);
pub fn parse_cmd(cmd: &str, values_map: &mut HashMap<String, String>) {
let cmd = cmd.trim().to_string();
let mut iter = cmd.split_whitespace();
// The `cmd` should be the first value.
Expand All @@ -40,7 +46,6 @@ pub fn parse_cmd(cmd: &str) -> HashMap<String, String> {
args.push_str(&format!(" {}", part));
}
}
values_map
}

#[cfg(test)]
Expand All @@ -51,7 +56,8 @@ mod tests {
#[test]
fn test_parse_complete_cmd() {
let git_commit = "git commit -a";
let values_map = parse_cmd(git_commit);
let mut values_map = HashMap::with_capacity(3);
parse_cmd(git_commit, &mut values_map);
assert!(values_map.contains_key("cmd"));
assert_eq!(values_map.get("cmd"), Some(&"git".to_string()));
assert!(values_map.contains_key("subcommand"));
Expand All @@ -63,7 +69,8 @@ mod tests {
#[test]
fn test_parse_cmd_with_no_args() {
let git_commit = "git commit";
let values_map = parse_cmd(git_commit);
let mut values_map = HashMap::with_capacity(3);
parse_cmd(git_commit, &mut values_map);
assert!(values_map.contains_key("cmd"));
assert_eq!(values_map.get("cmd"), Some(&"git".to_string()));
assert!(values_map.contains_key("subcommand"));
Expand All @@ -74,7 +81,8 @@ mod tests {
#[test]
fn test_parse_cmd_with_no_subcommand() {
let git_commit = "git";
let values_map = parse_cmd(git_commit);
let mut values_map = HashMap::with_capacity(3);
parse_cmd(git_commit, &mut values_map);
assert!(values_map.contains_key("cmd"));
assert_eq!(values_map.get("cmd"), Some(&"git".to_string()));
assert!(!values_map.contains_key("subcommand"));
Expand All @@ -84,7 +92,8 @@ mod tests {
#[test]
fn test_parse_cmd_with_no_subcommand_and_args() {
let git_commit = "git";
let values_map = parse_cmd(git_commit);
let mut values_map = HashMap::with_capacity(3);
parse_cmd(git_commit, &mut values_map);
assert!(values_map.contains_key("cmd"));
assert_eq!(values_map.get("cmd"), Some(&"git".to_string()));
assert!(!values_map.contains_key("subcommand"));
Expand All @@ -94,7 +103,8 @@ mod tests {
#[test]
fn test_parse_cmd_with_args_and_no_subcommand() {
let command = "ps -aux";
let values_map = parse_cmd(command);
let mut values_map = HashMap::with_capacity(3);
parse_cmd(command, &mut values_map);
assert!(values_map.contains_key("cmd"));
assert_eq!(values_map.get("cmd"), Some(&"ps".to_string()));
assert!(!values_map.contains_key("subcommand"));
Expand All @@ -105,7 +115,8 @@ mod tests {
#[test]
fn test_parse_cmd_with_two_args_and_no_subcommand() {
let command = "ps -aux -l";
let values_map = parse_cmd(command);
let mut values_map = HashMap::with_capacity(3);
parse_cmd(command, &mut values_map);
assert!(values_map.contains_key("cmd"));
assert_eq!(values_map.get("cmd"), Some(&"ps".to_string()));
assert!(!values_map.contains_key("subcommand"));
Expand All @@ -116,7 +127,8 @@ mod tests {
#[test]
fn test_parse_cmd_with_three_args_and_subcommand() {
let command = "git commit -a -m \"commit message\"";
let values_map = parse_cmd(command);
let mut values_map = HashMap::with_capacity(3);
parse_cmd(command, &mut values_map);
assert!(values_map.contains_key("cmd"));
assert_eq!(values_map.get("cmd"), Some(&"git".to_string()));
assert!(values_map.contains_key("subcommand"));
Expand Down

0 comments on commit f4d845e

Please sign in to comment.