Skip to content

Commit

Permalink
feat(parser): create the command parser
Browse files Browse the repository at this point in the history
  • Loading branch information
0x61nas committed Aug 23, 2023
1 parent 399dfee commit 5b054fb
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/helper/docs/cmd_parse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use std::collections::HashMap;

/// Parses a command string into a HashMap that contains the command parts.
///
/// the command string is expected to be in the format:
/// `cmd <subcommand> <args>`
/// the subcommand and args are optional.
///
/// # Example
/// ```
/// # use halp::helper::docs::cmd_parse::parse_cmd;
///
/// let git_commit = "git commit -a";
/// let mut values_map = parse_cmd(git_commit);
/// assert!(values_map.contains_key("cmd"));
/// assert_eq!(values_map.get("cmd").unwrap(), "git");
/// assert!(values_map.contains_key("subcommand"));
/// assert_eq!(values_map.get("subcommand").unwrap(), "commit");
/// assert!(values_map.contains_key("args"));
/// assert_eq!(values_map.get("args").unwrap(), "-a");
/// ```
///
/// # 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);
let cmd = cmd.to_string();
if cmd.contains(' ') {
let mut cmd = cmd.split(' ');
values_map.insert("cmd".to_string(), cmd.next().expect("The command should exist").to_string());
if let Some(subcommand) = cmd.next() {
values_map.insert("subcommand".to_string(), subcommand.to_string());
}
if let Some(args) = cmd.next() {
values_map.insert("args".to_string(), args.to_string());
}
} else {
values_map.insert("cmd".to_string(), cmd);
}
values_map
}

#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;

#[test]
fn test_parse_complete_cmd() {
let git_commit = "git commit -a";
let values_map = parse_cmd(git_commit);
assert!(values_map.contains_key("cmd"));
assert_eq!(values_map.get("cmd"), Some(&"git".to_string()));
assert!(values_map.contains_key("subcommand"));
assert_eq!(values_map.get("subcommand"), Some(&"commit".to_string()));
assert!(values_map.contains_key("args"));
assert_eq!(values_map.get("args"), Some(&"-a".to_string()));
}

#[test]
fn test_parse_cmd_with_no_args() {
let git_commit = "git commit";
let values_map = parse_cmd(git_commit);
assert!(values_map.contains_key("cmd"));
assert_eq!(values_map.get("cmd"), Some(&"git".to_string()));
assert!(values_map.contains_key("subcommand"));
assert_eq!(values_map.get("subcommand"), Some(&"commit".to_string()));
assert!(!values_map.contains_key("args"));
}

#[test]
fn test_parse_cmd_with_no_subcommand() {
let git_commit = "git";
let values_map = parse_cmd(git_commit);
assert!(values_map.contains_key("cmd"));
assert_eq!(values_map.get("cmd"), Some(&"git".to_string()));
assert!(!values_map.contains_key("subcommand"));
assert!(!values_map.contains_key("args"));
}

#[test]
fn test_parse_cmd_with_no_subcommand_and_args() {
let git_commit = "git";
let values_map = parse_cmd(git_commit);
assert!(values_map.contains_key("cmd"));
assert_eq!(values_map.get("cmd"), Some(&"git".to_string()));
assert!(!values_map.contains_key("subcommand"));
assert!(!values_map.contains_key("args"));
}
}

0 comments on commit 5b054fb

Please sign in to comment.