diff --git a/src/helper/docs/cmd_parse.rs b/src/helper/docs/cmd_parse.rs index e96c546..0b31383 100644 --- a/src/helper/docs/cmd_parse.rs +++ b/src/helper/docs/cmd_parse.rs @@ -6,12 +6,19 @@ use std::collections::HashMap; /// ` [] []` /// 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")); @@ -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 { - let mut values_map = HashMap::with_capacity(3); +pub fn parse_cmd(cmd: &str, values_map: &mut HashMap) { let cmd = cmd.trim().to_string(); let mut iter = cmd.split_whitespace(); // The `cmd` should be the first value. @@ -40,7 +46,6 @@ pub fn parse_cmd(cmd: &str) -> HashMap { args.push_str(&format!(" {}", part)); } } - values_map } #[cfg(test)] @@ -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")); @@ -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")); @@ -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")); @@ -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")); @@ -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")); @@ -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")); @@ -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"));